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 org.apache.commons.io.FileUtils;
030import org.apache.commons.logging.Log;
031import org.apache.commons.logging.LogFactory;
032import org.apache.poi.hssf.usermodel.HSSFWorkbook;
033import org.nuxeo.runtime.api.Framework;
034
035import net.sf.jmimemagic.MagicDetector;
036
037public class XlsMimetypeSniffer implements MagicDetector {
038
039    private static final Log log = LogFactory.getLog(XlsMimetypeSniffer.class);
040
041    @Override
042    public String getDisplayName() {
043        return "XLS MimeType Detector";
044    }
045
046    @Override
047    public String[] getHandledExtensions() {
048        return new String[] { "xls" };
049    }
050
051    @Override
052    public String[] getHandledTypes() {
053        return new String[] { "application/vnd.ms-excel", "application/msexcel", "application/vnd.microsoft-excel" };
054    }
055
056    @Override
057    public String getName() {
058        return "xlsdetector";
059    }
060
061    @Override
062    public String getVersion() {
063        return "0.1";
064    }
065
066    @Override
067    public String[] process(byte[] data, int offset, int length, long bitmask, char comparator, String mimeType,
068            @SuppressWarnings("rawtypes") Map params) {
069
070        String[] mimetypes = { "" };
071        File file = null;
072
073        try {
074            file = Framework.createTempFile("magicdetector", ".xls");
075            FileUtils.writeByteArrayToFile(file, data);
076            mimetypes = guessExcel(file);
077        } catch (IOException e) {
078            log.error(e, e);
079        } finally {
080            if (file != null) {
081                file.delete();
082            }
083        }
084
085        return mimetypes;
086    }
087
088    @Override
089    public String[] process(File file, int offset, int length, long bitmask, char comparator, String mimeType,
090            @SuppressWarnings("rawtypes") Map params) {
091
092        return guessExcel(file);
093    }
094
095    public String[] guessExcel(File file) {
096
097        String[] mimetypes = {};
098
099        try (FileInputStream stream = new FileInputStream(file); //
100                HSSFWorkbook workbook = new HSSFWorkbook(stream)) {
101            if (workbook.getNumberOfSheets() != 0) {
102                mimetypes = getHandledTypes();
103            }
104        } catch (FileNotFoundException e) {
105            // This is not an excel file
106            log.debug("MimeType detector : Not an excel file");
107        } catch (IOException e) {
108            // This is not an excel file
109            log.debug("MimeType detector : Not an excel file");
110        } catch (IllegalArgumentException e) {
111            log.debug("MimeType detector : Not an excel file");
112        }
113
114        return mimetypes;
115    }
116
117}