Skip to content

Commit 379e7e7

Browse files
committed
Add detectors for mp4/m4v. Fix multiple choice checking and add skipping
1 parent a8b629c commit 379e7e7

File tree

7 files changed

+64
-8
lines changed

7 files changed

+64
-8
lines changed

FileTypeChecker.Tests/FIleTypeValidatorTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ public void GetFileType_ShouldReturnFileExtension(string filePath, string expect
8787
[TestCase("./files/test.7z", "7-Zip File Format")]
8888
[TestCase("./files/test.bz2", "BZIP2 file")]
8989
[TestCase("./files/test.gz", "GZIP compressed file")]
90+
[TestCase("./files/test.mp4", "MP4 file")]
9091
public void GetFileType_ShouldReturnFileName(string filePath, string expectedFileTypeName)
9192
{
9293
using var fileStream = File.OpenRead(filePath);

FileTypeChecker.Tests/FileTypeChecker.Tests.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@
6767
<None Update="files\test.bz2">
6868
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
6969
</None>
70+
<None Update="files\test.mp4">
71+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
72+
</None>
7073
</ItemGroup>
71-
7274
</Project>

FileTypeChecker.Tests/files/test.mp4

946 KB
Binary file not shown.

FileTypeChecker/Abstracts/FileType.cs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,23 @@ public abstract class FileType : IFileType
1313
private string extension;
1414
private byte[][] bytes;
1515

16-
public FileType(string name, string extension, byte[] magicBytes)
16+
public FileType(string name, string extension, byte[] magicBytes, int skipBytes = 0)
1717
{
1818
this.Name = name;
1919
this.Extension = extension;
20-
this.Bytes = new byte[][] { magicBytes };
20+
this.Bytes = new[] { magicBytes };
21+
SkipBytes = skipBytes;
2122
}
2223

23-
public FileType(string name, string extension, byte[][] magicBytesJaggedArray)
24+
public FileType(string name, string extension, byte[][] magicBytesJaggedArray, int skipBytes = 0)
2425
{
2526
this.Name = name;
2627
this.Extension = extension;
2728
this.Bytes = magicBytesJaggedArray;
29+
SkipBytes = skipBytes;
2830
}
31+
32+
public int SkipBytes { get; }
2933

3034
/// <inheritdoc />
3135
public string Name
@@ -79,11 +83,27 @@ public bool DoesMatchWith(Stream stream, bool resetPosition = true)
7983
stream.Position = 0;
8084
}
8185

82-
return CompareBytes(stream);
86+
var buffer = new byte[20];
87+
stream.Read(buffer, 0, buffer.Length);
88+
return CompareBytes(buffer);
89+
}
90+
91+
public bool DoesMatchWith(byte[] buffer)
92+
{
93+
return CompareBytes(buffer);
8394
}
8495

8596

86-
protected bool CompareBytes(Stream stream)
87-
=> this.Bytes.Any(x => x.All(b => stream.ReadByte() == b));
97+
protected bool CompareBytes(byte[] buffer)
98+
{
99+
foreach (var x in this.Bytes)
100+
{
101+
if (buffer.Skip(SkipBytes).Take(x.Length).SequenceEqual(x))
102+
{
103+
return true;
104+
}
105+
}
106+
return false;
107+
}
88108
}
89109
}

FileTypeChecker/FileTypeValidator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ private static ICollection<IFileType> Types
4848
/// <param name="fileContent">File to check as stream.</param>
4949
/// <returns>If current type is supported</returns>
5050
/// <exception cref="System.ArgumentException"></exception>
51-
/// <exception cref="System.A rgumentNullException"></exception>
51+
/// <exception cref="System.ArgumentNullException"></exception>
5252
/// <exception cref="System.NotSupportedException"></exception>
5353
/// <exception cref="System.ObjectDisposedException"></exception>
5454
public static bool IsTypeRecognizable(Stream fileContent)

FileTypeChecker/Types/M4v.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using FileTypeChecker.Abstracts;
2+
3+
namespace FileTypeChecker.Types
4+
{
5+
public class M4v : FileType, IFileType
6+
{
7+
private static readonly string name = "M4v file";
8+
private static readonly string extension = "m4v";
9+
10+
private static readonly byte[][] magicBytesJaggedArray =
11+
{new byte[] {0x66, 0x74, 0x79, 0x70, 0x6D, 0x70, 0x34, 0x32}};
12+
13+
public M4v() : base(name, extension, magicBytesJaggedArray, 4)
14+
{
15+
}
16+
}
17+
}

FileTypeChecker/Types/Mp4.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using FileTypeChecker.Abstracts;
2+
3+
namespace FileTypeChecker.Types
4+
{
5+
public class Mp4 : FileType, IFileType
6+
{
7+
private static readonly string name = "MP4 file";
8+
private static readonly string extension = "mp4";
9+
private static readonly byte[][] magicBytesJaggedArray = { new byte[] { 0x66, 0x74 , 0x79 , 0x70 , 0x4D , 0x53 , 0x4E , 0x56 },
10+
new byte[] { 0x66, 0x74 , 0x79 , 0x70 , 0x69 , 0x73 , 0x6F , 0x6D }};
11+
12+
public Mp4() : base(name, extension, magicBytesJaggedArray, 4)
13+
{
14+
}
15+
}
16+
}

0 commit comments

Comments
 (0)