https://github.com/litenova/dmime
Detects a given file's MIME type based on file content regardless of file extension
https://github.com/litenova/dmime
csharp detector mime-type
Last synced: about 2 months ago
JSON representation
Detects a given file's MIME type based on file content regardless of file extension
- Host: GitHub
- URL: https://github.com/litenova/dmime
- Owner: litenova
- License: mit
- Created: 2021-06-03T21:21:45.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2023-07-07T06:30:35.000Z (over 2 years ago)
- Last Synced: 2025-08-01T01:42:57.888Z (2 months ago)
- Topics: csharp, detector, mime-type
- Language: C#
- Homepage:
- Size: 14.7 MB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Dmime
[](https://github.com/litenova/DMime/actions/workflows/publish-packages.yml)
[](https://www.nuget.org/packages/Dmime)
[](https://coveralls.io/github/litenova/DMime?branch=main)A fast and efficient library to detect a given file's MIME type based on the file content regardless of file extension.
* Written in .NET 7
* No Dependencies
* No Reflection
* Easy to Use
* Supported Types
* MP4
* BMP
* GIF
* JPEG
* PNG
* WEBP
* MP3
* EMF
* TIFF
* ICO
* WMF## How to Use
### Extension Method
Install the `DMime` package:
```
dotnet add package Dmime
```Then use the `DetectMimeTypeAsync` or `DetectMimeType` extension methods on any `Stream` type.
```c#
// an unkown file without extension
FileStream stream = File.OpenRead("webp-sample-550w-404h");var result = await stream.DetectMimeTypeAsync(); // result = image/webp
```### Dependency
Install the `Dmime.Extensions.MicrosoftDependencyInjection` package:
```
dotnet add package Dmime.Extensions.MicrosoftDependencyInjection
```Register the dependency into ASP.NET Core DI container:
```c#
services.AddMimeDetector();
```Then inject the `IMimeDetector` to your desired class and use the `DetectAsync` or `Detect` methods accordingly.
```c#
[ApiController]
[Route("[controller]")]
public class UploadController
{
private readonly IMimeDetector _mimeDetector;
public UploadController(IMimeDetector mimeDetector)
{
_mimeDetector = mimeDetector;
}
[HttpPost]
public async Task SendNotification(IFormFile formFile)
{
var streamContent = formFile.OpenReadStream();
var result = await _mimeDetector.DetectMimeTypeAsync(streamContent);
return Ok(result);
}
}
```## Sponsor
Thanks to JetBrains for supporting open source projects by providing JetBrains Rider IDE for free.
[![alt text][image]][hyperlink]
[hyperlink]: https://www.jetbrains.com/
[image]: assets/jetbrains-variant-4.png