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