Added standard MIME types for all existing types#44
Conversation
|
Could you please share some details about why and where this need is coming from? Why this change is beneficial? |
Sure @AJMitev. The system that I managed uses MIME types, not extensions as a routing mechanism for determining what post processing steps are required after a file is uploaded. Once a file is checked into the system, the old file name is replaced with an auto generated one (extension lost) the only way we can identify the file type is by MIME type. In addition to my use case, I believe this change is beneficial because MIME types act as an identifier by category. The hierarchal nature of the type, and subtype is well organized and useful. For example, I can write concise code like this: if (file.MediaType.StartsWith("image"))
{
await ProcessImageAsync(file, ct);
return;
}Rather than something like this: if (file.Name.EndsWith("png") || file.Name.EndsWith("jpg") || file.Name.EndsWith("webp") || file.Name.EndsWith("heic"))
{
await ProcessImageAsync(file, ct);
return;
}Simply, the MIME type serves as a standardized, hierarchal, content-based label for files that we find to be more useful then straight extensions. I believe this augmentation will be useful for other users of this library too. Thanks for taking the time to look at my PR :) |
|
In your example what is the type of "file"? Is it a stream, a byte array or other? Can't you just use the extension methods that the library currently provides? bool isImage = file.IsImage();
bool isPdf = file.Is<PortableDocumentFormat>(); |
The example provided represents a post-process step after the file has been uploaded, and identified (using the changes in this PR). |
|
I was actually hoping the mime-type would be part of the validation check for The validation should probably be part of FileTypeChecker.Web. |
|
@mentallabyrinth, Actually I am not sure how this change helps your case if you have a db record. In order to receive the @jerone Why you worry about the MIME type if the file signature is validated? |
|
@AJMitev here's a visualization for my previous comment: While the file is uploading we scan the first 4000 bytes using Reading the file bytes again from cloud storage to get the MIME Type is not efficient for our operation for the following reasons:
|
For the same reason the extension should be validated too; to confirm the Anyways, we thought the FileTypeChecker.Web packages validated the whole |
|
@mentallabyrinth Great explanation. @jerone I believe that MIME Types are overused when working with files because there are no easy ways to check the file's signature. I still don't get it why people care so much about the MIME type or file's extension when you can know the exact Great work creating your own attribute. The second goal of mine was to provide a way if you have some specific use case to be able to write your own custom attribute. Btw if you feel like it can match a common use case and can help others you can contribute it to the project or you can share it so I can include it in a future release. |

Based on this issue #42 I took a pass at MIME type inclusion because it's something needed for the application I'm development.
For the most part associations are analog. I used this resource https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/MIME_types/Common_types for many of the types. For file types not listed, I used web search and the
fileto verify mime types (developing on OSX so windows file type confirmation is out of reach). There's a few file types that are not straight forward:ExecutableandMp4to name a few.Executable has three associated mime types:
I selected for "application/x-msdownload" for executables as it's more widely accepted
MP4 has three associated mime types:
I selected "video/mp4" for MP4, but I suspect the right implementation is determining the magic bytes and creating separate types for each case e.g.,
Mp4AudioVideo,Mp4Audio, andMp4ApplicationBut before putting more effort into this endeavor I want to get your feedback and verify this implementation is the right path to travel.
Btw the https://github.com/AJMitev/FileTypeChecker/blob/master/CONTRIBUTING.md is 404 but did my best to follow the development patterns in the code base, including tests. Thank you for the excellent work on the library, and look forward to feedback.