Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Path extension and MIME type of file in swift

Tags:

I'm learning swift , and I made a table with cells that show an image ,and classes in groups by MIME TYPE (using path extension). I had a question about extensions. If the images (for example, it could be a video or a pdf) are taken from internet , i'm not sure if they have the extension or not how can I get the MIMEtype of a file inside my file System without using path extension ? PS :Sorry for my bad english it's not my native language

like image 207
kholl Avatar asked Jul 06 '15 10:07

kholl


People also ask

What is MIME type and file extension?

The abbreviation MIME stands for Multi-purpose Internet Mail Extensions. MIME types form a standard way of classifying file types on the internet. First, let's have a look at a common MIME type for an example: text/html. A MIME type consists of two parts: a type and a subtype.

Is MIME type same as extension?

Slightly longer answer: Mime types and file extensions provide hints to how to deal with a file. Whereas file extensions are commonly used for your OS to decide what program to open a file with, Mime types are used by your browser to decide how to present some data (or the server on how to interpret received data).

How do I get MIME type extension?

string mimeType = MimeMapping. GetMimeMapping(fileName); If you need to add custom mappings you probably can use reflection to add mappings to the BCL MimeMapping class, it uses a custom dictionary that exposes this method, so you should invoke the following to add mappings (never tested tho, but should prob. work).

Where is MIME type stored in a file?

All MIME type information is stored in a database. The MIME database is located in the directory /usr/share/mime/ . The MIME database contains a large number of common MIME types, stored in the file /usr/share/mime/packages/freedesktop.


1 Answers

If anyone wants to get the MimeType from the actual URL of the file this code worked for me:

import MobileCoreServices  func mimeTypeForPath(path: String) -> String {     let url = NSURL(fileURLWithPath: path)     let pathExtension = url.pathExtension      if let uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, pathExtension! as NSString, nil)?.takeRetainedValue() {         if let mimetype = UTTypeCopyPreferredTagWithClass(uti, kUTTagClassMIMEType)?.takeRetainedValue() {             return mimetype as String         }     }     return "application/octet-stream" } 
like image 150
Paul Lehn Avatar answered Sep 18 '22 12:09

Paul Lehn