Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to obtain a file UTI when the file has no extension?

Is there a way to figure out the file UTI (uniform type identifier) even if there is no file extension?

Right now I obtain it like this, when the file has an extension:

CFStringRef fileUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (CFStringRef)[file pathExtension], NULL);

I've found this snippet on the net which aims to do it on the Mac. But I am in iOS:

CFStringRef typeString = UTCreateStringForOSType(outInfo.filetype);
itemUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, typeString, NULL);
CFRelease( typeString );

Note: outInfo is of type LSItemInfoRecord which does not exist in iOS.

like image 832
dontWatchMyProfile Avatar asked Nov 14 '22 22:11

dontWatchMyProfile


1 Answers

The only way to identify a file that has no extension and no other metadata is to look at its contents and identify the file based on that.

If it's an image file then it usually starts with some magic string that identifies it as a JPEG, PNG, etc. You could also just try to open it as an image and see if it works.

For a text file though, that approach won't work as text files can contain any content. If you have some idea about what text is in the file you could just open it as a string and then search the string for that text. If not, you could just look for words from a dictionary and then assume that it's text if it contains a reasonable number of dictionary words.

You might be better off just asking the user to identify the file type if there's no extension though - it's easier and more reliable.

like image 191
Nick Lockwood Avatar answered Dec 27 '22 18:12

Nick Lockwood