001/* 002 * (C) Copyright 2006-2018 Nuxeo (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 */ 020package org.nuxeo.ecm.platform.convert.plugins; 021 022import java.io.File; 023import java.io.IOException; 024import java.util.ArrayList; 025import java.util.List; 026 027import org.apache.commons.io.FileUtils; 028import org.apache.commons.logging.Log; 029import org.apache.commons.logging.LogFactory; 030import org.apache.commons.text.StringEscapeUtils; 031import org.nuxeo.common.utils.Path; 032import org.nuxeo.common.utils.ZipUtils; 033import org.nuxeo.ecm.core.api.Blob; 034import org.nuxeo.ecm.core.api.Blobs; 035import org.nuxeo.ecm.core.api.NuxeoException; 036import org.nuxeo.ecm.core.convert.api.ConversionException; 037import org.nuxeo.ecm.core.convert.cache.SimpleCachableBlobHolder; 038import org.nuxeo.ecm.platform.mimetype.MimetypeDetectionException; 039import org.nuxeo.ecm.platform.mimetype.MimetypeNotFoundException; 040import org.nuxeo.ecm.platform.mimetype.interfaces.MimetypeRegistry; 041import org.nuxeo.runtime.api.Framework; 042 043/** 044 * Cachable implementation of a zip file. 045 * 046 * @author Laurent Doguin 047 * @deprecated since 11.5, unused 048 */ 049@Deprecated 050public class ZipCachableBlobHolder extends SimpleCachableBlobHolder { 051 052 private static final Log log = LogFactory.getLog(ZipCachableBlobHolder.class); 053 054 protected Blob zipBlob; 055 056 /** 057 * @deprecated since 11.1. Use {@link Framework#getService(Class)} with {@link MimetypeRegistry} instead. 058 */ 059 @Deprecated 060 protected MimetypeRegistry mimeTypeService; 061 062 protected String key; 063 064 public ZipCachableBlobHolder() { 065 } 066 067 public ZipCachableBlobHolder(Blob zipBlob) { 068 this.zipBlob = zipBlob; 069 } 070 071 public Blob getBlob(String path) 072 throws IOException, MimetypeNotFoundException, MimetypeDetectionException, ConversionException { 073 String filePath = key + path; 074 File file = new File(filePath); 075 Blob blob = Blobs.createBlob(file); 076 String mimeType = Framework.getService(MimetypeRegistry.class).getMimetypeFromBlob(blob); 077 blob.setMimeType(mimeType); 078 blob.setFilename(path); 079 return blob; 080 } 081 082 @Override 083 public Blob getBlob() { 084 return zipBlob; 085 } 086 087 @Override 088 public List<Blob> getBlobs() { 089 if (blobs == null) { 090 try { 091 load(key); 092 } catch (IOException e) { 093 throw new NuxeoException(e); 094 } 095 } 096 return blobs; 097 } 098 099 @Override 100 public void load(String path) throws IOException { 101 blobs = new ArrayList<>(); 102 File base = new File(path); 103 104 if (base.isDirectory()) { 105 addDirectoryToList(base, ""); 106 } else { 107 File file = new File(path); 108 String mimeType = getMimeType(file); 109 Blob mainBlob = Blobs.createBlob(file, mimeType, null, file.getName()); 110 blobs.add(mainBlob); 111 } 112 113 orderIndexPageFirst(blobs); 114 } 115 116 @Override 117 public String persist(String basePath) throws IOException { 118 Path path = new Path(basePath); 119 path = path.append(getHash()); 120 File dir = new File(path.toString()); 121 dir.mkdir(); 122 ZipUtils.unzip(zipBlob.getStream(), dir); 123 key = dir.getAbsolutePath(); 124 125 // Check if creating an index.html file is needed 126 load(path.toString()); 127 if (blobs != null && !blobs.get(0).getFilename().contains("index.html")) { 128 log.info("Any index.html file found, generate a listing as index page."); 129 File index = new File(dir, "index.html"); 130 if (index.createNewFile()) { 131 Blob indexBlob = createIndexBlob(); 132 blobs.add(0, indexBlob); 133 FileUtils.writeByteArrayToFile(index, indexBlob.getByteArray()); 134 } else { 135 log.info("Unable to create index.html file"); 136 } 137 } 138 139 return key; 140 } 141 142 public String getMimeType(File file) throws ConversionException { 143 try { 144 return Framework.getService(MimetypeRegistry.class).getMimetypeFromFile(file); 145 } catch (ConversionException e) { 146 throw new ConversionException("Could not get MimeTypeRegistry", e); 147 } catch (MimetypeNotFoundException | MimetypeDetectionException e) { 148 return "application/octet-stream"; 149 } 150 } 151 152 /** 153 * @deprecated since 11.1. Use {@link Framework#getService(Class)} with {@link MimetypeRegistry} instead. 154 */ 155 @Deprecated 156 public MimetypeRegistry getMimeTypeService() throws ConversionException { 157 if (mimeTypeService == null) { 158 mimeTypeService = Framework.getService(MimetypeRegistry.class); 159 } 160 return mimeTypeService; 161 } 162 163 protected Blob createIndexBlob() { 164 StringBuilder page = new StringBuilder("<html><body>"); 165 page.append("<h1>") 166 .append(StringEscapeUtils.escapeEcmaScript(StringEscapeUtils.escapeHtml4(zipBlob.getFilename()))) 167 .append("</h1>"); 168 page.append("<ul>"); 169 for (Blob blob : blobs) { 170 String fn = StringEscapeUtils.escapeEcmaScript(StringEscapeUtils.escapeHtml4(blob.getFilename())); 171 page.append("<li><a href=\"").append(fn).append("\">"); 172 page.append(fn); 173 page.append("</a></li>"); 174 } 175 page.append("</ul></body></html>"); 176 return Blobs.createBlob(page.toString(), "text/html"); 177 } 178}