001/* 002 * (C) Copyright 2006-2016 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Nuxeo - initial API and implementation 018 * 019 */ 020 021package org.nuxeo.ecm.platform.mimetype.detectors; 022 023import java.io.File; 024import java.io.FileInputStream; 025import java.io.FileNotFoundException; 026import java.io.IOException; 027import java.util.Map; 028 029import net.sf.jmimemagic.MagicDetector; 030 031import org.apache.commons.logging.Log; 032import org.apache.commons.logging.LogFactory; 033import org.apache.poi.hssf.usermodel.HSSFWorkbook; 034 035import org.nuxeo.common.utils.FileUtils; 036import org.nuxeo.runtime.api.Framework; 037 038public class XlsMimetypeSniffer implements MagicDetector { 039 040 private static final Log log = LogFactory.getLog(XlsMimetypeSniffer.class); 041 042 @Override 043 public String getDisplayName() { 044 return "XLS MimeType Detector"; 045 } 046 047 @Override 048 public String[] getHandledExtensions() { 049 return new String[] { "xls" }; 050 } 051 052 @Override 053 public String[] getHandledTypes() { 054 return new String[] { "application/vnd.ms-excel", "application/msexcel", "application/vnd.microsoft-excel" }; 055 } 056 057 @Override 058 public String getName() { 059 return "xlsdetector"; 060 } 061 062 @Override 063 public String getVersion() { 064 return "0.1"; 065 } 066 067 @Override 068 public String[] process(byte[] data, int offset, int length, long bitmask, char comparator, String mimeType, 069 Map params) { 070 071 String[] mimetypes = { "" }; 072 File file = null; 073 074 try { 075 file = Framework.createTempFile("magicdetector", ".xls"); 076 FileUtils.writeFile(file, data); 077 mimetypes = guessExcel(file); 078 } catch (IOException e) { 079 log.error(e, e); 080 } finally { 081 if (file != null) { 082 file.delete(); 083 } 084 } 085 086 return mimetypes; 087 } 088 089 @Override 090 public String[] process(File file, int offset, int length, long bitmask, char comparator, String mimeType, 091 Map params) { 092 093 return guessExcel(file); 094 } 095 096 public String[] guessExcel(File file) { 097 098 String[] mimetypes = {}; 099 100 try { 101 FileInputStream stream = new FileInputStream(file); 102 HSSFWorkbook workbook = new HSSFWorkbook(stream); 103 if (workbook.getNumberOfSheets() != 0) { 104 mimetypes = getHandledTypes(); 105 } 106 } catch (FileNotFoundException e) { 107 // This is not an excel file 108 log.debug("MimeType detector : Not an excel file"); 109 } catch (IOException e) { 110 // This is not an excel file 111 log.debug("MimeType detector : Not an excel file"); 112 } catch (IllegalArgumentException e) { 113 log.debug("MimeType detector : Not an excel file"); 114 } 115 116 return mimetypes; 117 } 118 119}