An open API service indexing awesome lists of open source software.

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

Awesome Lists containing this project

README

          

# Dmime
[![CI/CD](https://github.com/litenova/DMime/actions/workflows/publish-packages.yml/badge.svg)](https://github.com/litenova/DMime/actions/workflows/publish-packages.yml)
[![NuGet](https://img.shields.io/nuget/vpre/Dmime.svg)](https://www.nuget.org/packages/Dmime)
[![Coverage Status](https://coveralls.io/repos/github/litenova/DMime/badge.svg?branch=main)](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
* PDF
* 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