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 */ 019package org.nuxeo.ecm.platform.mimetype.detectors; 020 021import static java.nio.charset.StandardCharsets.UTF_8; 022 023import java.io.File; 024import java.io.IOException; 025import java.util.Map; 026import java.util.zip.ZipEntry; 027import java.util.zip.ZipFile; 028 029import org.apache.commons.io.FileUtils; 030import org.apache.commons.logging.Log; 031import org.apache.commons.logging.LogFactory; 032import org.nuxeo.common.utils.ZipUtils; 033import org.nuxeo.runtime.api.Framework; 034 035import net.sf.jmimemagic.MagicDetector; 036 037public class OOoMimetypeSniffer implements MagicDetector { 038 039 private static final Log log = LogFactory.getLog(OOoMimetypeSniffer.class); 040 041 @Override 042 public String getDisplayName() { 043 return "OOo 1.x & OpenDocument MimeType Detector"; 044 } 045 046 @Override 047 public String[] getHandledExtensions() { 048 return new String[] { "ods", "ots", "odt", "ott", "odp", "otp", "odg", "otg", "otm", "oth", "odi", "oti", "odf", 049 "otf", "odc", "otc", "sxw", "stw", "sxg", "sxc", "stc", "sxi", "sti", "sxd", "std", "sxm", }; 050 } 051 052 @Override 053 public String[] getHandledTypes() { 054 return new String[] { "application/vnd.oasis.opendocument.spreadsheet", 055 "application/vnd.oasis.opendocument.spreadsheet-template", "application/vnd.oasis.opendocument.text", 056 "application/vnd.oasis.opendocument.text-template", "application/vnd.oasis.opendocument.presentation", 057 "application/vnd.oasis.opendocument.presentation-template", 058 "application/vnd.oasis.opendocument.graphics", "application/vnd.oasis.opendocument.graphics-template", 059 "application/vnd.oasis.opendocument.text-master", "application/vnd.oasis.opendocument.text-web", 060 "application/vnd.oasis.opendocument.image", "application/vnd.oasis.opendocument.image-template", 061 "application/vnd.oasis.opendocument.formula", "application/vnd.oasis.opendocument.formula-template", 062 "application/vnd.oasis.opendocument.chart", "application/vnd.oasis.opendocument.chart-template", 063 // OOo 1.x file format 064 "application/vnd.sun.xml.writer", "application/vnd.sun.xml.writer.template", 065 "application/vnd.sun.xml.writer.global", "application/vnd.sun.xml.calc", 066 "application/vnd.sun.xml.calc.template", "application/vnd.sun.xml.impress", 067 "application/vnd.sun.xml.impress.template", "application/vnd.sun.xml.draw", 068 "application/vnd.sun.xml.draw.template", "application/vnd.sun.xml.math", }; 069 } 070 071 @Override 072 public String getName() { 073 return "ooodetector"; 074 } 075 076 @Override 077 public String getVersion() { 078 return "0.2"; 079 } 080 081 @Override 082 public String[] process(byte[] data, int offset, int length, long bitmask, char comparator, String mimeType, 083 @SuppressWarnings("rawtypes") Map params) { 084 String[] mimetypes = {}; 085 File file = null; 086 try { 087 file = Framework.createTempFile("magicdetector", ".xml"); 088 FileUtils.writeByteArrayToFile(file, data); 089 mimetypes = guessOOo(file); 090 } catch (IOException e) { 091 log.error(e); 092 } finally { 093 if (file != null) { 094 file.delete(); 095 } 096 } 097 return mimetypes; 098 } 099 100 @Override 101 public String[] process(File file, int offset, int length, long bitmask, char comparator, String mimeType, 102 @SuppressWarnings("rawtypes") Map params) { 103 return guessOOo(file); 104 } 105 106 public String[] guessOOo(File file) { 107 108 String[] mimetype = {}; 109 File tempFile = null; 110 111 try (ZipFile zip = new ZipFile(file)) { 112 ZipEntry entry = zip.getEntry("mimetype"); 113 114 if (entry != null) { 115 // we have an opendocument so lets unzip 116 117 // unzip file to process xml content 118 tempFile = Framework.createTempFile("nxMimeTypeDetector_", ".dir"); 119 tempFile.delete(); // to be able to create a dir under this name 120 if (!tempFile.isDirectory()) { 121 tempFile.mkdir(); 122 } 123 ZipUtils.unzip(file, tempFile); 124 125 // retrieves mimetypefile 126 String path = tempFile.getAbsolutePath(); 127 path += File.separator + "mimetype"; 128 File mimetypeFile = new File(path); 129 mimetype = new String[] { FileUtils.readFileToString(mimetypeFile, UTF_8) }; 130 } 131 } catch (IOException e) { 132 // probably not a zip file 133 } finally { 134 if (tempFile != null) { 135 FileUtils.deleteQuietly(tempFile); 136 } 137 } 138 139 return mimetype; 140 } 141 142}