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.IOException;
026import java.util.Map;
027
028import org.apache.commons.io.FileUtils;
029import org.apache.commons.logging.Log;
030import org.apache.commons.logging.LogFactory;
031import org.apache.poi.hslf.usermodel.HSLFSlideShow;
032import org.nuxeo.runtime.api.Framework;
033
034import net.sf.jmimemagic.MagicDetector;
035
036public class PptMimetypeSniffer implements MagicDetector {
037
038    private static final Log log = LogFactory.getLog(PptMimetypeSniffer.class);
039
040    @Override
041    public String getDisplayName() {
042        return "PPT MimeType Detector";
043    }
044
045    @Override
046    public String[] getHandledExtensions() {
047        return new String[] { "ppt", "pps" };
048    }
049
050    @Override
051    public String[] getHandledTypes() {
052        return new String[] { "application/vnd.ms-powerpoint" };
053    }
054
055    @Override
056    public String getName() {
057        return "pptdetector";
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
069        String[] mimetypes = { "" };
070        File file = null;
071
072        try {
073            file = Framework.createTempFile("magicdetector", ".ppt");
074            FileUtils.writeByteArrayToFile(file, data);
075            mimetypes = guessPowerpoint(file);
076        } catch (IOException e) {
077            log.error(e, e);
078        } finally {
079            if (file != null) {
080                file.delete();
081            }
082        }
083
084        return mimetypes;
085    }
086
087    @Override
088    public String[] process(File file, int offset, int length, long bitmask, char comparator, String mimeType,
089            @SuppressWarnings("rawtypes") Map params) {
090
091        return guessPowerpoint(file);
092    }
093
094    public String[] guessPowerpoint(File file) {
095        String[] mimetypes = {};
096        try {
097            try (FileInputStream stream = new FileInputStream(file)) {
098                try (HSLFSlideShow ppt = new HSLFSlideShow(stream)) {
099                    if (ppt.getSlides().size() != 0) {
100                        mimetypes = getHandledTypes();
101                    }
102                }
103            }
104        } catch (IOException | RuntimeException e) {
105            log.debug("MimeType detector: Not a PowerPoint file", e);
106        }
107        return mimetypes;
108    }
109
110}