using System; using System.IO; using xCommons.Models; using xCommons.Helpers; using System.Threading; using xCommons.Constants; using xCommons.Interfaces; using xCommons.Extensions; using System.Threading.Tasks; using xCommons.Configurations; using System.Collections.Generic; namespace xCommons.Providers { public class XFileTypeDetector : IXFileTypeDetector { private readonly XFileTypeDetectorConfiguration configuration; private readonly Dictionary MagicSignatures = new(StringComparer.OrdinalIgnoreCase) { ["image/png"] = new byte[] { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A }, ["image/jpeg"] = new byte[] { 0xFF, 0xD8, 0xFF }, ["image/gif"] = new byte[] { 0x47, 0x49, 0x46, 0x38 }, ["image/bmp"] = new byte[] { 0x42, 0x4D }, ["image/x-icon"] = new byte[] { 0x00, 0x00, 0x01, 0x00 }, ["application/pdf"] = new byte[] { 0x25, 0x50, 0x44, 0x46 }, ["application/zip"] = new byte[] { 0x50, 0x4B, 0x03, 0x04 }, ["application/x-rar-compressed"] = new byte[] { 0x52, 0x61, 0x72, 0x21, 0x1A, 0x07 }, ["application/x-7z-compressed"] = new byte[] { 0x37, 0x7A, 0xBC, 0xAF, 0x27, 0x1C }, ["audio/mpeg"] = new byte[] { 0xFF, 0xFB }, ["audio/ogg"] = new byte[] { 0x4F, 0x67, 0x67, 0x53 }, ["font/ttf"] = new byte[] { 0x00, 0x01, 0x00, 0x00 } }; public XFileTypeDetector( XFileTypeDetectorConfiguration configuration ) { this.configuration = configuration; } /// /// Detect Type based on File Name ... /// /// /// public string DetectFromExtension(string fileName) { // var result = string.Empty; // // Validate ... if (fileName.IsNullOrEmpty()) { return result; } // var extension = Path.GetExtension(fileName); if (extension.IsNullOrEmpty()) { return result; } // result = XFileHelper.GetMimeType(extension); // return result; } /// /// Detect XFileExtensions based on File Name ... /// /// /// public XFileExtensions? DetectExtension(string fileName) { // XFileExtensions? result = null; if (fileName.IsNullOrEmpty()) { return result; } // var extension = Path.GetExtension(fileName); var info = XFileHelper.GetByExtension(extension); if (info.IsNull()) { return result; } // result = (XFileExtensions)info.Value; // return result; } /// /// Check a File Extension is Known or not ... /// /// /// public bool IsKnownExtension(string fileName) { // var result = !fileName.IsNullOrEmpty(); if (!result) { return result; } // var extension = Path.GetExtension(fileName); result = XFileHelper.IsSupported(extension); // return result; } /// /// Detect Type based on File's Magic Byte ... /// /// /// /// public async Task DetectFromMagicBytesAsync( Stream stream, CancellationToken cancellationToken = default ) { // var result = string.Empty; // // Validate ... if (stream == null || !stream.CanRead) { return result; } // var originalPosition = stream.CanSeek ? stream.Position : 0L; try { // var header = new byte[configuration.HeaderSize]; var read = await ReadHeaderAsync(stream, header, cancellationToken); if (read < 2) { return result; } // foreach (var signature in MagicSignatures) { // if (MatchesSignature(header, read, signature.Value)) { // result = signature.Key; return result; } } // return result; } finally { // if (stream.CanSeek) { stream.Position = originalPosition; } } } /// /// Complexity Detect Type ... /// /// /// /// /// /// public async Task DetectAsync( string fileName, string providedMimeType, Stream stream, CancellationToken cancellationToken = default ) { // var result = new XFileTypeDetectionResult { Source = XFileTypeDetectionSource.None }; // if (configuration.PreferMagicBytes) { // var magicMime = await DetectFromMagicBytesAsync(stream, cancellationToken); if (!magicMime.IsNullOrEmpty()) { // result.MimeType = magicMime; result.Extension = XFileHelper.GetExtension(magicMime); result.Source = XFileTypeDetectionSource.MagicBytes; // return result; } } // var extensionMime = DetectFromExtension(fileName); if (!extensionMime.IsNullOrEmpty()) { // result.MimeType = extensionMime; result.Extension = Path.GetExtension(fileName)?.ToLowerInvariant(); result.Source = XFileTypeDetectionSource.Extension; // return result; } // if (!string.IsNullOrWhiteSpace(providedMimeType)) { // var info = XFileHelper.GetByMimeType(providedMimeType); if (info != null) { // result.MimeType = info.MimeType; result.Extension = info.Extension; result.Source = XFileTypeDetectionSource.ProvidedMime; } } // return result; } // #region Private ... private static async Task ReadHeaderAsync( Stream stream, byte[] buffer, CancellationToken ct = default ) { // var totalRead = 0; while (totalRead < buffer.Length) { // var read = await stream.ReadAsync( buffer, totalRead, buffer.Length - totalRead, ct); if (read == 0) break; totalRead += read; } // return totalRead; } private static bool MatchesSignature(byte[] header, int length, byte[] signature) { // if (length < signature.Length) return false; for (var i = 0; i < signature.Length; i++) { if (header[i] != signature[i]) return false; } // return true; } #endregion } }