Search code examples
node.jsubuntuelectronmime-typesfile-type

How can I create my own file type on Node?


I am wondering if I can change the mime-type of a file on Node. I have checked many mime-type modules, but all of them are just to get information from the files and from the system.

What I would like to achieve is to zip some files in one single file and then, change the mime-type. On this way I could check if the file type is suited for my application.

I know there are differences between operating systems:

  • Windows. I change the extension and it is done
  • Ubuntu. I must deal with mime-types. This is what I would like to change
  • Mac. They have a system similar to Ubuntu.

How to achieve this on Ubuntu or Mac?

Assuming that I can do it and I have just changed the mime-type of a zip file on Ubuntu, is it still going to detect the file as a zip file by the system?

If all of this is not possible. How can I create my own file built with different files?

For example I would like to include all these files compressed inside the packaged file called filename.foo:

  • An XML file with metadata
  • A text file
  • A CSV file
  • An image

Solution

  • Finally I have changed the file extension and added a new MIME-TYPE to the system. In this way Nautilus (Ubuntu) and the Windows explorer recognise the file with the new type.

    When I use the command file --mime-type I still detect the zip mime-type. But that's not bad because I can use this detection to avoid errors in my app.

    Create your own MIME-TYPE for Nautilus

    1. Create a new XML in /usr/share/mime/packages/ with this content:

      <?xml version="1.0" encoding="UTF-8"?>
      <mime-info xmlns='http://www.freedesktop.org/standards/shared-mime-info'>
        <mime-type type="application/todo">
          <comment>Todos Text file</comment>
          <glob pattern="*.TODO"/>
          <glob pattern="*.todo"/>
        </mime-type>
      </mime-info>
      
    2. Execute the following command to update mime database

      sudo update-mime-database /usr/share/mime
      

    Assign an icon to the new extension

    First step is renaming the icon to application-todo and it must be in svg extension.

    If you're using gnome you have move the icon to /usr/share/icons/gnome/scalable then update the cache with

    $ sudo gtk-update-icon-cache /usr/share/icons/gnome -f
    

    If you're using unity move it to /usr/share/icons/Humanity/mimes/ then update the cache

    $ sudo gtk-update-icon-cache /usr/share/icons/Humanity -f
    

    Give your nautilus a refresh and you'll have a new mime-type with icon