37 lines
1.0 KiB
C#
37 lines
1.0 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace InternshipSystem.Api.Service
|
|
{
|
|
public class FileValidator
|
|
{
|
|
private readonly List<(string Mime, byte[] Signature)> validFileTypes;
|
|
|
|
public FileValidator()
|
|
{
|
|
validFileTypes = new List<(string, byte[])> {
|
|
("application/pdf", new byte[] { 0x25, 0x50, 0x44, 0x46 }),
|
|
("image/jpeg", new byte[] { 0xFF, 0xD8, 0xFF, 0xDB }),
|
|
("image/jpeg", new byte[] { 0xFF, 0xD8, 0xFF, 0xE0 }),
|
|
("image/jpeg", new byte[] { 0xFF, 0xD8, 0xFF, 0xE1 })
|
|
};
|
|
}
|
|
|
|
public bool IsValidFile(byte[] scan)
|
|
{
|
|
return IsFileValidType(scan);
|
|
}
|
|
|
|
private bool IsFileValidType(byte[] scan)
|
|
{
|
|
return GetFileMime(scan) != null;
|
|
}
|
|
|
|
public string GetFileMime(byte[] scan)
|
|
{
|
|
var header = scan[..4];
|
|
|
|
return validFileTypes.FirstOrDefault(sig => sig.Signature.SequenceEqual(header)).Mime;
|
|
}
|
|
}
|
|
} |