magic2mime 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #! /usr/local/bin/perl
  2. # -*- PERL -*-
  3. # $Id: magic2mime,v 1.2 2003/03/23 04:17:27 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. # made up by me
  44. '^bitmap', 'image/x-bitmap',
  45. '^PC bitmap data, Windows 3.x format', 'image/x-msw3bmp',
  46. '^FLI', 'video/x-fli',
  47. '^FLC', 'video/x-flc',
  48. 'AVI data', 'video/x-avi',
  49. 'WAVE', 'audio/x-wav',
  50. 'VOC', 'audio/x-voc',
  51. );
  52. local($mimetype,$index,$regexp);
  53. while (<>)
  54. {
  55. chop;
  56. $index = $#mapping - 1;
  57. while ($index > -1 && !defined($mimetype))
  58. {
  59. $mimetype = $mapping[$index + 1] if (/$mapping[$index]/);
  60. $index -= 2;
  61. }
  62. print "$mimetype\n";
  63. undef $mimetype; # hack
  64. }
  65. 0;