WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: is file or gio (glib2) required to find the mimetype of a file ?  (Read 373 times)

Offline mocore

  • Hero Member
  • *****
  • Posts: 647
  • ~.~
file example from https://github.com/Cloudef/PKGBUILDS/blob/master/linopen/open#L90
Code: [Select]
$(file -L -b --mime-type "$1")
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

gio example from https://gitlab.com/GNUser/tinyopen/-/blob/master/tinyopen/tinyopen?ref_type=heads#L42
Code: [Select]
mimetype="$(gio info "$1" | grep '::content-type' | cut -d' ' -f4)"

how gio mime works ....
Code: [Select]
' is a mystery
It's like the changing of the seasons
And the tides of the sea

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
« Last Edit: October 03, 2024, 03:27:11 AM by mocore »

Online Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11656
Re: is file or gio (glib2) required to find the mimetype of a file ?
« Reply #1 on: October 03, 2024, 09:52:00 AM »
Hi mocore
... is file or gio (glib2) required to find the mimetype of a file ?

eg can mimeID be scripted ??? ...
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.

Offline mocore

  • Hero Member
  • *****
  • Posts: 647
  • ~.~
Re: is file or gio (glib2) required to find the mimetype of a file ?
« Reply #2 on: October 08, 2024, 04:07:38 AM »

It can be scripted, but it may get complicated if you want
it to be foolproof and cover all mime types.


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: [Select]
# 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   
}


Offline mocore

  • Hero Member
  • *****
  • Posts: 647
  • ~.~
libmagic / polyfile
« Reply #3 on: October 16, 2024, 03:19:11 PM »
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