Register File Extensions/Mime Types in Linux

Register file extensions / mime types in Linux

Use xdg-utils from freedesktop.org Portland.

Register the icon for the MIME type:

xdg-icon-resource install --context mimetypes --size 48 myicon-file-type.png x-application-mytype

Create a configuration file (freedesktop Shared MIME documentation):

<?xml version="1.0"?>
<mime-info xmlns='http://www.freedesktop.org/standards/shared-mime-info'>
<mime-type type="application/x-mytype">
<comment>A witty comment</comment>
<comment xml:lang="it">Uno Commento</comment>
<glob pattern="*.myapp"/>
</mime-type>
</mime-info>

Install the configuration file:

xdg-mime install mytype-mime.xml

This gets your files recognized and associated with an icon. xdg-mime default can be used for associating an application with the MIME type after you get a .desktop file installed.

Unix file associations of mime-type with multiple extensions

As of 8.0.1, this is indeed not possible. In 8.0.2, you will be able to specify multiple extensions separated by commas. Please contact support@ej-technologies.com to get a build where this is already implemented.

Mime types for custom file type

I finally found the issue:

the mime info file has to have exactly the name of the mime type. Renaming it to x-cal.xml did the trick.

Create files with specific mime type

The concept of a "file" is simply some data which belongs to some metadata and that metadata includes a file name and might also include some other stuff like permissions and time stamps.

A file by itself does usually not contain any information about mime types, but its file name might include an extension which might give a hint (right or wrong) about the file content.

Mime types is rather a concept used when a web server sends data to a web client to tell the client how the data should be used. The data that the server sends to a client might be taken from a file or dynamically generated by the web server.

How I could create register file type and its icon using Qt Framework Installer?

On Linux you can define new extensions in /etc/mime.types.

Then you need to have a .desktop file in /usr/share/applications for your program, that defines which MIME types it is connected to.

You might want to look at this SO thread.

How do I associate a file extension with an application using a .desktop file (under linux)?

You need to create a MIME type first.

Creating Files Of Specific Type

There is no "type" of regular file in Linux/Unix. You could register any of your own file extension to open files with specified extension with application you will chose.

Please look at answers for question: Register file extensions / mime types in Linux

Also to understand other association mechanism on Linux named "Shebangs (#!)", you could read this: File extensions and association with programs in linux

How can I find out a file's MIME type (Content-Type)?

Use file. Examples:

> file --mime-type image.png
image.png: image/png

> file -b --mime-type image.png
image/png

> file -i FILE_NAME
image.png: image/png; charset=binary

Get Mime type from file extension

you need to use GContentType inside GIO:

https://developer.gnome.org/gio/stable/gio-GContentType.html

and to be precise g_content_type_guess():

https://developer.gnome.org/gio/stable/gio-GContentType.html#g-content-type-guess

which takes either a file name, or the file contents, and returns the guessed content type; from there, you can use g_content_type_get_mime_type() to get the MIME type for the content type.

this is an example for how to use g_content_type_guess():

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <gio/gio.h>

int
main (int argc, char *argv[])
{
const char *file_name = "test.html";
gboolean is_certain = FALSE;

char *content_type = g_content_type_guess (file_name, NULL, 0, &is_certain);

if (content_type != NULL)
{
char *mime_type = g_content_type_get_mime_type (content_type);

g_print ("Content type for file '%s': %s (certain: %s)\n"
"MIME type for content type: %s\n",
file_name,
content_type,
is_certain ? "yes" : "no",
mime_type);

g_free (mime_type);
}

g_free (content_type);

return EXIT_SUCCESS;
}

once compiled, the output on Linux is:

Content type for file 'test.html': text/html (certain: no)
MIME type for content type: text/html

explanation: on Linux, content type is a MIME type; this is not true on other platforms, that's why you must convert GContentType strings to MIME type.

also, as you can see, just using the extension will set the boolean is certain flag, as an extension by itself is not enough to determine the accurate content type.



Related Topics



Leave a reply



Submit