Skip to content

Commit 1b90ca0

Browse files
committed
add text/event-stream
1 parent 9b49122 commit 1b90ca0

File tree

5 files changed

+52
-4
lines changed

5 files changed

+52
-4
lines changed

Directory.Build.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
<RepositoryUrl>https://github.com/managedcode/MimeTypes</RepositoryUrl>
1818
<PackageProjectUrl>https://github.com/managedcode/MimeTypes</PackageProjectUrl>
1919
<Product>Managed Code - MimeTypes</Product>
20-
<Version>1.0.2</Version>
21-
<PackageVersion>1.0.2</PackageVersion>
20+
<Version>1.0.3</Version>
21+
<PackageVersion>1.0.3</PackageVersion>
2222

2323
</PropertyGroup>
2424
<PropertyGroup Condition="'$(GITHUB_ACTIONS)' == 'true'">

ManagedCode.MimeTypes.Tests/GeneratorTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public class GeneratorTests
88
[Fact]
99
public void ExtensionsTest()
1010
{
11+
MimeHelper.GetMimeType("somefile.pdf").ShouldBe("application/pdf");
1112
MimeHelper.GetMimeType("pdf").ShouldBe("application/pdf");
1213
MimeHelper.GetMimeType(".gz").ShouldBe("application/gzip");
1314
MimeHelper.GetMimeType("word.docx").ShouldBe("application/vnd.openxmlformats-officedocument.wordprocessingml.document");

ManagedCode.MimeTypes/ManagedCode.MimeTypes.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<Nullable>enable</Nullable>
55
<LangVersion>13</LangVersion>
6-
<TargetFrameworks>netstandard2.0;net6.0;net7.0;net8.0;net9.0</TargetFrameworks>
6+
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
77
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
88
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)Generated</CompilerGeneratedFilesOutputPath>
99
</PropertyGroup>

ManagedCode.MimeTypes/MimeHelper.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,52 @@ public static string GetMimeType(string extension)
5050
private static readonly Regex CalendarPattern = new(@"^(?:text/calendar|application/ics)$", RegexOptions.Compiled | RegexOptions.IgnoreCase);
5151
private static readonly Regex EmailPattern = new(@"^(?:message/(?:rfc822|global)|application/(?:mbox|x-msmessage))", RegexOptions.Compiled | RegexOptions.IgnoreCase);
5252

53+
private static readonly Dictionary<byte[], string> MimeTypesForContent = new()
54+
{
55+
{ new byte[] { 0x25, 0x50, 0x44, 0x46 }, "application/pdf" }, // PDF
56+
{ new byte[] { 0xFF, 0xD8, 0xFF }, "image/jpeg" }, // JPEG
57+
{ new byte[] { 0x89, 0x50, 0x4E, 0x47 }, "image/png" }, // PNG
58+
{ new byte[] { 0x47, 0x49, 0x46, 0x38 }, "image/gif" }, // GIF
59+
{ new byte[] { 0x50, 0x4B, 0x03, 0x04 }, "application/zip" }, // ZIP
60+
{ new byte[] { 0x1F, 0x8B }, "application/gzip" } // GZIP
61+
};
62+
63+
public static string GetMimeTypeByContent(string filePath)
64+
{
65+
byte[] fileHeader = new byte[4];
66+
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
67+
{
68+
fs.ReadExactly(fileHeader, 0, fileHeader.Length);
69+
}
70+
71+
foreach (var mime in MimeTypes)
72+
{
73+
if (fileHeader.AsSpan().Slice(0, mime.Key.Length).SequenceEqual(mime.Key))
74+
{
75+
return mime.Value;
76+
}
77+
}
78+
79+
return "application/octet-stream"; // Default MIME type
80+
}
81+
82+
public static string GetMimeTypeByContent(Stream fileStream)
83+
{
84+
byte[] fileHeader = new byte[4];
85+
fileStream.ReadExactly(fileHeader, 0, fileHeader.Length);
86+
87+
foreach (var mime in MimeTypes)
88+
{
89+
if (fileHeader.AsSpan().Slice(0, mime.Key.Length).SequenceEqual(mime.Key))
90+
{
91+
return mime.Value;
92+
}
93+
}
94+
95+
return "application/octet-stream"; // Default MIME type
96+
}
97+
98+
5399
public static MimeTypeCategory GetMimeCategory(string mime)
54100
{
55101
if (string.IsNullOrWhiteSpace(mime))

ManagedCode.MimeTypes/mimeTypes.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1173,5 +1173,6 @@
11731173
"zip": "application/zip",
11741174
"zir": "application/vnd.zul",
11751175
"zirz": "application/vnd.zul",
1176-
"zmm": "application/vnd.handheld-entertainment+xml"
1176+
"zmm": "application/vnd.handheld-entertainment+xml",
1177+
"event_stream":"text/event-stream"
11771178
}

0 commit comments

Comments
 (0)