123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.IO.Compression;
- using System.Reflection;
- using System.Text;
- using Masuit.Tools.Mime;
- namespace Masuit.Tools.Files.FileDetector.Detectors;
- [FormatCategory(FormatCategory.Document)]
- internal class OpenDocumentSpreadSheetDetector : AbstractZipDetailDetector
- {
- public override IEnumerable<string> Files
- {
- get
- {
- yield return "mimetype";
- }
- }
- public override string Precondition => "zip";
- public override string Extension => "ods";
- public override string MimeType => new MimeMapper().GetMimeFromExtension("." + Extension);
- public override List<FormatCategory> FormatCategories => GetType().GetCustomAttributes<FormatCategoryAttribute>().Select(a => a.Category).ToList();
- protected override bool IsValid(string filename, ZipArchiveEntry entry)
- {
- if (filename == "mimetype")
- {
- using Stream mimetypeStream = entry.Open();
- byte[] buffer = new byte["application/vnd.oasis.opendocument.spreadsheet".Length];
- if (mimetypeStream.Read(buffer, 0, buffer.Length) != buffer.Length)
- {
- return false;
- }
- if (Encoding.GetEncoding("ascii").GetString(buffer, 0, buffer.Length) != "application/vnd.oasis.opendocument.spreadsheet")
- {
- return false;
- }
- }
- return base.IsValid(filename, entry);
- }
- public override string ToString() => "OpenDocument ShreadSheet File Detector";
- }
|