Lothar
Lothar

Reputation: 13093

How do get the GTK mime-type/content-type information from the file extension

I'm not allowed to access the file content (for performance reasons), so using GFile is not an option.

Upvotes: 3

Views: 2344

Answers (2)

gpoo
gpoo

Reputation: 8638

you need g_content_type_guess.

In Python (GTK2):

import gio
gio.content_type_guess('foo.pdf')

This will return application/pdf

In Python (GTK3):

from gi.repository import Gio
content_type, val = Gio.content_type_guess('filename=foo.pdf', data=None)

content_type will contain application/pdf

More details in the documentation: http://developer.gnome.org/gio/stable/gio-GContentType.html#g-content-type-guess

Upvotes: 4

Johannes Sasongko
Johannes Sasongko

Reputation: 4248

Looks like GIO's content type detection is based on the file extension (when there is an extension).

$ ./file /bin/sh
application/x-executable
$ cp /bin/sh a.wav
$ ./file a.wav
audio/x-wav

where ./file is

#!/usr/bin/env python2
import sys, gio
f = gio.File(sys.argv[1])
info = f.query_info('standard::content-type')
print info.get_content_type()

Upvotes: 2

Related Questions