...
 
Commits (3)
......@@ -3,6 +3,16 @@ stages:
- publish
- deploy
apache-test:
stage: test
image: pomma89/dotnet:3-sdk
script:
- cp src/PommaLabs.MimeTypes/MimeTypeMap.cs MimeTypeMap.cs.bak
- cp test/PommaLabs.MimeTypes.UnitTests/MimeTypeMapTests.cs MimeTypeMapTests.cs.bak
- pwsh generate-code.ps1
- diff src/PommaLabs.MimeTypes/MimeTypeMap.cs MimeTypeMap.cs.bak
- diff test/PommaLabs.MimeTypes.UnitTests/MimeTypeMapTests.cs MimeTypeMapTests.cs.bak
format-test:
stage: test
image: pomma89/dotnet-mono:dotnet-3-mono-6-sdk
......
......@@ -5,19 +5,29 @@ 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
### Added
- All mime types can be used as constants (e.g. `MimeTypeMap.Image.Png`).
### Changed
- Library is now 99% compatible with `MediaTypeMap.Core` package (see README for details).
## [1.0.1] - 2020-03-15
### Added
- Added "GetExtensionWithoutDot" utility method.
- Added `GetExtensionWithoutDot` utility method.
### Changed
- Renamed "fileName" parameter into "filePath".
- Renamed `fileName` parameter into `filePath`.
### Fixed
- Default "bin" extension did not contain the dot character.
- Default `bin` extension did not contain the dot character.
## [1.0.0] - 2020-03-14
......@@ -25,5 +35,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Initial release.
[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
......@@ -3,9 +3,21 @@
*Simple dictionary that provides a few methods to lookup by mime type/extension,
generated from [Apache's mime.types](https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types).*
## Summary
- Build status on [GitLab](https://gitlab.com/pomma89/mime-types/pipelines): [![pipeline status](https://gitlab.com/pomma89/mime-types/badges/master/pipeline.svg)](https://gitlab.com/pomma89/mime-types/commits/master)
- [DocFX](hhttps://dotnet.github.io/docfx/) generated API documentation: [https://pomma89.gitlab.io/mime-types/](https://pomma89.gitlab.io/mime-types/)
- [NuGet](https://www.nuget.org) package(s):
+ [PommaLabs.MimeTypes](https://www.nuget.org/packages/PommaLabs.MimeTypes/), main package.
## Introduction
### Compatibility
This library tries to be as much compatible as possible with [MediaTypeMap.Core](https://www.nuget.org/packages/MediaTypeMap.Core/) package,
with following caveats:
- Namespace is different, in order to avoid name clashes.
- An empty string will not be returned to unknown inputs when `throwIfMissing` is false.
+ Missing mime types will receive `.bin` as default extension.
+ Missing extensions will receive `application/octet-stream` as default mime type.
......@@ -21,13 +33,6 @@ with following caveats:
+ `audio/x-pn-realaudio-plugin` now returns `rmp` instead of `rpm`.
That seemed a typo, since `rpm` stands for Red Hat Package Manager.
## Summary
- Build status on [GitLab](https://gitlab.com/pomma89/mime-types/pipelines): [![pipeline status](https://gitlab.com/pomma89/mime-types/badges/master/pipeline.svg)](https://gitlab.com/pomma89/mime-types/commits/master)
- [DocFX](hhttps://dotnet.github.io/docfx/) generated API documentation: [https://pomma89.gitlab.io/mime-types/](https://pomma89.gitlab.io/mime-types/)
- [NuGet](https://www.nuget.org) package(s):
+ [PommaLabs.MimeTypes](https://www.nuget.org/packages/PommaLabs.MimeTypes/), main package.
## About this repository and its maintainer
Everything done on this repository is freely offered on the terms of the project license. You are free to do everything you want with the code and its related files, as long as you respect the license and use common sense while doing it :-)
......
cd ./src/PommaLabs.MimeTypes.Generator
cd src/PommaLabs.MimeTypes.Generator
dotnet run -- src | Out-File -Encoding "UTF8" ..\..\src\PommaLabs.MimeTypes\MimeTypeMap.cs
dotnet run -- test | Out-File -Encoding "UTF8" ..\..\test\PommaLabs.MimeTypes.UnitTests\MimeTypeMapTests.cs
cd ../..
\ No newline at end of file
......@@ -22,10 +22,11 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using StringMap = System.Collections.Generic.Dictionary<string, string>;
// WARNING: This file was auto generated by DotLiquid at {{ utc_now }}. DON'T modify it manually.
// WARNING: This file was auto generated by DotLiquid. DON'T modify it manually.
namespace PommaLabs.MimeTypes
{
/// <summary>
......@@ -161,5 +162,23 @@ namespace PommaLabs.MimeTypes
? throw new ArgumentException($"Requested extension is not mapped: {extension}")
: DefaultMimeType;
}
{% for gkv in grouped_mime_types -%}
/// <summary>
/// Mime type constants.
/// </summary>
[SuppressMessage("Design", "CA1034:Nested types should not be visible", Justification = "This is a class with constant fields only")]
public static class {{ gkv.Key }}
{
{% for kv in gkv.Value -%}
/// <summary>
/// "{{ kv.Value }}" mime type constant.
/// </summary>
public const string {{ kv.Key }} = "{{ kv.Value }}";
{% endfor -%}
}
{% endfor -%}
}
}
\ No newline at end of file
......@@ -24,7 +24,7 @@
using System;
using NUnit.Framework;
// WARNING: This file was auto generated by DotLiquid at {{ utc_now }}. DON'T modify it manually.
// WARNING: This file was auto generated by DotLiquid. DON'T modify it manually.
namespace PommaLabs.MimeTypes.UnitTests
{
public sealed class MimeTypeMapTests
......
......@@ -28,6 +28,7 @@ using System.Net;
using System.Text.RegularExpressions;
using DotLiquid;
using StringMap = System.Collections.Generic.SortedList<string, string>;
using StringMultiMap = System.Collections.Generic.SortedList<string, System.Collections.Generic.SortedList<string, string>>;
namespace PommaLabs.MimeTypeMap.Generator
{
......@@ -52,12 +53,12 @@ namespace PommaLabs.MimeTypeMap.Generator
return 1;
}
var (extensions, mimeTypes) = GetExtensionsAndMimeTypes();
var (extensions, mimeTypes, groupedMimeTypes) = GetExtensionsAndMimeTypes();
switch (args[0])
{
case "src":
Console.WriteLine(GenerateMimeTypeMap(extensions, mimeTypes));
Console.WriteLine(GenerateMimeTypeMap(extensions, mimeTypes, groupedMimeTypes));
return 0;
case "test":
......@@ -69,17 +70,17 @@ namespace PommaLabs.MimeTypeMap.Generator
}
}
private static string GenerateMimeTypeMap(StringMap extensions, StringMap mimeTypes)
private static string GenerateMimeTypeMap(StringMap extensions, StringMap mimeTypes, StringMultiMap groupedMimeTypes)
{
var templateContents = File.ReadAllText("MimeTypeMap.liquid");
var template = Template.Parse(templateContents);
return template.Render(Hash.FromAnonymousObject(new
{
utc_now = DateTime.UtcNow,
source_url = ApacheMimeTypesUrl,
extensions,
mime_types = mimeTypes,
grouped_mime_types = groupedMimeTypes,
}));
}
......@@ -90,19 +91,20 @@ namespace PommaLabs.MimeTypeMap.Generator
return template.Render(Hash.FromAnonymousObject(new
{
utc_now = DateTime.UtcNow,
extensions,
mime_types = mimeTypes,
}));
}
private static (StringMap extensions, StringMap mimeTypes) GetExtensionsAndMimeTypes()
private static (StringMap extensions, StringMap mimeTypes, StringMultiMap groupedMimeTypes) GetExtensionsAndMimeTypes()
{
using var webClient = new WebClient();
var response = webClient.DownloadString(new Uri(ApacheMimeTypesUrl));
var extensions = new StringMap(StringComparer.OrdinalIgnoreCase);
var mimeTypes = new StringMap(StringComparer.OrdinalIgnoreCase);
var groupedMimeTypes = new StringMultiMap(StringComparer.OrdinalIgnoreCase);
using var reader = new StringReader(response);
string line;
......@@ -135,6 +137,20 @@ namespace PommaLabs.MimeTypeMap.Generator
first = false;
}
}
static string Capitalize(string w) => char.IsDigit(w[0]) ? $"D{w}" : $"{char.ToUpperInvariant(w[0])}{w[1..]}";
static string ToCSharpName(string w) => string.Join("", w.Split('-', '+', '.').Select(x => Capitalize(x)));
var splitMimeType = mimeType.Split('/');
var type = ToCSharpName(splitMimeType[0]);
var subtype = ToCSharpName(splitMimeType[1]);
if (!groupedMimeTypes.TryGetValue(type, out var subtypes))
{
groupedMimeTypes[type] = subtypes = new StringMap(StringComparer.OrdinalIgnoreCase);
}
subtypes.Add(subtype, mimeType);
}
// Following overrides ensures best compatibility with MediaTypeMap.Core results.
......@@ -154,7 +170,7 @@ namespace PommaLabs.MimeTypeMap.Generator
mimeTypes["video/mpeg"] = "mpg";
mimeTypes["video/quicktime"] = "mov";
return (extensions, mimeTypes);
return (extensions, mimeTypes, groupedMimeTypes);
}
}
}
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -24,7 +24,7 @@
using System;
using NUnit.Framework;
// WARNING: This file was auto generated by DotLiquid at 21/03/2020 14:16:20. DON'T modify it manually.
// WARNING: This file was auto generated by DotLiquid. DON'T modify it manually.
namespace PommaLabs.MimeTypes.UnitTests
{
public sealed class MimeTypeMapTests
......@@ -3400,3 +3400,4 @@ namespace PommaLabs.MimeTypes.UnitTests
}
}
}