https://github.com/ghost1face/filetypeinterrogator
Determine file type by 'Magic Numbers'
https://github.com/ghost1face/filetypeinterrogator
content-type csharp file-type-detection file-types
Last synced: 4 months ago
JSON representation
Determine file type by 'Magic Numbers'
- Host: GitHub
- URL: https://github.com/ghost1face/filetypeinterrogator
- Owner: ghost1face
- License: apache-2.0
- Created: 2015-10-02T16:24:09.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2023-01-11T01:05:39.000Z (over 2 years ago)
- Last Synced: 2024-08-31T09:47:43.803Z (9 months ago)
- Topics: content-type, csharp, file-type-detection, file-types
- Language: C#
- Homepage:
- Size: 16.8 MB
- Stars: 7
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# FileTypeInterrogator
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.
[](https://github.com/ghost1face/FileTypeInterrogator/actions/workflows/dotnet.yml)
[](https://www.nuget.org/packages/FileTypeInterrogator/)# Usage
```
IFileTypeInterrogator interrogator = new FileTypeInterrogator();byte[] fileBytes = File.ReadAllBytes("pdfFile.pdf");
FileTypeInfo fileTypeInfo = interrogator.DetectType(fileBytes);
Console.WriteLine("Name = " + fileTypeInfo.Name);
Console.WriteLine("Extension = " + fileTypeInfo.FileType);
Console.WriteLine("Mime Type = " + fileTypeInfo.MimeType);// The following output would be displayed:
// Name = Portable Document Format File
// Extension = pdf
// Mime Type = application/pdf
```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`:
```
IFileTypeInterrogator interrogator = new CustomFileTypeInterrogator("path_to_custom_definitions_file", Encoding.UTF8);FileTypeInfo fileTypeInfo = interrogator.DetectType(...);
```It is best to create a single instance and manage it's lifetime through an IOC/DI Container.