magic2mime 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #! /usr/bin/env perl
  2. # -*- PERL -*-
  3. # $File: magic2mime,v 1.4 2006/11/25 18:36:10 christos Exp $
  4. # Copyright (c) 1996, 1997 vax@linkdead.paranoia.com (VaX#n8)
  5. #
  6. # Usage: echo 'your-file-output-here' | file_to_ctype.pl
  7. # file -b files... | file_to_ctype.pl
  8. # It acts like a filter, reading from STDIN and any files on the command
  9. # line, printing to STDOUT.
  10. ## refs
  11. # http://www.faqs.org/faqs/mail/mime-faq/part1/index.html
  12. # comp.mail.mime FAQ
  13. # ftp://ftp.isi.edu/in-notes/iana/assignments/media-types/media-types
  14. # assigned content-types
  15. # ftp://ftp.uu.net/inet/rfc/rfc-index
  16. # RFC index; search for MIME
  17. @mapping =
  18. (
  19. # defaults
  20. 'data', 'application/octet-stream',
  21. 'text', 'text/plain',
  22. # more specific
  23. '^Rich Text Format data', 'text/richtext',
  24. '^HTML document text', 'text/html',
  25. '^exported SGML document text', 'text/sgml',
  26. 'mail text', 'message/rfc822',
  27. 'news text', 'message/news',
  28. '^PostScript document text', 'application/postscript',
  29. '^BinHex binary text', 'application/mac-binhex40',
  30. '^Zip archive data', 'application/zip',
  31. '^Microsoft Word', 'application/msword',
  32. '^PGP key', 'application/pgp-keys',
  33. '^PGP encrypted', 'application/pgp-encrypted',
  34. '^PGP armored data signature', 'application/pgp-signature',
  35. '^JPEG image', 'image/jpeg',
  36. '^GIF image', 'image/gif',
  37. '^PNG image', 'image/png',
  38. '^TIFF image', 'image/tiff',
  39. 'Computer Graphics Metafile', 'image/cgf',
  40. '^Sun/NeXT audio data', 'audio/basic',
  41. '^MPEG', 'video/mpeg',
  42. '^Apple QuickTime movie', 'video/quicktime',
  43. '^DICOM medical imaging data', 'application/dicom',
  44. # made up by me
  45. '^bitmap', 'image/x-bitmap',
  46. '^PC bitmap data, Windows 3.x format', 'image/x-msw3bmp',
  47. '^FLI', 'video/x-fli',
  48. '^FLC', 'video/x-flc',
  49. 'AVI data', 'video/x-avi',
  50. 'WAVE', 'audio/x-wav',
  51. 'VOC', 'audio/x-voc',
  52. );
  53. local($mimetype,$index,$regexp);
  54. while (<>)
  55. {
  56. chop;
  57. $index = $#mapping - 1;
  58. while ($index > -1 && !defined($mimetype))
  59. {
  60. $mimetype = $mapping[$index + 1] if (/$mapping[$index]/);
  61. $index -= 2;
  62. }
  63. print "$mimetype\n";
  64. undef $mimetype; # hack
  65. }
  66. 0;