ConfigurationDetector.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Xml;
  6. using Masuit.Tools.Mime;
  7. namespace Masuit.Tools.Files.FileDetector.Detectors;
  8. [FormatCategory(FormatCategory.Document)]
  9. internal sealed class ConfigurationDetector : IDetector
  10. {
  11. public string Precondition => "txt";
  12. public string Extension => "config";
  13. public string MimeType => new MimeMapper().GetMimeFromExtension("." + Extension);
  14. public List<FormatCategory> FormatCategories => GetType().GetCustomAttributes<FormatCategoryAttribute>().Select(a => a.Category).ToList();
  15. public bool Detect(Stream stream)
  16. {
  17. try
  18. {
  19. var reader = XmlReader.Create(stream, new XmlReaderSettings()
  20. {
  21. });
  22. if (reader.Read() && reader.IsStartElement() && reader.Name == "configuration")
  23. {
  24. return true;
  25. }
  26. }
  27. catch
  28. {
  29. }
  30. return false;
  31. }
  32. public override string ToString() => "Microsoft .NET Configuration File Detector";
  33. }