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 static java.nio.charset.StandardCharsets.UTF_8;
024
025import java.io.File;
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.nuxeo.runtime.api.Framework;
033
034import net.sf.jmimemagic.MagicDetector;
035
036public class MsoXmlMimetypeSniffer implements MagicDetector {
037
038    private static final Log log = LogFactory.getLog(MsoXmlMimetypeSniffer.class);
039
040    @Override
041    public String getDisplayName() {
042        return "XML Microsoft 2003 MimeType Detector";
043    }
044
045    @Override
046    public String[] getHandledExtensions() {
047        return new String[] { "xml" };
048    }
049
050    @Override
051    public String[] getHandledTypes() {
052        return new String[] { "application/vnd.ms-excel", "application/msword", };
053    }
054
055    @Override
056    public String getName() {
057        return "msoxml2003detector";
058    }
059
060    @Override
061    public String getVersion() {
062        return "0.1";
063    }
064
065    @Override
066    public String[] process(byte[] data, int offset, int length, long bitmask, char comparator, String mimeType,
067            @SuppressWarnings("rawtypes") Map params) {
068        String[] mimetypes = {};
069        File file = null;
070        try {
071            file = Framework.createTempFile("magicdetector", ".xml");
072            FileUtils.writeByteArrayToFile(file, data);
073            mimetypes = guessMsoXml(file);
074        } catch (IOException e) {
075            log.error(e);
076        } finally {
077            if (file != null) {
078                file.delete();
079            }
080        }
081        return mimetypes;
082    }
083
084    @Override
085    public String[] process(File file, int offset, int length, long bitmask, char comparator, String mimeType,
086            @SuppressWarnings("rawtypes") Map params) {
087        return guessMsoXml(file);
088    }
089
090    public String[] guessMsoXml(File file) {
091        String[] mimetype = {};
092        try {
093            String content = FileUtils.readFileToString(file, UTF_8);
094            if (content.contains("<?mso-application progid=\"Word.Document\"?>")) {
095                String[] type = { getHandledTypes()[1] };
096                mimetype = type;
097            } else {
098                if (content.contains("<?mso-application progid=\"Excel.Sheet\"?>")) {
099                    String[] type = { getHandledTypes()[0] };
100                    mimetype = type;
101                }
102            }
103        } catch (IOException e) {
104            log.error(e);
105        }
106        return mimetype;
107    }
108
109}