OpenDocumentFormulaDetector.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Linq;
  4. using System.IO.Compression;
  5. using System.Reflection;
  6. using System.Text;
  7. using Masuit.Tools.Mime;
  8. namespace Masuit.Tools.Files.FileDetector.Detectors;
  9. [FormatCategory(FormatCategory.Document)]
  10. internal class OpenDocumentFormulaDetector : AbstractZipDetailDetector
  11. {
  12. public override IEnumerable<string> Files
  13. {
  14. get
  15. {
  16. yield return "mimetype";
  17. }
  18. }
  19. public override string Precondition => "zip";
  20. public override string Extension => "odf";
  21. public override string MimeType => new MimeMapper().GetMimeFromExtension("." + Extension);
  22. public override List<FormatCategory> FormatCategories => GetType().GetCustomAttributes<FormatCategoryAttribute>().Select(a => a.Category).ToList();
  23. protected override bool IsValid(string filename, ZipArchiveEntry entry)
  24. {
  25. if (filename == "mimetype")
  26. {
  27. using var mimetypeStream = entry.Open();
  28. byte[] buffer = new byte["application/vnd.oasis.opendocument.formula".Length];
  29. if (mimetypeStream.Read(buffer, 0, buffer.Length) != buffer.Length)
  30. {
  31. return false;
  32. }
  33. if (Encoding.GetEncoding("ascii").GetString(buffer, 0, buffer.Length) != "application/vnd.oasis.opendocument.formula")
  34. {
  35. return false;
  36. }
  37. }
  38. return base.IsValid(filename, entry);
  39. }
  40. public override string ToString() => "OpenDocument Formula File Detector";
  41. }