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