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;
026import java.util.zip.ZipFile;
027
028import net.sf.jmimemagic.MagicDetector;
029
030import org.apache.commons.logging.Log;
031import org.apache.commons.logging.LogFactory;
032
033import org.nuxeo.common.utils.FileUtils;
034import org.nuxeo.runtime.api.Framework;
035
036public class MsoXML2007MimetypeSniffer implements MagicDetector {
037
038    private static final Log log = LogFactory.getLog(MsoXML2007MimetypeSniffer.class);
039
040    @Override
041    public String getDisplayName() {
042        return "Microsoft 2007 files MimeType Detector";
043    }
044
045    @Override
046    public String[] getHandledExtensions() {
047        return new String[] { "docm", "docx", "dotm", "dotx", "ppsm", "ppsx", "pptm", "pptx", "xlsb", "xlsm", "xlsx",
048                "xps" };
049    }
050
051    @Override
052    public String[] getHandledTypes() {
053        return new String[] { "application/vnd.ms-word.document.macroEnabled.12",
054                "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
055                "application/vnd.ms-word.template.macroEnabled.12",
056                "application/vnd.openxmlformats-officedocument.wordprocessingml.template",
057                "application/vnd.ms-powerpoint.slideshow.macroEnabled.12",
058                "application/vnd.openxmlformats-officedocument.presentationml.slideshow",
059                "application/vnd.ms-powerpoint.presentation.macroEnabled.12",
060                "application/vnd.openxmlformats-officedocument.presentationml.presentation",
061                "application/vnd.ms-excel.sheet.binary.macroEnabled.12",
062                "application/vnd.ms-excel.sheet.macroEnabled.12",
063                "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "application/vnd.ms-xpsdocument", };
064    }
065
066    @Override
067    public String getName() {
068        return "mso2007detector";
069    }
070
071    @Override
072    public String getVersion() {
073        return "0.1";
074    }
075
076    @Override
077    public String[] process(byte[] data, int offset, int length, long bitmask, char comparator, String mimeType,
078            Map params) {
079        String[] mimetypes = {};
080        File file = null;
081        try {
082            file = Framework.createTempFile("magicdetector", ".xml");
083            FileUtils.writeFile(file, data);
084            mimetypes = guessMsoXML2007(file);
085        } catch (IOException e) {
086            log.error(e);
087        } finally {
088            if (file != null) {
089                file.delete();
090            }
091        }
092        return mimetypes;
093    }
094
095    @Override
096    public String[] process(File file, int offset, int length, long bitmask, char comparator, String mimeType,
097            Map params) {
098        return guessMsoXML2007(file);
099    }
100
101    public String[] guessMsoXML2007(File file) {
102        String[] mimetype = {};
103        try {
104            // unzip
105            ZipFile zip = new ZipFile(file);
106            // look at mimetype
107        } catch (IOException e) {
108            log.error(e, e);
109        }
110        return mimetype;
111    }
112
113}