Skip to content

Commit e4d04b5

Browse files
committed
Update README and project
1 parent 402264b commit e4d04b5

3 files changed

Lines changed: 47 additions & 13 deletions

File tree

README.md

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,32 @@
1-
# File type interrogator
1+
# FileTypeInterrogator
22

3-
Determine file type by 'Magic Numbers' for most common file types instead of trusting file extensions
3+
netstandard library for detecting file types by 'magic numbers', similar to the `file` command in Linux/Unix. Useful for validating file uploads instead of trusting file extensions.
44

5-
# License
5+
# Usage
66

7-
Copyright 2018 Daniel Destouche
7+
```
8+
IFileTypeInterrogator interrogator = new FileTypeInterrogator();
89
9-
Licensed under the Apache License, Version 2.0 (the "License");
10-
you may not use this file except in compliance with the License.
11-
You may obtain a copy of the License at
10+
byte[] fileBytes = File.ReadAllBytes("pdfFile.pdf");
1211
13-
http://www.apache.org/licenses/LICENSE-2.0
12+
FileTypeInfo fileTypeInfo = interrogator.DetectType(fileBytes);
1413
15-
Unless required by applicable law or agreed to in writing, software
16-
distributed under the License is distributed on an "AS IS" BASIS,
17-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18-
See the License for the specific language governing permissions and
19-
limitations under the License.
14+
Console.WriteLine("Name = " + fileTypeInfo.Name);
15+
Console.WriteLine("Extension = " + fileTypeInfo.FileType);
16+
Console.WriteLine("Mime Type = " + fileTypeInfo.MimeType);
17+
18+
// The following output would be displayed:
19+
// Name = Portable Document Format File
20+
// Extension = pdf
21+
// Mime Type = application/pdf
22+
```
23+
24+
Notice that `IFileTypeInterrogator` was assigned, meaning custom implementations are welcomed. File definitions are provided in the source and will be regularly updated, feel free to submit an issue or pull request to add other signatures. To quickly provide support for a new signature, create an instance of `CustomFileTypeInterrogator`:
25+
26+
```
27+
IFileTypeInterrogator interrogator = new CustomFileTypeInterrogator("path_to_custom_definitions_file", Encoding.UTF8);
28+
29+
FileTypeInfo fileTypeInfo = interrogator.DetectType(...);
30+
```
31+
32+
It is best to create a single instance and manage it's lifetime through an IOC/DI Container.

src/FileTypeInterrogator/FileTypeInfo.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ internal FileTypeInfo(string name, string fileType, string mimeType, byte[] head
6262
/// The additional identifier.
6363
/// </value>
6464
public byte[] SubHeader { get; private set; }
65+
66+
public override string ToString()
67+
{
68+
return $@"{Name} ({FileType})
69+
{MimeType}
70+
{string.Join("|", Alias ?? new string[] { })}";
71+
}
6572
}
6673

6774
}

src/FileTypeInterrogator/FileTypeInterrogator.csproj

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,24 @@
22

33
<PropertyGroup>
44
<TargetFrameworks>netstandard1.4;netstandard2.0</TargetFrameworks>
5+
<PackageProjectUrl>https://github.com/ghost1face/FileTypeInterrogator</PackageProjectUrl>
6+
<RepositoryUrl>https://github.com/ghost1face/FileTypeInterrogator</RepositoryUrl>
7+
<PackageTags>file-type-detection file-types magic-numbers content-type mime-type</PackageTags>
8+
<AssemblyVersion>1.0.0.0</AssemblyVersion>
9+
<Authors>ghost1face</Authors>
10+
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
11+
<PackageLicenseFile>LICENSE.md</PackageLicenseFile>
12+
<PackageReleaseNotes>Update to use flat file definitions, unit tested</PackageReleaseNotes>
13+
<Copyright>2019 @ BeyondTechIO</Copyright>
14+
<Description>netstandard library for detecting file types by 'magic numbers', similar to the `file` command in Linux/Unix. Useful for validating file uploads instead of trusting file extensions.</Description>
515
</PropertyGroup>
616

717
<ItemGroup>
818
<None Remove="definitions_flat" />
19+
<None Include="..\..\LICENSE.md">
20+
<Pack>True</Pack>
21+
<PackagePath></PackagePath>
22+
</None>
923
</ItemGroup>
1024

1125
<ItemGroup>

0 commit comments

Comments
 (0)