ConfigurationDetector.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 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())
  23. {
  24. if (reader.IsStartElement())
  25. {
  26. if (reader.Name == "configuration")
  27. {
  28. return true;
  29. }
  30. }
  31. }
  32. }
  33. catch
  34. {
  35. }
  36. return false;
  37. }
  38. public override string ToString() => "Microsoft .NET Configuration File Detector";
  39. }