magic2mime 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #! /usr/bin/perl -w
  2. # -*- PERL -*-
  3. # $Id: magic2mime,v 1.1 1998/02/15 23:18:53 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.*type EPS' => 'image/eps',
  29. '^PostScript document text' => 'application/postscript',
  30. '^PDF document' => 'application/pdf',
  31. '^Rich Text Format' => 'application/rtf',
  32. '^TeX DVI file' => 'application/x-dvi',
  33. '^BinHex binary text' => 'application/mac-binhex40',
  34. '^Zip archive data' => 'application/zip',
  35. 'Microsoft Word[ 0-9.]*document data' => 'application/msword',
  36. '^PGP key' => 'application/pgp-keys',
  37. '^PGP encrypted' => 'application/pgp-encrypted',
  38. '^PGP armored data signature' => 'application/pgp-signature',
  39. '^JPEG image' => 'image/jpeg',
  40. '^GIF image' => 'image/gif',
  41. '^PNG image' => 'image/png',
  42. '^TIFF image' => 'image/tiff',
  43. 'Computer Graphics Metafile' => 'image/cgf',
  44. '^Sun/NeXT audio data' => 'audio/basic',
  45. '^MPEG.*layer 3 audio' => 'audio/mpeg',
  46. '^MPEG' => 'video/mpeg',
  47. '^Apple QuickTime movie' => 'video/quicktime',
  48. '^X pixmap image' => 'image/x-xpixmap',
  49. # made up by me
  50. '^bitmap' => 'image/x-bitmap',
  51. '^PC bitmap data, Windows 3.x format' => 'image/x-msw3bmp',
  52. '^FLI' => 'video/x-fli',
  53. '^FLC' => 'video/x-flc',
  54. 'AVI data' => 'video/x-avi',
  55. 'WAVE' => 'audio/x-wav',
  56. 'VOC' => 'audio/x-voc',
  57. 'Debian binary package' => 'application/x-debian-package',
  58. 'compiled Java class data' => 'application/x-java',
  59. 'MPEG.*audio stream data' => 'audio/mpeg',
  60. 'Standard MIDI data' => 'audio/midi',
  61. );
  62. my($mimetype,$index,$found);
  63. while (<>)
  64. {
  65. chop;
  66. $index = $#mapping - 1;
  67. $found = 0;
  68. $mimetype = "application/octet-stream";
  69. while ($index > -1 && !$found)
  70. {
  71. if(/$mapping[$index]/)
  72. {
  73. $mimetype = $mapping[$index + 1];
  74. $found=1;
  75. }
  76. $index -= 2;
  77. }
  78. print "$mimetype\n";
  79. }
  80. 0;