Forums | Mahara Community

Developers /
Plugin mime-type issues


Geoff Rowland's profile picture
Posts: 108

08 May 2011, 2:23 PM

Hi folks

We are putting the finishing touches on some Mahara artefact-files plugins, but have encountered some issues with handling the appropriate file mime-types.

The simplest case is a plugin for embedding FreeMind mind-maps with file extension .mm (application/x-freemind). Different client operating system/browser combinations seem to forward different mime-types (as I recollect, I have see text/html, text/xml and application/x-troff) on file upload. So, it can be tricky to display .mm files in the file picker.

http://freemind.sourceforge.net/wiki/index.php/Main_Page

The second set of plugins are to display 2D and 3D chemical structures using Jmol and related technologies. These can potentially use a whole raft of  esoteric chemical mime-types including .cml chemical/x-cml, .cif chemical/x-cif, .mol, chemical/mdl-molfile, .pdb chemical/x-pdb, .xyz chemical/x-xyz.

Although this can be fixed by the correct configuration at the client, most users will have neither the technical expertise nor the permissions (on an institutional PC) to do this. Therefore, I think a fix nneeds to be applied at the Mahara (server) end of things.

I see, from community postings and bug fixes, that similar issues regarding video file formats and compressed file formats have already been dealt with. However, these seem to involve correcting the generic mime-type application/octet-stream

In the cases above, Mahara is receiving an incorrect mime-type. So, would it be appropriate that for certain file suffixes (.mm, .cif, .pdb etc) Mahara could override the client-sent mime-type? I presume PHP mime_content_type or finfo can be used to get the correct mime-types if they are correctly configured on the server - e.g. through AddType in httpd.conf or direct editing of mime.types.

Before embarking on a hack of, say, /artefact/files/lib.php, I would be grateful for any comments or suggestions as to the best way to do this.

Thanks

Geoff

PS I think I have already grasped how to use /artefact/file/filetypes.xml (and tweaking /artefact/file/version.php) to add file extensions/mime types to the Mahara artefact_file_mime_types database table. It is just that the incorrect mime-types of uploaded files don't match those in the table.

anonymous profile picture
Deleted user
Posts: 808

08 May 2011, 5:24 PM

Geoff,

You should probably just hack artefact/file/lib.php (and all the other places that call file_mime_type()) so that the client mimetype is always overridden, and see how you get on.

I'm not sure if doing this in core mahara would cause problems for some people or not.  If it's likely to cause problems maybe we could do it with a setting in the file artefact plugin config in the admin area.

R.

Geoff Rowland's profile picture
Posts: 108

11 May 2011, 2:39 PM

Thanks Richard

For the moment I have tweaked our artefact/file/lib.php as below. Obviously, the switch statement can be easily extended for additional file extension/mime-types. Seems to work for us though I appreciate it might cause problems for others if they use the same file extensions for different types of file. Open to suggestions of better ways to do this.

Immediately after

         if ($um->file['type'] == 'application/octet-stream') {
            // the browser wasn't sure, so use file_mime_type to guess
            require_once('file.php');
            $data->filetype = file_mime_type($um->file['tmp_name']);
        }
        else {
            $data->filetype = $um->file['type'];
        }
I have added
         // The browser may have been wrong, so use file extension to force some mime-types.////
        require_once('file.php'); 
        switch ($um->original_filename_extension()) {
            case 'mm': $data->filetype = 'application/x-freemind';
            break;
            case 'cif': $data->filetype = 'chemical/x-cif';
            break;
            case 'cml': $data->filetype = 'chemical/x-cml';
            break;
            case 'mol': $data->filetype = 'chemical/x-mdl-molfile';
            break;
            case 'pdb': $data->filetype = 'chemical/x-pdb';
            break;
            case 'sdf': $data->filetype = 'chemical/x-mdl-sdfile';
            break;
            case 'xyz': $data->filetype = 'chemical/x-xyz';
            break;
        }
Hope to release the full code for the plugins in the next few days
Geoff
Geoff Rowland's profile picture
Posts: 108

29 November 2012, 9:27 AM

Recently noticed that this needed changing for Mahara 1.6.2 (presumably, any 1.6.x). Quite tricky to spot as only causes a problem if a particular OS/browser combination reports an incorrect mime-type for an uploaded time.

The following code works, though could probably be made more elegant

In /artefact/file/lib.php

immediately after

       if (empty($data->filetype) || $data->filetype == 'application/octet-stream') {
           $data->filetype = $data->guess;
       }

add

       // The browser may have been wrong, so use file extension to force some mime-types.////
       $ext = $data->oldextension;
       switch ($ext) {
           case 'mm': $data->filetype = 'application/x-freemind';
           break;
           case 'alc': $data->filetype = 'chemical/x-alchemy';
           break;
           case 'cif': $data->filetype = 'chemical/x-cif';
           break;
           case 'cml': $data->filetype = 'chemical/x-cml';
           break;
           case 'hin': $data->filetype = 'chemical/x-hin';
           break;
           case 'mcif': $data->filetype = 'chemical/x-mmcif';
           break;
           case 'mol': $data->filetype = 'chemical/x-mdl-molfile';
           break;
           case 'mol2': $data->filetype = 'chemical/x-mol2';
           break;
           case 'pdb': $data->filetype = 'chemical/x-pdb';
           break;
           case 'sdf': $data->filetype = 'chemical/x-mdl-sdfile';
           break;
           case 'xyz': $data->filetype = 'chemical/x-xyz';
           break;
      }

4 results