...
 
Commits (5)
......@@ -5,7 +5,17 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [1.1.0] - 2020-03-21
## [1.2.0] - 2020-03-22
### Added
- Added two methods to retrieve all extensions linked to a specific mime type.
### Changed
- Library now targets .NET Standard 2.0, .NET Framework 4.6.1 and 4.7.2.
## [1.1.0] - 2020-03-22
### Added
......@@ -36,6 +46,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Initial release.
[1.2.0]: https://gitlab.com/pomma89/mime-types/-/compare/1.1.0...1.2.0
[1.1.0]: https://gitlab.com/pomma89/mime-types/-/compare/1.0.1...1.1.0
[1.0.1]: https://gitlab.com/pomma89/mime-types/-/compare/1.0.0...1.0.1
[1.0.0]: https://gitlab.com/pomma89/mime-types/-/tags/1.0.0
\ No newline at end of file
......@@ -39,6 +39,7 @@ In that case, the generic `application/octet-stream` mime type is returned.
```cs
Console.WriteLine("audio/wav -> " + MimeTypeMap.GetExtension("audio/wav")); // "audio/wav -> .wav"
Console.WriteLine("audio/wav -> " + MimeTypeMap.GetExtensionWithoutDot("audio/wav")); // "audio/wav -> wav"
```
Pass in a mime type and get an extension back. If the mime type is not registered, an error is thrown.
......@@ -46,6 +47,13 @@ Pass in a mime type and get an extension back. If the mime type is not registere
If the mime type is not found then an exception is thrown, unless `throwIfMissing` is set to false.
In that case, the generic `.bin` extension is returned.
To get all the extensions linked to a specific mime type:
```cs
MimeTypeMap.GetExtensions("image/jpeg"); // [".jpg", ".jpeg", ".jpe"]
MimeTypeMap.GetExtensionsWithoutDot("image/jpeg"); // ["jpg", "jpeg", "jpe"]
```
#### Mime type constants
```cs
......
......@@ -22,8 +22,11 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using StringMap = System.Collections.Generic.Dictionary<string, string>;
// WARNING: This file was auto generated by DotLiquid. DON'T modify it manually.
......@@ -35,7 +38,9 @@ namespace PommaLabs.MimeTypes
public static class MimeTypeMap
{
private const string DefaultExtension = ".bin";
private const string DefaultMimeType = "application/octet-stream";
private const string DefaultMimeType = "application/octet-stream";
private static readonly ReadOnlyCollection<string> s_noExtensions = new ReadOnlyCollection<string>(Array.Empty<string>());
// Retrieved from: {{ source_url }}
private static readonly StringMap s_extensionMap = new StringMap(StringComparer.OrdinalIgnoreCase)
......@@ -100,6 +105,48 @@ namespace PommaLabs.MimeTypes
: DefaultExtension;
}
/// <summary>
/// Retrieves the extensions linked to <paramref name="mimeType"/>. If
/// <paramref name="mimeType"/> is not mapped, then an empty collection will be returned.
/// </summary>
/// <param name="mimeType">Mime type.</param>
/// <returns>The extensions linked to <paramref name="mimeType"/>.</returns>
/// <remarks>
/// Retrieving all extensions linked to <paramref name="mimeType"/> is not the main goal of this library.
/// Therefore, the performance of this method is not as optimized as the others.
/// </remarks>
public static IReadOnlyCollection<string> GetExtensions(string mimeType)
{
if (!s_mimeTypeMap.TryGetValue(mimeType, out var mainExtension))
{
return s_noExtensions;
}
return s_extensionMap
.Where(kv => string.Equals(kv.Value, mimeType, StringComparison.OrdinalIgnoreCase))
.Select(kv => kv.Key)
.Union(new[] { mainExtension })
.Distinct()
.ToList();
}
/// <summary>
/// Retrieves the extensions linked to <paramref name="mimeType"/> and strips the leading
/// dot character. If <paramref name="mimeType"/> is not mapped, then an empty collection will be returned.
/// </summary>
/// <param name="mimeType">Mime type.</param>
/// <returns>The extensions linked to <paramref name="mimeType"/>.</returns>
/// <remarks>
/// Retrieving all extensions linked to <paramref name="mimeType"/> is not the main goal of this library.
/// Therefore, the performance of this method is not as optimized as the others.
/// </remarks>
public static IReadOnlyCollection<string> GetExtensionsWithoutDot(string mimeType)
{
return GetExtensions(mimeType)
.Select(e => e.Substring(1))
.ToList();
}
/// <summary>
/// Retrieves the extension linked to <paramref name="mimeType"/> and strips the leading
/// dot character. If <paramref name="mimeType"/> is not mapped, then an exception will be thrown.
......
......@@ -40,12 +40,14 @@ namespace PommaLabs.MimeTypes.UnitTests
public void GetExtension_ExistingMimeTypeShouldBeMatchedToCorrectExtension(string mimeType, string extension)
{
Assert.That(MimeTypeMap.GetExtension(mimeType), Is.EqualTo(extension));
Assert.That(MimeTypeMap.GetExtensions(mimeType), Contains.Item(extension));
}
[Test]
public void GetExtension_MissingMimeTypeShouldBeMatchedToBinExtensionWhenThrowExceptionIsFalse()
{
Assert.That(MimeTypeMap.GetExtension("image/bread", false), Is.EqualTo(DefaultExtension));
Assert.That(MimeTypeMap.GetExtensions("image/bread"), Is.Empty);
}
[Test]
......@@ -97,12 +99,14 @@ namespace PommaLabs.MimeTypes.UnitTests
public void GetExtensionWithoutDot_ExistingMimeTypeShouldBeMatchedToCorrectExtension(string mimeType, string extension)
{
Assert.That(MimeTypeMap.GetExtensionWithoutDot(mimeType), Is.EqualTo(extension.Substring(1)));
Assert.That(MimeTypeMap.GetExtensionsWithoutDot(mimeType), Contains.Item(extension.Substring(1)));
}
[Test]
public void GetExtensionWithoutDot_MissingMimeTypeShouldBeMatchedToBinExtensionWhenThrowExceptionIsFalse()
{
Assert.That(MimeTypeMap.GetExtensionWithoutDot("image/bread", false), Is.EqualTo(DefaultExtension.Substring(1)));
Assert.That(MimeTypeMap.GetExtensionsWithoutDot("image/bread"), Is.Empty);
}
[Test]
......
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\netfx.props" />
<PropertyGroup>
<OutputType>Exe</OutputType>
<OutputType>exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
......
......@@ -22,8 +22,11 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using StringMap = System.Collections.Generic.Dictionary<string, string>;
// WARNING: This file was auto generated by DotLiquid. DON'T modify it manually.
......@@ -35,7 +38,9 @@ namespace PommaLabs.MimeTypes
public static class MimeTypeMap
{
private const string DefaultExtension = ".bin";
private const string DefaultMimeType = "application/octet-stream";
private const string DefaultMimeType = "application/octet-stream";
private static readonly ReadOnlyCollection<string> s_noExtensions = new ReadOnlyCollection<string>(Array.Empty<string>());
// Retrieved from: https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types
private static readonly StringMap s_extensionMap = new StringMap(StringComparer.OrdinalIgnoreCase)
......@@ -1844,6 +1849,48 @@ namespace PommaLabs.MimeTypes
: DefaultExtension;
}
/// <summary>
/// Retrieves the extensions linked to <paramref name="mimeType"/>. If
/// <paramref name="mimeType"/> is not mapped, then an empty collection will be returned.
/// </summary>
/// <param name="mimeType">Mime type.</param>
/// <returns>The extensions linked to <paramref name="mimeType"/>.</returns>
/// <remarks>
/// Retrieving all extensions linked to <paramref name="mimeType"/> is not the main goal of this library.
/// Therefore, the performance of this method is not as optimized as the others.
/// </remarks>
public static IReadOnlyCollection<string> GetExtensions(string mimeType)
{
if (!s_mimeTypeMap.TryGetValue(mimeType, out var mainExtension))
{
return s_noExtensions;
}
return s_extensionMap
.Where(kv => string.Equals(kv.Value, mimeType, StringComparison.OrdinalIgnoreCase))
.Select(kv => kv.Key)
.Union(new[] { mainExtension })
.Distinct()
.ToList();
}
/// <summary>
/// Retrieves the extensions linked to <paramref name="mimeType"/> and strips the leading
/// dot character. If <paramref name="mimeType"/> is not mapped, then an empty collection will be returned.
/// </summary>
/// <param name="mimeType">Mime type.</param>
/// <returns>The extensions linked to <paramref name="mimeType"/>.</returns>
/// <remarks>
/// Retrieving all extensions linked to <paramref name="mimeType"/> is not the main goal of this library.
/// Therefore, the performance of this method is not as optimized as the others.
/// </remarks>
public static IReadOnlyCollection<string> GetExtensionsWithoutDot(string mimeType)
{
return GetExtensions(mimeType)
.Select(e => e.Substring(1))
.ToList();
}
/// <summary>
/// Retrieves the extension linked to <paramref name="mimeType"/> and strips the leading
/// dot character. If <paramref name="mimeType"/> is not mapped, then an exception will be thrown.
......
......@@ -3,7 +3,7 @@
<PropertyGroup>
<AssemblyTitle>MimeTypes - Dictionary of Apache's mime.types</AssemblyTitle>
<TargetFrameworks>netstandard2.1;net472</TargetFrameworks>
<TargetFrameworks>netstandard2.0;net461;net472</TargetFrameworks>
<Nullable>enable</Nullable>
</PropertyGroup>
......
......@@ -802,12 +802,14 @@ namespace PommaLabs.MimeTypes.UnitTests
public void GetExtension_ExistingMimeTypeShouldBeMatchedToCorrectExtension(string mimeType, string extension)
{
Assert.That(MimeTypeMap.GetExtension(mimeType), Is.EqualTo(extension));
Assert.That(MimeTypeMap.GetExtensions(mimeType), Contains.Item(extension));
}
[Test]
public void GetExtension_MissingMimeTypeShouldBeMatchedToBinExtensionWhenThrowExceptionIsFalse()
{
Assert.That(MimeTypeMap.GetExtension("image/bread", false), Is.EqualTo(DefaultExtension));
Assert.That(MimeTypeMap.GetExtensions("image/bread"), Is.Empty);
}
[Test]
......@@ -2383,12 +2385,14 @@ namespace PommaLabs.MimeTypes.UnitTests
public void GetExtensionWithoutDot_ExistingMimeTypeShouldBeMatchedToCorrectExtension(string mimeType, string extension)
{
Assert.That(MimeTypeMap.GetExtensionWithoutDot(mimeType), Is.EqualTo(extension.Substring(1)));
Assert.That(MimeTypeMap.GetExtensionsWithoutDot(mimeType), Contains.Item(extension.Substring(1)));
}
[Test]
public void GetExtensionWithoutDot_MissingMimeTypeShouldBeMatchedToBinExtensionWhenThrowExceptionIsFalse()
{
Assert.That(MimeTypeMap.GetExtensionWithoutDot("image/bread", false), Is.EqualTo(DefaultExtension.Substring(1)));
Assert.That(MimeTypeMap.GetExtensionsWithoutDot("image/bread"), Is.Empty);
}
[Test]
......
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\netfx.props" />
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFrameworks>netcoreapp3.1;net461;net472</TargetFrameworks>
<IsPackable>false</IsPackable>
</PropertyGroup>
......