Как изменить mime type файла

What I'm looking for is a counterpart to file -I (Darwin; -i on Linux). For example, given: $ file -I filename.pdf filename.pdf: application/octet-stream; charset=binary I would like to be able ...

It’s a bit of a category mistake to talk about ‘the MIME type of a file’ – ‘files’ don’t have MIME types; only octet streams have them (I’m not necessarily disagreeing with @wumpus-q-wumbley’s description of MIME types as ‘fictional’, but this is another way of thinking about it).

MIME stands for Multipurpose Internet Mail Extensions, as originally described in in RFC 2045, and MIME types were originally intended to describe what a receiver is supposed to do with the bunch of bytes soon to follow down the wire, in the rest of the email message. They were very naturally repurposed in (for example) the HTTP protocol, to let a client understand how it is to interpret the bytes in the HTTP response which this MIME type forms the header of.

The fact that the file command can display a MIME type suggests the further extension of the idea, to act as the key which lets a windowing system look up the name of an application which should be used to open the file.

Thus, if ‘the MIME type of a file’ means anything, it means ‘the MIME type which a web server would prefix to this file if it were to be delivered in response to an HTTP request’ (or something like that). Thought of like that, it’s clear that the MIME type is part of the web server’s configuration, and not anything intrinsic to the file – a single file might be delivered with various MIME types depending on the URL which retrieves it, and details of the request and configuration. Thus an XHTML file might be delivered as text/html or application/xml or application/octet-stream depending on the details of the HTTP request, the directory the file’s located in, or indeed the phase of the moon (the latter would be an unhelpful server configuration).

A web server might have a number of mechanisms for deciding on this MIME type, which might include a lookup table based on any file extension, a .htaccess file, or indeed the output of the file command.

So the answer to your question is: it depends.

  • If what you want to do is change how a web server delivers this file, then you need to look at either your web server documentation, or the contents of your system’s /etc/mime.types file (if your system uses that and if the server is configured to fall back on that).
  • If what you want to do is to change the application which opens a given (type of) file, then your OS/window-manager documentation should help.
  • If you need to change the output of the file command specifically, for some other reason, then man file is your friend, and you’ll probably need to grub around in the magic numbers file, reasonably carefully.

I have a file that is formatted as a .doc, but Nautilus and LibreOffice insists that it is a .txt. Both precise and Ubuntu 12.10 are that way, but Google Docs can convert it (after playing with it).

enter image description here

enter image description here

enter image description here

asked Aug 24, 2012 at 13:23

jrg's user avatar

jrgjrg

59.5k53 gold badges171 silver badges246 bronze badges

3

To check if ‘extension’ mime type exist:

grep 'extension' /etc/mime.types

To create new mime type:

  1. Open /etc/mime.types with text-editor(with root privileges)

    gksudo gedit /etc/mime.types

  2. Add extension to /etc/mime.types in following format:

    text/extension extension

For more info click here

answered Aug 24, 2012 at 13:37

zuberuber's user avatar

zuberuberzuberuber

2,0202 gold badges19 silver badges32 bronze badges

1

Could be something as simple as a mangled/intentionally changed header. Here’s what the MS Word first 2 chunks looks like in a couple of documents I just checked:

D0 CF 11 E0 A1 B1 1A E1

You could MAKE A BACKUP and try changing the first several bytes to that signature and see if it doesn’t at least TRY to open as the right thing.

Generally, file can look at this area:
enter image description here
And tell the file-type. If it’s detecting it wrong, either it actually is a text file renamed to doc or the header got borked.

answered Aug 24, 2012 at 14:35

RobotHumans's user avatar

RobotHumansRobotHumans

28.7k3 gold badges75 silver badges113 bronze badges

0

  1. In Nautilus, right-click on any file with the desired filetype or extension, choose Properties from the context menu.
  2. In Properties click on the Open With.
  3. Select an application for the given filetype (writer I asssume). All files with the same extension will now be opened with this program by default.

enter image description here

You can do this manually too

  • /usr/share/applications/defaults.list holds the defaults (ie. system wide associations).
  • ~/.local/share/applications/mimeapps.list holds user specifics.

By the way: I assume you have software installed (ie. LibreOffice) that can open doc files?


From the comments and images added:

  • it could be that the file is corrupt. What a file is is based on the 1st byte of a file (and not the extension as Windows does). What you could do is set .txt files to open with Writer (just temporary) and see if it then does open and then save it under another name.
    Then reset opening txt to Gedit or what it was before.

answered Aug 24, 2012 at 13:38

Rinzwind's user avatar

RinzwindRinzwind

288k39 gold badges561 silver badges701 bronze badges

5

Short answer:

curl -Ls https://git.io/fj9xx > ole2compounddocs
file -m ole2compounddocs -e cdf -b --mime-type file.pub

application/vnd.ms-publisher

Long answer:

First (for posterity, some background):

The file utility (that is, the implementation used today by most systems) is Ian Darwin’s «Fine Free File Command» (n some package managers, this is called file; in others, it is called libmagic. The file command is a CLI utility wrapping libmagic. Basically, they’re the same thing.

The libmagic library itself typically relies on a «database» (a compressed concatenated list, usually called «magic.mgc», installed in various places depending on OS) of «magic» files that contain regex and other patterns that the library can use fingerprint the «type» of unknown files (or data streams) from samples of raw binary.

The filetype of a file is determined through a list of various criteria based on internal rules, the filename/extension (if any), the «main» compressed magic file, and any magic files added on the command line with the -m flag.

The tool has to strike a balance between what is a file’s «type», and what is information contained in it. In many cases, a file extension is of little value, but some; take, for example, the classic case of a .zip file and a .jar file. Both are «zip files», i.e., i.e., they are files compressed with Info-Zip specification. In this case, the file extension should be enough for it to know this is a Java executable, not just a zip archive. However, with Microsoft Publisher’s ‘.pub’ extension, there is little value to this, as the most common use of ‘.pub’ on *nix platforms is for SSH public keys.

Futher complicating matters is the fact that Microsoft uses several different versions of its «Object Linking and Embedding (OLE)» formats within its «Compound Document File» archives for a myriad of very different applications. These themselves are usually zipped XML files. All of this adds up to some pretty difficult (and computationally expensive) logic to drill down through multiple binary/compression formats to be able to distinguish a «Publisher» document from, say, a Microsoft Outlook database.

Finally, its important to note that MIME types (technically, IANA media types) are notoriously unreliable markers of «content disposition» for precisely the reasons above, and because many developers don’t bother to register their «vendor» (vdn.xxxxxxx) types: although Microsoft has registered a variety of media types, application/vnd.ms-office (which is what file uses for CDF/CDF2 files) is unregistered, as is application/ms-publisher. In fact the mime.types file is never checked by libmagic, and its existance is not at all standard on most platforms. Instead, MIME types are simple hardcoded in «magic» files, and application/vnd.ms-publisher has only recently been added.

Its also important to note that the -i flag’s functionality often differs greatly from what’s expected, and it operates differently in various versions of file, or may not exist at all. However, in general, it’s intent is machine readability and (importantly), in some versions, it changes the heuristics in favor of more machine-relevant results. Therefore, in some versions of file and its database, you’ll get a result from file -bi that is simply: application/octet-stream; charset=binary (which is a valid, non-vendor MIME type) instead of application/vnd.ms-office. And if does return application/vnd.ms-office (CDF), it requires the flags -e cdf to effectively «pass through» that result, because (assumedly), is more relevant to another program/algorthm that can read CDF files than the fact that it is a Publisher file. Ignoring the CDF result to keep searching and get any of the ODF2 identifiers and their MIME types is significantly more computationally expensive — the result of application/vnd.ms-office takes six magic test operations, while application/vnd.ms-publisher takes hundreds or thousands (you can see this with the -d flag). Another reason its best to avoid -i: in most newer versions of file, that flag returns the MIME encoding as well as the MIME type, so its best to use --mime-type if you do not want the result to be application/vnd.ms-publisher; charset=binary.

In conclusion, to get the results you want, my recommendation would be to:

  1. Recompile a new version of file from bleeding-edge source, and
  2. Use the flags: file -b -e cdf --mime-type instead of file -bi.

Introduction

MIME types are a sort of identification card for files on Ubuntu. A file’s MIME type is usually determined by the extension of its filename; for example, a text file may end in .txt, and image file might end in .png or .jpg. This tells Ubuntu what icon should be used to represent it and what programs should open it by default.

Unfortunately, the obvious options for editing MIME types in Ubuntu are shallow. You can teach Ubuntu to open a certain file extension with a certain program using the Properties window, but you cannot set an icon for that extension as a whole: only for the individual file. This can be annoying if you are say, a Python programmer. Ubuntu does not include a special icon for Python files, and it is hard to visually differentiate .py and .pyc files.

This guide will both teach you to add new MIME types and to associate icons with existing types. We will use .py files as an example.

Adding a MIME Type

To start out, we must be assure that the MIME type for our file extension does not already exist. Open a command line and enter the line below, replacing the letters py with your extension.

grep 'py' /etc/mime.types

Analysing results

This particular command will output two lines. If your extension does not output any lines, or if the lines outputed do not include your extension, you must create a new MIME type. Otherwise, look at the right side of this output, find your file extension, and copy the corresponding text to the far right. For ‘py’, we must copy down ‘text/x-python’.

application/x-python-code                       pyc pyo
text/x-python                                   py

Creating the MIME type, if needed

If there was no output, or the output given did not include your extension, we must add a MIME type.

gksudo gedit /etc/mime.types

Modify the following text so that the word «extension» is replaced with your file extension (no period mark), add the line to the end of the mime.types file, and save.

text/extension                   extension

And copy the modified ‘text/extension’ part.

Another way to add a MIME Type

On 12.04LTS, I edited the /etc/mime.types file and it had no effect. What worked for me was to add xml files in /usr/share/mime/application; for example here’s a file (scad.xml) to describe scad files:

<?xml version="1.0" encoding="utf-8"?>
<mime-type xmlns="http://www.freedesktop.org/standards/shared-mime-info" type="application/scad">
  <glob pattern="*.scad"/>
</mime-type>

After you’ve added or modified whatever you need, run the command

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

Adding an Icon

Now we need to associate an icon with the MIME type. Get an SVG icon and name it «text-extension.svg», or whatever your modified MIME type is named; this will be the icon to represent all instances of the MIME type on your system. For our python example, this is a good choice. Rename the .svg file so that the it matches «text-x-python.svg» (or «insertYourMIMEtype.svg») so that the slashes are replaced with «-» and there are no capital letters.

Then simply run the following commands, with ‘text-x-python’ replaced with your MIME type.

sudo cp text-x-python.svg /usr/share/icons/gnome/scalable/mimetypes
sudo gtk-update-icon-cache /usr/share/icons/gnome/ -f

Relogin and all files ending in the MIME extension will display with that icon.

I’m developing a website that has a HTML5 video. It looks like when ogg files are served with other mime types than video/ogg firefox flips. I’ve updated the mime.types file inside the mamp/apache/config folder and have restarted the MAMP server multiple times, but ogg files are still served with plain/text context-type.

Can someone point out what I need to do?

UPDATE

The only time I’m able to change the mime type is if I change the DefaultType text/plain to DefaultType video/ogg which is stupid.

asked Jul 12, 2010 at 7:56

Roman's user avatar

RomanRoman

10.3k17 gold badges64 silver badges99 bronze badges

The mime.types file is in Applications/MAMP/conf/apache

There you can alter your mime types.
Reset server after changing the file for it to take effect.

answered Apr 8, 2013 at 9:32

Joeri's user avatar

JoeriJoeri

2,08821 silver badges23 bronze badges

1

Do you have mod_mime_magic installed and enabled? Is your mime magic working correctly? When you do file -m on one of your OGG files, does it say it is a plain text file? Are the file extensions correct?

answered Jul 12, 2010 at 8:06

Borealid's user avatar

BorealidBorealid

93.8k8 gold badges104 silver badges122 bronze badges

4

Adding mime-type is the same for all, it’s just you need to understand how the types will be applied and what’s the mime-type point is. In this answer I’ll how I tried to add WebAssembly mime-type application/wasm in MAMP.

Here it goes:

I recently tried WebAssembly in MAMP and configured out the below mime-type that works for me.

First, we need to navigate the folder of MAMP in MacOS’s Applications/MAMP/conf/apache, And then open the file mime.types in any editor.

After opening the file, add the following line anywhere in the mime.types file:

application/wasm wasm

Here is the example wasm mime type in my mime.types file:

#application/wasm
application/wasm wasm

answered Mar 23, 2021 at 18:18

Riyaz Khan's user avatar

Riyaz KhanRiyaz Khan

2,3202 gold badges14 silver badges28 bronze badges

In MAMP Pro these days you can add <VirtualHost> parameters using the gui. Here’s a screenshot showing how I add MIME types for .mjs and .wasm as:

AddType application/javascript      .mjs
AddType application/wasm      wasm

screenshot

Don’t forget to restart the server after adding.

answered Oct 12, 2021 at 15:23

Dan Poynor's user avatar

I’m developing a website that has a HTML5 video. It looks like when ogg files are served with other mime types than video/ogg firefox flips. I’ve updated the mime.types file inside the mamp/apache/config folder and have restarted the MAMP server multiple times, but ogg files are still served with plain/text context-type.

Can someone point out what I need to do?

UPDATE

The only time I’m able to change the mime type is if I change the DefaultType text/plain to DefaultType video/ogg which is stupid.

asked Jul 12, 2010 at 7:56

Roman's user avatar

RomanRoman

10.3k17 gold badges64 silver badges99 bronze badges

The mime.types file is in Applications/MAMP/conf/apache

There you can alter your mime types.
Reset server after changing the file for it to take effect.

answered Apr 8, 2013 at 9:32

Joeri's user avatar

JoeriJoeri

2,08821 silver badges23 bronze badges

1

Do you have mod_mime_magic installed and enabled? Is your mime magic working correctly? When you do file -m on one of your OGG files, does it say it is a plain text file? Are the file extensions correct?

answered Jul 12, 2010 at 8:06

Borealid's user avatar

BorealidBorealid

93.8k8 gold badges104 silver badges122 bronze badges

4

Adding mime-type is the same for all, it’s just you need to understand how the types will be applied and what’s the mime-type point is. In this answer I’ll how I tried to add WebAssembly mime-type application/wasm in MAMP.

Here it goes:

I recently tried WebAssembly in MAMP and configured out the below mime-type that works for me.

First, we need to navigate the folder of MAMP in MacOS’s Applications/MAMP/conf/apache, And then open the file mime.types in any editor.

After opening the file, add the following line anywhere in the mime.types file:

application/wasm wasm

Here is the example wasm mime type in my mime.types file:

#application/wasm
application/wasm wasm

answered Mar 23, 2021 at 18:18

Riyaz Khan's user avatar

Riyaz KhanRiyaz Khan

2,3202 gold badges14 silver badges28 bronze badges

In MAMP Pro these days you can add <VirtualHost> parameters using the gui. Here’s a screenshot showing how I add MIME types for .mjs and .wasm as:

AddType application/javascript      .mjs
AddType application/wasm      wasm

screenshot

Don’t forget to restart the server after adding.

answered Oct 12, 2021 at 15:23

Dan Poynor's user avatar

MIME (Multipurpose Internet Mail Extensions) — стандарт для определения типа передаваемой информации и механизмов ее обработки. Описан в следующих документах: RFC2045, RFC2046, RFC2047, RFC2048, RFC2049.

В основном, стандарт предназначен для идентификации нетекстовой информации. Тип информации определяется в следующем формате: type/subtype.

На данный момент существуют следующие зарегистрированные типы, используемые в системе WWW:

  • text — предназначен для описания всех «принципиально» текстовых форматов
  • image — предназначен для описания графических форматов
  • audio — предназначен для описания аудио форматов
  • video — предназначен для описания видео форматов
  • model — предназначен для описания форматов представления моделей (RFC2077)
  • application — предназначен для тех форматов, которые не подходят к другим категориям, а также для тех, которым необходима обработка сторонними приложениями

Более подробную информацию о зарегистрированных в IANA (The Internet Assigned Numbers Authority — www.iana.org) типах MIME можно получить по адресу: http://www.iana.org/assignments/media-types/.

Определение типов MIME используется для согласования работы веб-сервера и браузера.

Алгоритм следующий:

1. Клиент запрашивает в браузере документ по адресу http://www.zenon.net/index.html. Браузер делает запрос документа:

GET /index.html HTTP/1.1
Host: www.zenon.net

2. Сервер «знает», что /index.html является документом HTML и ему соответствует mime-type text/html. Об этом сервер сообщает в заголовках своего ответа браузеру в поле Content-Type:

HTTP/1.1 200 OK
Date: Wed, 27 Dec 2000 19:15:11 GMT
Server: Apache/1.3.14 (Unix) PHP/3.0.16 FrontPage/3.0.4.2
mod_perl/1.24 AuthMySQL/2.20
Cache-Control: max-age=1800
Expires: Wed, 27 Dec 2000 19:45:11 GMT
Connection: close
Content-Type: text/html; charset=windows-1251

3. Браузер, получая в поле Content-Type: значение text/html, использует соответствующий механизм отображения.

Для того, чтобы описанный механизм работал согласно Вашим потребностям, необходимо правильно настроить работу веб-сервера и веб-браузера.

Дальнейшее описание настроек веб-сервера будет приведено в соответствии с условиями предоставления услуг хостинга компанией Зенон Н.С.П. При работе по определенным типам тарифов некоторые возможности по настройке сервера могут быть недоступны, что будет оговариваться отдельно. Если Вы используете хостинг другого провайдера, проконсультируйтесь в соответствующей технической службе о тонкостях настроек.

Основные типы MIME описываются в конфигурационном файле mime.types, информацию из которого сервер считывает при запуске. Формат описания типов следующий:

   MIME-type extension [extension] ...

Пример описания видео формата MPEG:

   video/mpeg mpeg mpg mpe

В файле mime.types представлены форматы:

графические:

   image/bmp                       bmp
   image/gif                       gif
   image/jpeg                      jpeg jpg jpe
   image/png                       png
   image/tiff                      tiff tif

аудио:

   audio/basic                     au snd
   audio/midi                      mid midi kar
   audio/mpeg                      mpga mp2 mp3
   audio/x-pn-realaudio            ram rm
   audio/x-pn-realaudio-plugin     rpm
   audio/x-realaudio               ra
   audio/x-wav                     wav

видео:

   video/mpeg                      mpeg mpg mpe
   video/quicktime                 qt mov
   video/x-msvideo                 avi
   video/x-sgi-movie               movie

модели:

   model/vrml                      wrl vrml

текстовые:

   text/css                        css
   text/html                       html htm
   text/plain                      asc txt
   text/richtext                   rtx
   text/rtf                        rtf
   text/sgml                       sgml sgm
   text/xml                        xml

архивы:

   application/x-gtar              gtar
   application/x-tar               tar
   application/zip                 zip

приложения Microsoft:

   application/msword              doc
   application/vnd.ms-excel        xls
   application/vnd.ms-powerpoint   ppt

типы MIME для корректной работы wap:

   application/vnd.wap.wbxml       wbxml
   application/vnd.wap.wmlc        wmlc
   application/vnd.wap.wmlscriptc  wmlsc
   image/vnd.wap.wbmp              wbmp
   text/vnd.wap.wml                wml
   text/vnd.wap.wmlscript          wmls

другие приложения:

   application/octet-stream        bin dms lha exe class
   application/pdf                 pdf
   application/postscript          ai eps ps
   application/x-shockwave-flash   swf

В случае, если в приведенном выше списке Вы не нашли нужного Вам формата или Вам не подходят текущие описания форматов, Вы можете внести собственные изменения. Как это сделать и как воспользоваться возможностями, облегчающими выполнение некоторых задач, описано ниже.

Не для всех тарифов на хостинге «Зенон Н.С.П.» возможно изменение файла mime.types, а также и остальных конфигурационных файлов, использующихся при запуске web-сервера. Стандартный файл mime.types, используемый по умолчанию, можно посмотреть, зайдя в Unix-Shell
и выполнив команду:

less /www/conf/mime.types

Стоит также упомянуть о директивах, относящихся к типам MIME и используемых в httpd2.conf:

TypesConfig
— определяет полный или относительный путь к файлу mime.types. Путь по умолчанию: conf/mime.types. Формат директивы:

   TypesConfig path

DefaultType
— определяет тип MIME по умолчанию, то есть для тех файлов, тип которых не определен в файле mime.types. Значение по умолчанию — text/plain. Формат директивы:

   DefaultType MIME-type

Применение данной директивы удобно в тех случаях, когда на Вашем сервере возможно появление файлов с незарегистрированными расширениями. В таком случае для этого файла сервером будет выдаваться тип MIME согласно установкам директивы. Следует также отметить, что, если формат таких файлов не текстовый, то предпочтительнее устанавливать тип MIME application/octet-stream. В этом случае не будут производиться перекодировки, возможные при неправильном типе MIME, и пользователь получит файл в первоначальном виде.

Пример:

   DefaultType application/octet-stream

Директива DefaultType, как и все директивы, которые будут описаны далее, также может быть использована в файле .htaccess. Соответственно, они могут быть использованы при работе по любому тарифу.

AddType — добавление типа MIME для файлов с определенными расширениями. Может также использоваться для переопределения типа MIME для ранее описанных расширений. Формат директивы:

   AddType MIME-type extension [extension] ...

Директива используется как альтернатива изменения файла mime.types. Удобна в том случае, когда необходимо добавить новый или переопределить имеющийся тип MIME для файлов в определенной директории.

Например, в стандартном файле mime.types тип MIME application/x-gzip не связан ни с каким расширением, хотя стандартным расширением для файлов такого типа является gz. Эту ситуацию легко исправить, прописав в файле .htaccess директиву:

   AddType application/x-gzip .gz

До последнего времени ярким примером использования директивы являлось «включение поддержки протокола wap». Вся процедура состояла из добавления определенного набора типов MIME:

AddType text/vnd.wap.wml .wml
AddType application/vnd.wap.wmlc .wmlc
AddType text/vnd.wap.wmlscript .wmls
AddType application/vnd.wap.wmlscriptc .wmlsc
AddType image/vnd.wap.wbmp .wbmp

На данный момент все эти типы MIME уже присутствуют в стандартных настройках серверов по тарифам хостинга.

Еще один пример. Возможно возникновение ситуации, когда необходимо предоставить wap-доступ к какой-либо директории с находящимися в ней html-файлами. Для этого в файле .htaccess, находящемся в этой директории, прописывается директива:

   AddType text/vnd.wap.wml .html

После этого все файлы отдаются с корректным типом MIME. Следует учесть, что доступ к этой директории стандартными браузерами будет невозможен.

ForceType
— переопределение типа MIME для всех файлов в директории. Формат директивы:

   ForceType MIME-type

Пример: в какую-либо директорию нужно будет разместить текстовые файлы и не хотелось бы думать об их расширениях, а DefaultType установлен не text/plain. Для этой директории нужно установить:

   ForceType text/plain

RemoveType
— удаляет определение типа MIME для файлов с определенными расширениями. То есть для этих файлов тип MIME определяется согласно установкам директивы DefaultType. Формат директивы:

   RemoveType extension [extension] ...

В продолжение последнего примера для директивы AddType. После переопределения типа MIME для файлов с расширением html можно вернуть тип MIME для этих файлов комбинацией следующих директив:

RemoveType text/vnd.wap.wml .html
DefaultType text/html

Понравилась статья? Поделить с друзьями:
  • Как изменить microsoft edge на internet explorer
  • Как изменить message коммита
  • Как изменить memory limit
  • Как изменить memoji на айфоне свой стикер
  • Как изменить mediainfo видеофайла