Skip to content

Commit

Permalink
Added support for more formats
Browse files Browse the repository at this point in the history
  • Loading branch information
detaybey committed Nov 17, 2023
1 parent 79394f8 commit 5c47f49
Show file tree
Hide file tree
Showing 16 changed files with 122 additions and 25 deletions.
Binary file added MimeBank.Tests/Files/test_file_7z
Binary file not shown.
Binary file added MimeBank.Tests/Files/test_file_heic
Binary file not shown.
Binary file added MimeBank.Tests/Files/test_file_heif
Binary file not shown.
Binary file added MimeBank.Tests/Files/test_file_pdf
Binary file not shown.
Binary file added MimeBank.Tests/Files/test_file_rar
Binary file not shown.
Binary file added MimeBank.Tests/Files/test_file_zip
Binary file not shown.
21 changes: 19 additions & 2 deletions MimeBank.Tests/MimeBank.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;net6.0</TargetFrameworks>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<IsPackable>false</IsPackable>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>
Expand All @@ -18,7 +18,6 @@

<ItemGroup>
<Folder Include="Files" />
<Folder Include="Tests" />
</ItemGroup>

<ItemGroup>
Expand Down Expand Up @@ -68,6 +67,24 @@
<None Update="Files\test_file_wmv">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Files\test_file_7z">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Files\test_file_heic">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Files\test_file_heif">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Files\test_file_pdf">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Files\test_file_rar">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Files\test_file_zip">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
32 changes: 32 additions & 0 deletions MimeBank.Tests/Tests/CompressedFileTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using NUnit.Framework;
using System.IO;
using System.Threading.Tasks;

namespace MimeBank.Tests
{
[TestFixture]
public class CompressedFileTests : BaseTest
{
[Test]
[TestCase("7z")]
[TestCase("zip")]
[TestCase("rar")]
public void TestCompressedFile(string type)
{
var path = Path.Combine(AssemblyPath, "test_file_" + type);
var header = MimeChecker.GetFileHeader(path);
DoTests(header, FileType.Compressed, type);
}

[Test]
[TestCase("7z")]
[TestCase("zip")]
[TestCase("rar")]
public async Task TestCompressedFilesAsync(string type)
{
var path = Path.Combine(AssemblyPath, "test_file_" + type);
var header = await MimeChecker.GetFileHeaderAsync(path);
DoTests(header, FileType.Compressed, type);
}
}
}
28 changes: 28 additions & 0 deletions MimeBank.Tests/Tests/DocumentFileTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using NUnit.Framework;
using System.IO;
using System.Threading.Tasks;

namespace MimeBank.Tests
{
[TestFixture]
public class DocumentFileTests : BaseTest
{
[Test]
[TestCase("pdf")]
public void TestDocumentFiles(string type)
{
var path = Path.Combine(AssemblyPath, "test_file_" + type);
var header = MimeChecker.GetFileHeader(path);
DoTests(header, FileType.Document, type);
}

[Test]
[TestCase("pdf")]
public async Task TestDocumentFilesAsync(string type)
{
var path = Path.Combine(AssemblyPath, "test_file_" + type);
var header = await MimeChecker.GetFileHeaderAsync(path);
DoTests(header, FileType.Document, type);
}
}
}
10 changes: 7 additions & 3 deletions MimeBank.Tests/Tests/ImageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,22 @@ public class ImageTests : BaseTest
[TestCase("jpg")]
[TestCase("png")]
[TestCase("gif")]
public void TestImageFile(string type)
[TestCase("heic")]
[TestCase("heif")]
public void TestImageFiles(string type)
{
var path = Path.Combine(AssemblyPath, "test_file_" + type);
var header = MimeChecker.GetFileHeader(path);
DoTests(header, FileType.Image, type);
}

[Test]
[TestCase("jpg")]
[TestCase("png")]
[TestCase("gif")]
public async Task TestImageFileAsync(string type)
[TestCase("heic")]
[TestCase("heif")]
public async Task TestImageFilesAsync(string type)
{
var path = Path.Combine(AssemblyPath, "test_file_" + type);
var header = await MimeChecker.GetFileHeaderAsync(path);
Expand Down
24 changes: 12 additions & 12 deletions MimeBank/FileHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ namespace MimeBank
/// </summary>
public class FileHeader
{
private const int skipValue = -1;
private const char skipChar = '?';
private const string skipMarker = "??";
private const int SkipValue = -1;
private const char SkipChar = '?';
private const string SkipMarker = "??";

/// <summary>
/// The enum type of the file.
Expand Down Expand Up @@ -46,10 +46,10 @@ public FileHeader(FileType type, string extension, string header)
private static int[] ParsePattern(string header) =>
header.Split(' ')
.Select(part =>
part == skipMarker
? skipValue
: part[0] == skipChar || part[1] == skipChar
? -1 * Convert.ToInt32(part.Replace(skipChar, '0'), 16)
part == SkipMarker
? SkipValue
: part[0] == SkipChar || part[1] == SkipChar
? -1 * Convert.ToInt32(part.Replace(SkipChar, '0'), 16)
: Convert.ToInt32(part, 16))
.ToArray();

Expand All @@ -60,23 +60,23 @@ private static int[] ParsePattern(string header) =>
/// <returns>true if the type is correct</returns>
public bool Check(byte[] buffer)
{
int headerLength = Header.Length;
var headerLength = Header.Length;
if (headerLength > buffer.Length)
{
return false;
}

for (int i = 0; i < headerLength; i++)
for (var i = 0; i < headerLength; i++)
{
int headerValue = Header[i];
if (headerValue == skipValue)
var headerValue = Header[i];
if (headerValue == SkipValue)
{
continue;
}

if (headerValue < 0)
{
byte b = (byte)(-1 * headerValue);
var b = (byte)(-1 * headerValue);
if ((buffer[i] & b) != b)
{
return false;
Expand Down
4 changes: 4 additions & 0 deletions MimeBank/FileType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,9 @@ public enum FileType
Video,
Audio,
Swf,
Compressed,
Document,
Executable,
Database
}
}
12 changes: 12 additions & 0 deletions MimeBank/HeaderData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public static class HeaderData
new FileHeader(FileType.Image, "tif", "49 49 2A 00"),
new FileHeader(FileType.Image, "tif", "4D 4D 00 2A"),
new FileHeader(FileType.Image, "tif", "4D 4D 00 2B"),
new FileHeader(FileType.Image, "heic", "00 00 00 18 66 74 79 70 6D"),
new FileHeader(FileType.Image, "heif", "00 00 00 18 66 74 79 70 68 65 69"),
new FileHeader(FileType.Image, "webp", "52 49 46 46 ?? ?? ?? ?? 57 45 42 50"),

new FileHeader(FileType.Swf, "swf", "46 57 53"), //FWS - uncompressed SWF
new FileHeader(FileType.Swf, "swf", "43 57 53"), //CWS - compressed SWF by using the ZLIB open standard
Expand Down Expand Up @@ -57,6 +60,15 @@ public static class HeaderData
new FileHeader(FileType.Video, "wmv", "30 26 B2 75 8E 66 CF 11 A6 D9 00 AA 00 62 CE 6C"),
new FileHeader(FileType.Video, "avi", "52 49 46 46 ?? ?? ?? ?? 41 56 49 20 4C 49 53 54"),
new FileHeader(FileType.Video, "mkv", "1A 45 DF A3"),

new FileHeader(FileType.Compressed, "rar", "52 61 72 21 1A 07 00"),
new FileHeader(FileType.Compressed, "7z", "37 7A BC AF 27 1C"),
new FileHeader(FileType.Compressed, "zip", "50 4B 03 04"),

new FileHeader(FileType.Document, "pdf", "25 50 44 46"),
new FileHeader(FileType.Executable, "exe", "4D 5A"),
new FileHeader(FileType.Database, "sqlite", "53 51 4C 69 74 65 20 66 6F 72 6D 61 74 20 33 00"),

};
}
}
2 changes: 1 addition & 1 deletion MimeBank/MimeBank.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.1;net6.0</TargetFrameworks>
<TargetFrameworks>net6.0;netstandard2.1;net7.0</TargetFrameworks>
<IsPackable>true</IsPackable>
<Version>2.0.0</Version>
<AssemblyVersion>$(Version)</AssemblyVersion>
Expand Down
8 changes: 4 additions & 4 deletions MimeBank/MimeBank.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
<package >
<metadata>
<id>MimeBank</id>
<version>1.0.2</version>
<authors>Umut Oguz Çelenli</authors>
<owners>Umut Oguz Çelenli</owners>
<version>1.0.3</version>
<authors>Umut Oguz Çelenli</authors>
<owners>Umut Oguz Çelenli</owners>
<projectUrl>https://github.com/detaybey/MimeBank</projectUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>MimeBank is a file-type checker for dotnet.</description>
<releaseNotes>Added MKV support (thanks ScottMB), Updated tests, Added a static check() helper method for ease of use.</releaseNotes>
<releaseNotes>Added support for more formats</releaseNotes>
<copyright>Copyright 2016</copyright>
<tags>Mime File Check Header</tags>
<dependencies>
Expand Down
6 changes: 3 additions & 3 deletions MimeBank/MimeChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ public class MimeChecker

public async Task<FileHeader> GetFileHeaderAsync(string file)
{
using var stream = File.OpenRead(file);
await using var stream = File.OpenRead(file);
return await GetFileHeaderAsync(stream);
}

public async Task<FileHeader> GetFileHeaderAsync(Stream stream)
{
var buffer = new byte[HeaderData.MaxBufferSize];
await stream.ReadAsync(buffer, 0, HeaderData.MaxBufferSize);
var readAsync = await stream.ReadAsync(buffer, 0, HeaderData.MaxBufferSize);

return HeaderData.Items.FirstOrDefault(mime => mime.Check(buffer));
}
Expand All @@ -38,7 +38,7 @@ public FileHeader GetFileHeader(string file)
public FileHeader GetFileHeader(Stream stream)
{
var buffer = new byte[HeaderData.MaxBufferSize];
stream.Read(buffer, 0, HeaderData.MaxBufferSize);
var read = stream.Read(buffer, 0, HeaderData.MaxBufferSize);

return HeaderData.Items.FirstOrDefault(mime => mime.Check(buffer));
}
Expand Down

0 comments on commit 5c47f49

Please sign in to comment.