Custom Function - GetFileMimeType

Post any questions you have about using the Verj.io Studio, including client and server-side programming with Javascript or FPL, and integration with databases, web services etc.

Moderators: Jon, Steve, Ian, Dave

ehmd
Ebase User
Posts: 53
Joined: Thu Sep 13, 2007 9:02 am
Contact:

Custom Function - GetFileMimeType

#1

Postby ehmd » Fri Mar 01, 2013 8:37 am

Custom Function to get a files MimeType.
Useful to check any uploaded files are of the correct file type and matches the file extension. e.g. to stop someone renaming an executable file as a .jpg

Code: Select all

package com.ebasetech.ufs.customfunctions; 

import java.util.*;
import org.nfunk.jep.*;
import com.ebasetech.ufs.function.*;
import com.ebasetech.ufs.kernel.*;
import java.io.File;
import org.apache.tika.Tika;
import org.apache.tika.detect.DefaultDetector;
import org.apache.tika.detect.Detector;
import org.apache.tika.io.TikaInputStream;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.mime.MimeTypes;

public class GetFileMimeType extends UFSCustomFunction {

    public GetFileMimeType (UFSFormInterface formInterface) {
	super(formInterface);

        /* Specify the number of parameters to be received by the custom function */

	numberOfParameters = 1;
	}

    public void run(Stack inStack) throws ParseException {

        boolean fileExists = false;
        String mimeType = null;
        
        Object param1 = inStack.pop();

		if ( param1 instanceof String ) {
            
                String fileName = param1.toString();
                
                if ( fileName.length() == 0) {
                        inStack.push(new String("ERROR"));
                        return;                        
                }
                
                try {
                    fileExists = (new File(fileName)).exists();
                    if ( fileExists ) {
                        
                        File f = new File(fileName);
                        
                        Tika tika = null;
                        TikaInputStream is = null;
	 
                        // Creating new Instance of org.apache.tika.Tika
                        tika = new Tika();
	 
                        // Detecting MIME Type of the File
                        mimeType = tika.detect(f);
                        
                        inStack.push(new String(mimeType));
                        return;                        
                        
                        
                    } else {
                        inStack.push(new String("NOFILE"));
                        return;
                    }
                    
                } catch ( Exception e ) {
                    inStack.push(new String("ERROR"));
                    return;                   
                } 
                
        } else {
                inStack.push(new String("ERROR"));
                return;             
        }

        
    }

    
}
Uses Apache Tika from http://tika.apache.org
0 x

Jon
Moderator
Moderator
Posts: 1342
Joined: Wed Sep 12, 2007 12:49 pm

#2

Postby Jon » Tue Mar 05, 2013 4:52 pm

Here is the corresponding function written in Javascript:

Code: Select all

function getMimeTypeForFile(path)
{
	var f = new java.io.File(path);
	if (f.exists())
	{
		var tika = new org.apache.tika.Tika();
		return tika.detect(f);
	}
	return null;
}
Note to do this, you need to add the apache tika jar file to WEB-INF/lib on the server.

With Java 1.7, this can be done more easily (i.e. without apache tika) using the Files.probeContentType() method.
0 x

Jeramy1Patraw

#3

Postby Jeramy1Patraw » Sat Nov 22, 2014 3:19 am

If you have noticed the walk through metal detector when we passed through it,it will give an alarm if metallic objects were detected. And are you curious about the principle of it ?Then you can come to Detectorall to have a look.
0 x


Who is online

Users browsing this forum: No registered users and 7 guests