dotnet-MimeType
1.0.0
dotnet add package dotnet-MimeType --version 1.0.0
NuGet\Install-Package dotnet-MimeType -Version 1.0.0
<PackageReference Include="dotnet-MimeType" Version="1.0.0" />
<PackageVersion Include="dotnet-MimeType" Version="1.0.0" />
<PackageReference Include="dotnet-MimeType" />
paket add dotnet-MimeType --version 1.0.0
#r "nuget: dotnet-MimeType, 1.0.0"
#:package dotnet-MimeType@1.0.0
#addin nuget:?package=dotnet-MimeType&version=1.0.0
#tool nuget:?package=dotnet-MimeType&version=1.0.0
dotnet-mimetype
A .NET library for detecting MIME types and file extensions based on file signatures (magic bytes) and predefined mappings. This project provides a robust way to identify file types from byte arrays, streams, or file paths, supporting both signature-based detection and extension-to-MIME type mappings.
Overview
The dotnet-mimetype
library is designed to help developers identify file types and MIME types accurately in .NET applications. It supports detection through file signatures (magic bytes) for precise identification and includes a comprehensive mapping of MIME types to file extensions. The library is built with extensibility in mind, allowing developers to add support for additional file formats.
Features
- File Signature Detection: Detects file types by analyzing magic bytes from files, streams, or byte arrays.
- MIME Type Detection: Maps file extensions to their corresponding MIME types.
- Extensible Architecture: Easily extendable to support additional file formats through custom signature checkers.
- Thread-Safe Lazy Loading: Uses lazy-initialized collections for efficient and thread-safe access to file type and MIME type data.
- Exception Handling: Provides clear error messages for invalid or non-readable inputs.
Installation
Clone the repository and include the project in your .NET solution, or build and reference the compiled DLL.
git clone https://github.com/ibrahimekinci/dotnet-mimetype.git
Usage
Detecting File Types by Signature
using MimeType.Services;
using MimeType.Core.Models;
var detector = new FileSignatureDetector();
byte[] fileBytes = File.ReadAllBytes("sample.png");
var fileTypes = detector.Detect(fileBytes);
foreach (var fileType in fileTypes)
{
Console.WriteLine($"MIME: {fileType.Mime}, Extensions: {string.Join(", ", fileType.Extensions)}");
}
Detecting MIME Types by Extension
using MimeType.Services;
var mimeDetector = new MimeTypeDetector();
var mimeTypes = mimeDetector.Detect(".png");
foreach (var mime in mimeTypes)
{
Console.WriteLine($"MIME Type: {mime}");
}
Detecting File Extensions by MIME Type
using MimeType.Services;
var extensionDetector = new FileExtensionDetector();
var extensions = extensionDetector.Detect("image/png");
foreach (var ext in extensions)
{
Console.WriteLine($"Extension: {ext}");
}
Supported File Signatures
The library currently supports signature-based detection for the following image formats:
- BMP (.bmp): Starts with "BM".
- GIF (.gif): Starts with "GIF87a" or "GIF89a".
- PNG (.png): Starts with
0x89 0x50 0x4E 0x47 0x0D 0x0A 0x1A 0x0A
. - TIFF (.tiff, .tif): Starts with
0x49 0x49 0x2A 0x00
(II*) or0x4D 0x4D 0x00 0x2A
(MM*). - WebP (.webp): Starts with "RIFF" followed by "WEBP" at offset 8.
- JPEG (.jpg, .jpeg, .jpe, .jfif): Starts with
0xFF 0xD8 0xFF
. - JPEG XL (.jxl): Starts with
0xFF 0x0A
(raw codestream) or a 12-byte container signature. - DWG (.dwg): Starts with "AC" followed by version strings like "1.40", "1002", etc.
Additional file formats (e.g., audio, video, and archive formats) are supported for MIME type-to-extension mappings but lack signature detection at this time. Support for these formats will be added in future updates.
Contributing
We welcome contributions to expand the supported file formats and improve the library! To contribute:
- Fork the repository: https://github.com/ibrahimekinci/dotnet-mimetype
- Add new file signature checkers or enhance existing ones in the
MimeType.Infrastructure.FileSignatureCheckers
namespace. - Update the
BuiltInFileTypes
class to include new file signatures and mappings. - Submit a pull request with a clear description of your changes.
Please ensure your code follows the existing structure and includes appropriate tests.
References
License
This project is licensed under the MIT License. See the LICENSE file for details.
Author
Developed by Ibrahim Ekinci.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last Updated |
---|---|---|
1.0.0 | 137 | 9/1/2025 |