FileDetector.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. const detect = require('detect-file-type');
  2. //html
  3. detect.addSignature(
  4. {
  5. "type": "html",
  6. "ext": "html",
  7. "mime": "text/html",
  8. "rules": [
  9. { "type": "or", "rules":
  10. [
  11. { "type": "contains", "bytes": "3c68746d6c" },
  12. { "type": "contains", "bytes": "3c00680074006d006c00" },
  13. { "type": "contains", "bytes": "3c21646f6374797065" },
  14. { "type": "contains", "bytes": "3c626f6479" },
  15. { "type": "contains", "bytes": "3c68656164" },
  16. { "type": "contains", "bytes": "3c696672616d65" },
  17. { "type": "contains", "bytes": "3c696d67" },
  18. { "type": "contains", "bytes": "3c6f626a656374" },
  19. { "type": "contains", "bytes": "3c736372697074" },
  20. { "type": "contains", "bytes": "3c7461626c65" },
  21. { "type": "contains", "bytes": "3c7469746c65" },
  22. ]
  23. }
  24. ]
  25. }
  26. );
  27. //xml 3c 3f 78 6d 6c 20 76 65 72 73 69 6f 6e 3d 22 31 2e 30 22
  28. detect.addSignature(
  29. {
  30. "type": "xml",
  31. "ext": "xml",
  32. "mime": "application/xml",
  33. "rules": [
  34. { "type": "or", "rules":
  35. [
  36. { "type": "contains", "bytes": "3c3f786d6c2076657273696f6e3d22312e3022" },
  37. ]
  38. }
  39. ]
  40. }
  41. );
  42. class FileDetector {
  43. detectFile(filename) {
  44. return new Promise((resolve, reject) => {
  45. detect.fromFile(filename, (err, result) => {
  46. if (err) reject(err);
  47. resolve(result);
  48. });
  49. });
  50. }
  51. }
  52. module.exports = FileDetector;