General TC > Programming & Scripting - Unofficial
is file or gio (glib2) required to find the mimetype of a file ?
(1/1)
mocore:
file example from https://github.com/Cloudef/PKGBUILDS/blob/master/linopen/open#L90
--- Code: ---$(file -L -b --mime-type "$1")
--- End code ---
the method this uses appears to be something called libmagic
https://man7.org/linux/man-pages/man3/libmagic.3.html
which it appears compiles a list of file signatures
https://en.wikipedia.org/wiki/List_of_file_signatures
some inconsistencies between versions of file mentioned @ https://stackoverflow.com/questions/2227182/how-can-i-find-out-a-files-mime-type-content-type/18624064#18624064
--- Quote ---file version < 5 : file -i -b /path/to/file
file version >=5 : file --mime-type -b /path/to/file
--- End quote ---
gio example from https://gitlab.com/GNUser/tinyopen/-/blob/master/tinyopen/tinyopen?ref_type=heads#L42
--- Code: --- mimetype="$(gio info "$1" | grep '::content-type' | cut -d' ' -f4)"
--- End code ---
how gio mime works ....
--- Code: ---' is a mystery
It's like the changing of the seasons
And the tides of the sea
--- End code ---
so
is file or gio (glib2) required to find the mimetype of a file ?
eg can mimeID be scripted ???
---
https://wiki.debian.org/MIME
customized database(json) of types
https://github.com/jshttp/mime-db/tree/master/src
sources they use
https://github.com/jshttp/mime-db/blob/master/scripts/fetch-apache.js#L32
https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types
https://github.com/jshttp/mime-db/blob/master/scripts/fetch-iana.js#L97
'https://www.iana.org/assignments/media-types/' ?..?..?
https://github.com/jshttp/mime-db/blob/master/scripts/fetch-nginx.js#L30
https://raw.githubusercontent.com/nginx/nginx/master/conf/mime.types
Rich:
Hi mocore
--- Quote from: mocore on October 03, 2024, 03:07:46 AM --- ... is file or gio (glib2) required to find the mimetype of a file ?
eg can mimeID be scripted ??? ...
--- End quote ---
It can be scripted, but it may get complicated if you want
it to be foolproof and cover all mime types.
This is a simple version I wrote for a limited number of
file types:
https://forum.tinycorelinux.net/index.php/topic,27204.0.html
Here are a couple of examples of how I use it:
https://forum.tinycorelinux.net/index.php/topic,27199.msg174770.html#msg174770
https://forum.tinycorelinux.net/index.php/topic,24247.msg153398.html#msg153398
These example are from before I changed the script to allow
piping data through it.
mocore:
--- Quote from: Rich on October 03, 2024, 09:52:00 AM ---
It can be scripted, but it may get complicated if you want
it to be foolproof and cover all mime types.
--- End quote ---
hi rich
thanks for the links and script !!
...coincidentally i just happened to find another example of scripted magic
@ https://raw.githubusercontent.com/markfeit/sheller/refs/heads/master/libexec/sheller/modules/file
--- Code: ---# Determine if a file is an iso9660 image without using file(1)
#
# Args:
# 1 - Path to file
file_is_iso9660()
{
[ -r "$1" ] \
&& __ISO9660MAGIC=$(dd "if=$1" bs=1 skip=32769 count=5 2>/dev/null) \
&& [ "${__ISO9660MAGIC}" = "CD001" ] \
|| false
}
--- End code ---
mocore:
ftr
hear -> https://blog.trailofbits.com/2022/07/01/libmagic-the-blathering/
is an interesting write up of lib-magic implementation methods/techniques (and ambiguities / bugs ect)
and an alternative (not binding) implemented in python https://github.com/trailofbits/polyfile
Navigation
[0] Message Index
Go to full version