samuelneff / mimetypemap Goto Github PK
View Code? Open in Web Editor NEWProvides a huge dictionary of file extensions to mime types.
License: MIT License
Provides a huge dictionary of file extensions to mime types.
License: MIT License
It should be "application/vnd.oasis.opendocument.spreadsheet"
Commit ace145c added some breaking changes and also introduced some ambiguous behavior.
GetMimeType("")
previously returned "application/octet-stream" but now throws ArgumentNullException
.
GetMimeType
used to take a file extension as input but now takes a file name or file extension. This can lead to some cases where it is not clear what the expected output should be.
Consider for example the string "gif". This could be a valid filename for a file without file extension and should in that case return "application/octet-stream". But it could also be interpreted as the file extension ".gif". To remove this ambiguity I suggest reverting to the previous behavior of expecting a file extension only.
Another option could be to change GetMimeType
to expect a path string instead and then use System.IO.Path.GetExtension(path)
to get the file extension before looking in the internal dictionary.
public static string GetMimeType(string path) {
if (path == null) {
throw new ArgumentNullException(nameof(path));
}
if (path != string.Empty && Path.GetFileName(path) == string.Empty) {
throw new ArgumentException($"{path} is not a file path", nameof(path));
}
var ext = Path.GetExtension(path);
return MimeTypeMap.TryGetMimeType(ext, out var result) ? result : DefaultMimeType;
}
The following examples would then all be valid input:
GetMimeType(null)
=> throws ArgumentNullExceptionGetMimeType("")
=> "application/octet-stream"GetMimeType(".")
=> "application/octet-stream"GetMimeType("gif")
=> "application/octet-stream"GetMimeType(".gif")
=> "image/gif"GetMimeType("test.gif")
=> "image/gif"GetMimeType("/test/image.gif")
=> "image/gif"GetMimeType(@"C:\test")
=> "application/octet-stream"GetMimeType(@"C:\test\")
=> throws ArgumentExceptionGetMimeType(@"C:\test\image.gif")
=> "image/gif"Would it also be possible to have your library as strong name nuget package?
Please consider adding .geojson, "application/geo+json"
I tried to upload a zip file using my .net webapi but failed due to this. I made a util method to help me uploading files:
private string GetExtensionFromFileHeader(HttpContent file)
{
return MimeTypeMap.GetExtension(file.Headers.ContentType.ToString());
}
But it's crashing when I upload zip files (works fine with everything else I tried so far from images to pdfs and words)
Am I missing something?
See https://www.w3.org/TR/PNG/#A-Media-type
It is recommended that implementations also recognize the media type "image/x-png".
Are you still maintaining this project? It hasn't been updated since 2015. Or maybe you can transfer it to a new maintainer?
It would be nice to have the string values (IE...application/json) of the content type setup in a static class. This would be handy to have for validating without having magic strings throughout the code.
Another method should be added to handle these cases.
References:
http://voice.firefallpro.com/2012/03/html5-audio-video-mime-types.html
http://www.iana.org/assignments/media-types/media-types.xhtml
Hello,
thank you for providing this useful library. Since I just ran into an issue using m4b
files, I would like to ask, which sources you used for the mapping?
The result for m4b
files is audio/m4b
, but AFAIK this type does not exist / is not specified. The "official" list of registered Mimetypes linked to RFCs / Persons is available at:
https://www.iana.org/assignments/media-types/media-types.xhtml
These are without extension mapping, since the extension of a file can only be a hint for the real mimetype, although it maybe a good hint ;-)
The best resource I found WITH extension mapping was:
http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types
which also lacks m4b
:-/
Another good resource might be the magic.mgc
which is common on Unix:
https://man7.org/linux/man-pages/man4/magic.4.html
Searching for m4b
in the types definition, it is referenced as audio/x-m4b
.
# grep 'm4b' /usr/share/mime/types
audio/x-m4b
# file -b --mime-type sample.m4b
audio/x-m4b
So it seems that in this library / type definition every custom mimetype, that is not referenced in the official listing, is prefixed with x-
.
The magic.mgc
contains a binary analysis instruction set, that does not rely on the extension, but this may also be incorrect (or at least unwanted), since the result for some m4b
files is video/mp4
, which is technically correct but assuming the extension is valid, the wanted result is audio/mp4
or better audio/x-m4b
, even if the file contains the binary signature of a video.
To get to the bottom line:
audio/m4b
, which may be wrong in your library/x-
(e.g. audio/x-m4b
)m4b
result to audio/x-m4b
or is this behaviour intended?Hello!
Thanks for you library, I found it useful. But the only problem it's not published to Nuget so I can't integrate it into my app easily. What do you think about publishing packages using GitHub action? Here is link: https://github.com/marketplace/actions/publish-nuget All you need is to login on https://www.nuget.org/ and create key, add yaml file with configuration to your solution. If it's not fine for you I can upload latest package to Nuget but it will be manual approach.
missing "audio/mp3" type.
repeat "application/acad" type.
Hi, I am having problems with extension "application/vnd.ms-outlook". I reviewed class MimeTypeMap.cs and it is there but for some reason I am getting an exception "Requested mime type is not registered: application/vnd.ms-outlook".
I don't understand why you didn't simply return mappings and have written this code?
`var cache = mappings.ToList(); // need ToList() to avoid modifying while still enumerating
foreach (var mapping in cache)
{
if (!mappings.ContainsKey(mapping.Value))
{
mappings.Add(mapping.Value, mapping.Key);
}
}
return mappings;`
When this is useful?
Hi team, I would like to know how can I fix this warning, as the functionality is working fine.
.rmvb application/vnd.rn-realmedia-vbr
Some of the items in the list are in mixed case, as far as I can tell TryGetValue is case sensitive so unless the extension is presented in exactly the right case it will fail. Was this intentional?
Unusable together with signed assemblies, resulting in
Could not load file or assembly 'MimeTypeMap, Version=2.1.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. A strongly-named assembly is required. (Exception from HRESULT: 0x80131044)
When referencing the NUGet package in a .Net Core 3.1 project, the NU1701 warning is recieved:
warning NU1701: Package 'MediaTypeMap 2.1.0' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8' instead of the project target framework '.NETCoreApp,Version=v3.1'. This package may not be fully compatible with your project.
This class works without issue; is it possible to have the NUGet package ”officially” support .net core 3.1?
I was browsing around and found this rather interesting MIME ContentType provider via the Windows Registry:
private static string _GetContentTypeRegistry(string fileName)
{
var extension = Path.GetExtension(fileName);
if (String.IsNullOrWhiteSpace(extension))
{
return null;
}
var registryKey = Registry.ClassesRoot.OpenSubKey(extension);
if (registryKey == null)
{
return null;
}
var value = registryKey.GetValue("Content Type") as string;
return String.IsNullOrWhiteSpace(value) ? null : value;
}
It has already been stated before this method is system-specific and unreliable, but I feel as though it would be a good fallback alternative should something not be found in your MIMEType database.
Splitting the MIME Mapping into your original, this, and a validator which simply contains logic that if the Winodws Registry method returns null or "application/octlet-stream" (invalid), grab it from your Mappings database, seems like a more robust solution.
.wasm (Web Assembly) files have the mime type application/wasm
. This isn't in MimeTypeMap, but it should be.
Is it possible to build this package so it's useable with UAP Apps (Windows 10 Universal Apps)?
.zip should be application/zip , not application/x-zip-compressed according to IANA registered types. See here (http://www.iana.org/assignments/media-types/application/zip) and here (http://www.iana.org/assignments/media-types/media-types.xhtml)
Hello there!
It seems that 3 days ago, the NuGet package was updated. As you stated in #113, you also included a TryGetExtension
method in your codebase. Unfortunately, it does not seem to be anywhere on Github nor in the package version 1.0.12.
Here are a few screenshots:
As you can see, it is the latest version available and there are no methods added nor XML documentation (even though TryGetMimeType
was added with PR #114).
Here I cannot find the TryGetExtension
method you were talking about in issue #113 .
I tried getting the new .NET Core 2.0 version of this NuGet package but it doesn't show up in nuget.org even if I check include pre-release. How do I get it?
It seems like the package on nuget is missing almost two years of work, are there any plans to update the package, or is the hope that people will just copy+past the code?
The library should support Core CLR and ported to .Net Platform 5.4.
Can you please bump the nuget package. Several commits since 5/25/2021 are not included
with mimeTypes i need. Many thanks for your time on this.
Seems your pipeline is brokem as the Nuget release 1.0.7 doesn't contain any compiled libraries, only the source code - as you can see via NugetPackageExplorer.
Please update the nuget-package to catch up with the latest version, including support for .NET Core
Good afternoon!
I came across your repository while searching for a solution to make sure that the filename of a file matches its content-type.
I was wondering if it would be possible to implement TryGetMimeType
& GetMimeType
using your private _mappings
variable.
You can find below the possible signature of such methods:
public static bool TryGetMimeType(string? fileName, [NotNullWhen(true)] out string? mimeType);
public static string GetMimeType(string filename);
It is a well-known type from what I get but MimeTypeMap does not recognise it
Today I encounter a strange behavior about matching mp3 mime type with browser return ContentType
and I have search an article: Which mime type should I use for mp3
Found out that each browser return different ContentType
if (!audio.ContentType.Equals(MimeTypeMap.GetMimeType(".mp3")))
{
ViewBag.alert = "Incorrect,only accept *.mp3";
return View(data);
}
using chrome will not be able to upload mp3 file
hi @samuelneff -
I am the new maintainer of:
https://www.nuget.org/packages/Mime-Detective/
https://github.com/MediatedCommunications/Mime-Detective
and have recently published an update expanding to over 14,000 mime signatures. Are you open to combining/joining efforts?
Hello, Is there a roadmap for providing support for DotNet Core?
Should be application/javascript.
http://www.ietf.org/rfc/rfc4329.txt
Using Microsoft's urlmon.dll to identify mime types will give you 'application/x-msdownload' for .exe files in Visual Studio 2019, but according to your mapping .exe files will give us 'application/octet-stream'.
Saw your post http://stackoverflow.com/questions/1029740/get-mime-type-from-filename-extension/14108040#14108040.
Getting your single file as Source from NuGet would be vastly better than as a dll. I recently starting providing NuGet packages for source files instead of dll files and it is quite useful.
http://www.rhyous.com/2016/03/03/nuget-for-source-using-add-as-link-1
Any chance of a Nuget update?
This content type is not recognized by MimeTypeMap
This is not "mandatory" but would lead to a more friendly approach for a new user that comes across your package. Obviously, if the data is not found, you should never return a value (even if an empty string) as it may lead to strange behavior or misunderstanding.
I would highly recommend to introduce this breaking change in your library:
public static string GetExtension(string mimeType, bool throwErrorIfNotFound = true)
{
...
- return string.Empty;
+ return null;
}
...I would love to see one for this!
Currently assembly could not be used with strong-typed libraries. It's easy to fix it, but it's on you.
Hmm, I found that similar request is open from sept 2015. It's 5 minutes to do, just create an snk file and add it in project properties.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.