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