JpegDetector.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Reflection;
  5. using Masuit.Tools.Mime;
  6. namespace Masuit.Tools.Files.FileDetector.Detectors;
  7. [FormatCategory(FormatCategory.Image)]
  8. public sealed class JpegDetector : AbstractSignatureDetector
  9. {
  10. private static readonly SignatureInformation[] JpegSignatureInfo = {
  11. new() { Position = 0, Signature = new byte [] { 0xFF, 0xD8, 0xFF, 0xFE, 0x00 } },
  12. new () { Position = 0, Signature = new byte [] { 0xFF, 0xD8, 0xFF, 0xDB } },
  13. new () { Position = 0, Signature = new byte [] { 0xFF, 0xD8, 0xFF, 0xE0 } },
  14. new () { Position = 0, Signature = new byte [] { 0xFF, 0xD8, 0xFF, 0xE1 } },
  15. new () { Position = 0, Signature = new byte [] { 0xFF, 0xD8, 0xFF, 0xE2 } },
  16. new () { Position = 0, Signature = new byte [] { 0xFF, 0xD8, 0xFF, 0xE3 } },
  17. new () { Position = 0, Signature = new byte [] { 0xFF, 0xD8, 0xFF, 0xE8 } },
  18. new () { Position = 0, Signature = new byte [] { 0xFF, 0xD8, 0xFF, 0xEE } },
  19. };
  20. public override string Extension => "jpg";
  21. protected override SignatureInformation[] SignatureInformations => JpegSignatureInfo;
  22. public override string MimeType => new MimeMapper().GetMimeFromExtension("." + Extension);
  23. public override List<FormatCategory> FormatCategories => GetType().GetCustomAttributes<FormatCategoryAttribute>().Select(a => a.Category).ToList();
  24. public override string ToString() => "JPEG Detector";
  25. }