Get Mimetype

The Mimetype method of the FileType class seems to be a way to specify which file types your app uses. But can it be used to read in the mimetype of a file? i.e. If I use FolderItem with a .txt file I don’t want to rely on the file extension to determine file type, but rather I want to verify it based on the mimetype info.

Note that I am not using an OpenDialog to select the file

If this is a one shot (or one time), you can add a File Type Group^top the Navigation pane, then drop the file whose extension you want to know…
Easy, isn’t it ?

More thanks to Greg for disclosing this trick.

PS: when finished, you can delete it. Check the File Type (the by code version) is still here when you need it / before using it.

Not sure this will work, but you could try setting the mime type to text/plain and the extension to .*

Isn’t he looking for some magical tool that can detect the ACTUAL contents of a file instead of relying on an extension?

The “file” command line tool may be helpful if he’s on macOS:

File(1): no, but File: YES ! Check man File

For macOS the UTIs are relevant (Uniform Type Identifiers | Apple Developer Documentation) and not MimeTypes. Those I need for emails and now for message attachments. For all emails I check if the file really contains what it says. For messages the information is more reliable. But even there I have some checks like GIF files starting with “GIF87a” or “GIF89a”.

1 Like

Linux/Unix:

file -i /path/to/the/file.whatever

Inspects the file and return the most probable mime type of a file.

For Windows, I’ve compiled a compatible pack here with all dependencies:

file-cmd.zip (473.2 KB)

But you will need to inform where the “magic” is using -m

/path/to/file.exe -m /path/to/the/file/magic -i /path/to/the/file.whatever

thanks everyone. this isn’t a once off, in short i don’t want to trust the file extension in case malware sneaks in (this is not my only protection mechanism)

@Rick_Araujo when you say that you’ve compiled a pack, what does that mean exactly? i see your zip file has binary content, so wondering where the code from that comes from

my goal was to try and use native code was i was hopefully when I saw the FileType class! but Xojo doesn’t seem to have the capability

The GNUWin project has a lot of sparse contents, bins in one place, libs in another, dependencies in another, etc. I managed to get the minimal set able to make file.exe packed together and working.

Here are the sources: GnuWin - Browse /file at SourceForge.net

1 Like