mirror of
https://github.com/ldqk/Masuit.Tools.git
synced 2026-10-19 23:30:04 +08:00
52 lines
1.5 KiB
C#
52 lines
1.5 KiB
C#
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 sealed class OpenDocumentTextDetector : AbstractZipDetailDetector
|
|
{
|
|
public override IEnumerable<string> Files
|
|
{
|
|
get
|
|
{
|
|
yield return "mimetype";
|
|
}
|
|
}
|
|
|
|
public override string Precondition => "zip";
|
|
|
|
public override string Extension => "odt";
|
|
|
|
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.text".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.text")
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return base.IsValid(filename, entry);
|
|
}
|
|
|
|
public override string ToString() => "OpenDocument Text File Detector";
|
|
}
|