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