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