| 1 |
#!/bin/sh |
| 2 |
|
| 3 |
# HtContent |
| 4 |
# |
| 5 |
# Determine a file's MIME type from its contents. |
| 6 |
# |
| 7 |
# |
| 8 |
|
| 9 |
# Part of the ht://Dig package <http://www.htdig.org/> |
| 10 |
# Copyright (c) 2003 The ht://Dig Group |
| 11 |
# For copyright details, see the file COPYING in your distribution |
| 12 |
# or the GNU Public License version 2 or later |
| 13 |
# <http://www.gnu.org/copyleft/gpl.html> |
| 14 |
|
| 15 |
# $Id: HtFileType,v 1.5 2004/06/11 15:55:16 grdetil Exp $ |
| 16 |
|
| 17 |
if file -v > /dev/null; then |
| 18 |
have_modern_file=true |
| 19 |
else |
| 20 |
have_modern_file=false |
| 21 |
fi |
| 22 |
|
| 23 |
tmpfile=`mktemp /tmp/HtFileType.XXXXXX` || exit 1 |
| 24 |
magic_file=@CONFIG_DIR@/HtFileType-magic.mime |
| 25 |
#magic_file=${0}-magic.mime |
| 26 |
|
| 27 |
# Go through each specified file. |
| 28 |
# Can't say 'for input in "$*"; do' as that breaks up names containing spaces |
| 29 |
while [ $# -gt 0 ]; do |
| 30 |
input="$1"; shift; |
| 31 |
|
| 32 |
#echo -n $input ' ' |
| 33 |
|
| 34 |
# Classify based on start of file. Strip leading whitespace and |
| 35 |
# convert broken "<!" style comments to "<!--". |
| 36 |
# (If available, use a fixed magic file, which is faster...) |
| 37 |
# From the output, strip path name and comments like ', with very long lines' |
| 38 |
|
| 39 |
if $have_modern_file; then |
| 40 |
output=`file -i -b -m $magic_file "$input" 2>/dev/null | sed 's/[,;].*//'` |
| 41 |
else |
| 42 |
# old file(1) command can't strip leading whitespace, or accept -i option |
| 43 |
head -100 "$input" 2> /dev/null | tr '\012\015' ' ' | |
| 44 |
sed -e's/^ *//' -e's/ *<! /<!--/' > $tmpfile |
| 45 |
output=`file -m $magic_file $tmpfile 2>/dev/null | sed -e 's/.*:[ ]*//' -e's/[,;].*//'` |
| 46 |
/bin/rm -f $tmpfile |
| 47 |
fi |
| 48 |
|
| 49 |
case $output in |
| 50 |
# 'file' calls most human-readable files "text", so check what type |
| 51 |
*text) case $output in |
| 52 |
*HTML* ) type=text/html;; |
| 53 |
*SGML* ) type=text/sgml;; |
| 54 |
*XML* ) type=text/xml;; |
| 55 |
|
| 56 |
# ignore most source code |
| 57 |
*script* | *program* | *command* ) type=application/x-unknown;; |
| 58 |
*pre-processor*|*"'diff' output"*) type=application/x-unknown;; |
| 59 |
|
| 60 |
# assume all other text is plain |
| 61 |
# (Includes outputs "English t.", "ASCII t.", "international t.") |
| 62 |
*) type=text/plain;; |
| 63 |
esac;; |
| 64 |
|
| 65 |
# Our magic file already outputs MIME types, so don't change them |
| 66 |
*/* ) type=$output;; |
| 67 |
|
| 68 |
# Other recognised types |
| 69 |
*postscript* | *PostScript* ) type=application/postscript;; |
| 70 |
*PDF* | *acrobat* ) type=application/pdf;; |
| 71 |
|
| 72 |
# otherwise give up |
| 73 |
*) type=application/x-unknown;; |
| 74 |
esac |
| 75 |
|
| 76 |
# Catch HTML documents, which are special cases of SGML and XML |
| 77 |
case $type in |
| 78 |
text/xml | text/sgml ) |
| 79 |
if head -100 "$input"| egrep -i '<html|<head|<title|< *a *href *='>/dev/null |
| 80 |
then |
| 81 |
echo text/html |
| 82 |
else |
| 83 |
echo $type |
| 84 |
fi;; |
| 85 |
* ) echo $type |
| 86 |
esac |
| 87 |
done |