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 */
019package org.nuxeo.ecm.platform.mimetype.detectors;
020
021import java.io.File;
022import java.io.IOException;
023import java.util.Map;
024import java.util.zip.ZipEntry;
025import java.util.zip.ZipFile;
026
027import org.apache.commons.io.FileUtils;
028import org.apache.commons.logging.Log;
029import org.apache.commons.logging.LogFactory;
030import org.nuxeo.common.utils.ZipUtils;
031import org.nuxeo.runtime.api.Framework;
032
033import net.sf.jmimemagic.MagicDetector;
034
035public class OOoMimetypeSniffer implements MagicDetector {
036
037    private static final Log log = LogFactory.getLog(OOoMimetypeSniffer.class);
038
039    @Override
040    public String getDisplayName() {
041        return "OOo 1.x & OpenDocument MimeType Detector";
042    }
043
044    @Override
045    public String[] getHandledExtensions() {
046        return new String[] { "ods", "ots", "odt", "ott", "odp", "otp", "odg", "otg", "otm", "oth", "odi", "oti", "odf",
047                "otf", "odc", "otc", "sxw", "stw", "sxg", "sxc", "stc", "sxi", "sti", "sxd", "std", "sxm", };
048    }
049
050    @Override
051    public String[] getHandledTypes() {
052        return new String[] { "application/vnd.oasis.opendocument.spreadsheet",
053                "application/vnd.oasis.opendocument.spreadsheet-template", "application/vnd.oasis.opendocument.text",
054                "application/vnd.oasis.opendocument.text-template", "application/vnd.oasis.opendocument.presentation",
055                "application/vnd.oasis.opendocument.presentation-template",
056                "application/vnd.oasis.opendocument.graphics", "application/vnd.oasis.opendocument.graphics-template",
057                "application/vnd.oasis.opendocument.text-master", "application/vnd.oasis.opendocument.text-web",
058                "application/vnd.oasis.opendocument.image", "application/vnd.oasis.opendocument.image-template",
059                "application/vnd.oasis.opendocument.formula", "application/vnd.oasis.opendocument.formula-template",
060                "application/vnd.oasis.opendocument.chart", "application/vnd.oasis.opendocument.chart-template",
061                // OOo 1.x file format
062                "application/vnd.sun.xml.writer", "application/vnd.sun.xml.writer.template",
063                "application/vnd.sun.xml.writer.global", "application/vnd.sun.xml.calc",
064                "application/vnd.sun.xml.calc.template", "application/vnd.sun.xml.impress",
065                "application/vnd.sun.xml.impress.template", "application/vnd.sun.xml.draw",
066                "application/vnd.sun.xml.draw.template", "application/vnd.sun.xml.math", };
067    }
068
069    @Override
070    public String getName() {
071        return "ooodetector";
072    }
073
074    @Override
075    public String getVersion() {
076        return "0.2";
077    }
078
079    @Override
080    public String[] process(byte[] data, int offset, int length, long bitmask, char comparator, String mimeType,
081            Map params) {
082        String[] mimetypes = {};
083        File file = null;
084        try {
085            file = Framework.createTempFile("magicdetector", ".xml");
086            FileUtils.writeByteArrayToFile(file, data);
087            mimetypes = guessOOo(file);
088        } catch (IOException e) {
089            log.error(e);
090        } finally {
091            if (file != null) {
092                file.delete();
093            }
094        }
095        return mimetypes;
096    }
097
098    @Override
099    public String[] process(File file, int offset, int length, long bitmask, char comparator, String mimeType,
100            Map params) {
101        return guessOOo(file);
102    }
103
104    public String[] guessOOo(File file) {
105
106        String[] mimetype = {};
107        File tempFile = null;
108
109        try {
110            ZipFile zip = new ZipFile(file);
111            ZipEntry entry = zip.getEntry("mimetype");
112
113            if (entry != null) {
114                // we have an opendocument so lets unzip
115
116                // unzip file to process xml content
117                tempFile = Framework.createTempFile("nxMimeTypeDetector_", ".dir");
118                tempFile.delete(); // to be able to create a dir under this name
119                if (!tempFile.isDirectory()) {
120                    tempFile.mkdir();
121                }
122                ZipUtils.unzip(file, tempFile);
123
124                // retrieves mimetypefile
125                String path = tempFile.getAbsolutePath();
126                path += File.separator + "mimetype";
127                File mimetypeFile = new File(path);
128                mimetype = new String[] { FileUtils.readFileToString(mimetypeFile) };
129            }
130        } catch (IOException e) {
131            // probably not a zip file
132        } finally {
133            if (tempFile != null) {
134                FileUtils.deleteQuietly(tempFile);
135            }
136        }
137
138        return mimetype;
139    }
140
141}