-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileFormatInfo.cs
More file actions
93 lines (81 loc) · 3.26 KB
/
Copy pathFileFormatInfo.cs
File metadata and controls
93 lines (81 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using SmartFileKit.Detection;
using System;
using System.Collections.Generic;
namespace SmartFileKit.Domain
{
/// <summary>
/// Holds the metadata, mime mapping, category, and detection signatures for a specific file format.
/// </summary>
public class FileFormatInfo
{
/// <summary>
/// Gets the primary file extension (e.g. ".jpg"). Always starts with a dot and is lowercase.
/// </summary>
public string Extension { get; }
/// <summary>
/// Gets the MIME type associated with the format (e.g. "image/jpeg").
/// </summary>
public string MimeType { get; }
/// <summary>
/// Gets the classification category of this format (e.g. Image, Archive).
/// </summary>
public FileCategory Category { get; }
/// <summary>
/// Gets the list of signatures (magic bytes) that can identify this file type.
/// </summary>
public IReadOnlyList<FileSignature> Signatures { get; }
/// <summary>
/// Gets the optional validator for text-based or complex structural content verification.
/// </summary>
public Func<byte[], bool> ExtraValidator { get; }
/// <summary>
/// Initializes a new instance of the <see cref="FileFormatInfo"/> class.
/// </summary>
public FileFormatInfo(
string extension,
string mimeType,
FileCategory category,
IReadOnlyList<FileSignature> signatures = null,
Func<byte[], bool> extraValidator = null)
{
if (string.IsNullOrEmpty(extension))
throw new ArgumentException("Extension cannot be null or empty.", nameof(extension));
if (string.IsNullOrEmpty(mimeType))
throw new ArgumentException("MimeType cannot be null or empty.", nameof(mimeType));
Extension = extension.StartsWith(".") ? extension.ToLowerInvariant() : "." + extension.ToLowerInvariant();
MimeType = mimeType.ToLowerInvariant();
Category = category;
Signatures = signatures ?? Array.Empty<FileSignature>();
ExtraValidator = extraValidator;
}
/// <summary>
/// Checks if a buffer matches any of the registered signatures or custom content validators.
/// </summary>
public bool Matches(byte[] buffer)
{
if (buffer == null || buffer.Length == 0)
return false;
// If we have signatures, at least one must match
if (Signatures.Count > 0)
{
bool signatureMatched = false;
foreach (var signature in Signatures)
{
if (signature.Matches(buffer))
{
signatureMatched = true;
break;
}
}
if (!signatureMatched)
return false;
}
// If we also have a custom validator, run it
if (ExtraValidator != null)
{
return ExtraValidator(buffer);
}
return Signatures.Count > 0; // If no extra validator and matched signature, return true. Otherwise false.
}
}
}