001/*
002 * (C) Copyright 2006-2007 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 *
017 * $Id: PptMimetypeSniffer.java 20310 2007-06-11 15:54:14Z lgodard $
018 */
019
020package org.nuxeo.ecm.platform.mimetype.detectors;
021
022import java.io.File;
023import java.io.FileInputStream;
024import java.io.FileNotFoundException;
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;
034import org.nuxeo.common.utils.FileUtils;
035
036public class PptMimetypeSniffer implements MagicDetector {
037
038    private static final Log log = LogFactory.getLog(PptMimetypeSniffer.class);
039
040    public String getDisplayName() {
041        return "PPT MimeType Detector";
042    }
043
044    public String[] getHandledExtensions() {
045        return new String[] { "ppt", "pps" };
046    }
047
048    public String[] getHandledTypes() {
049        return new String[] { "application/vnd.ms-powerpoint" };
050    }
051
052    public String getName() {
053        return "pptdetector";
054    }
055
056    public String getVersion() {
057        return "0.1";
058    }
059
060    public String[] process(byte[] data, int offset, int length, long bitmask, char comparator, String mimeType,
061            Map params) {
062
063        String[] mimetypes = { "" };
064        File file = null;
065
066        try {
067            file = File.createTempFile("magicdetector", ".ppt");
068            FileUtils.writeFile(file, data);
069            mimetypes = guessPowerpoint(file);
070        } catch (IOException e) {
071            log.error(e, e);
072        } finally {
073            if (file != null) {
074                file.delete();
075            }
076        }
077
078        return mimetypes;
079    }
080
081    public String[] process(File file, int offset, int length, long bitmask, char comparator, String mimeType,
082            Map params) {
083
084        return guessPowerpoint(file);
085    }
086
087    public String[] guessPowerpoint(File file) {
088
089        String[] mimetypes = {};
090
091        try {
092            FileInputStream stream = new FileInputStream(file);
093            HSLFSlideShow ppt = new HSLFSlideShow(stream);
094            SlideShow presentation = new SlideShow(ppt);
095
096            if (presentation.getSlides().length != 0) {
097                mimetypes = getHandledTypes();
098            }
099        } catch (FileNotFoundException e) {
100            // This is not powerpoint file
101            log.debug("MimeType detector : Not a powerpoint file - FileNotFoundException");
102        } catch (IOException e) {
103            // This is not a powerpoint file
104            log.debug("MimeType detector : Not a powerpoint file - IOException");
105        } catch (RuntimeException e) {
106            // This is not a powerpoint file
107            log.debug("MimeType detector : Not a powerpoint file - RuntimeException");
108        }
109        return mimetypes;
110    }
111
112}