001/* 002 * (C) Copyright 2006-2017 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.core.convert.cache; 021 022import java.io.File; 023import java.io.IOException; 024import java.util.ArrayList; 025import java.util.List; 026 027import org.nuxeo.common.utils.Path; 028import org.nuxeo.ecm.core.api.Blob; 029import org.nuxeo.ecm.core.api.Blobs; 030import org.nuxeo.ecm.core.api.blobholder.SimpleBlobHolder; 031import org.nuxeo.ecm.core.api.impl.blob.FileBlob; 032 033/** 034 * Cachable implementation of the {@link SimpleBlobHolder}. 035 * 036 * @author tiry 037 */ 038public class SimpleCachableBlobHolder extends SimpleBlobHolder implements CachableBlobHolder { 039 040 public SimpleCachableBlobHolder() { 041 } 042 043 public SimpleCachableBlobHolder(Blob blob) { 044 super(blob); 045 } 046 047 public SimpleCachableBlobHolder(List<Blob> blobs) { 048 super(blobs); 049 } 050 051 public SimpleCachableBlobHolder(String path) { 052 super(new FileBlob(new File(path))); 053 } 054 055 @Override 056 public void load(String path) throws IOException { 057 blobs = new ArrayList<>(); 058 File base = new File(path); 059 if (base.isDirectory()) { 060 addDirectoryToList(base, ""); 061 } else { 062 File file = new File(path); 063 Blob mainBlob = Blobs.createBlob(file); 064 mainBlob.setFilename(file.getName()); 065 blobs.add(mainBlob); 066 } 067 068 orderIndexPageFirst(blobs); 069 } 070 071 public void addDirectoryToList(File directory, String prefix) throws IOException { 072 File[] directoryContent = directory.listFiles(); 073 for (File file : directoryContent) { 074 if (file.isDirectory()) { 075 int beginIndex = prefix.length(); 076 prefix = prefix + file.getName() + File.separatorChar; 077 addDirectoryToList(file, prefix); 078 prefix = prefix.substring(0, beginIndex); 079 } else { 080 Blob blob = Blobs.createBlob(file); 081 blob.setFilename(prefix + file.getName()); 082 if (file.getName().equalsIgnoreCase("index.html")) { 083 blobs.add(0, blob); 084 } else { 085 blobs.add(blob); 086 } 087 } 088 } 089 } 090 091 @Override 092 public String persist(String basePath) throws IOException { 093 if (blobs == null || blobs.isEmpty()) { 094 return null; 095 } 096 Path path = new Path(basePath); 097 path = path.append(getHash()); 098 if (blobs.size() == 1) { 099 File file = new File(path.toString()); 100 getBlob().transferTo(file); 101 return file.getAbsolutePath(); 102 } else { 103 File dir = new File(path.toString()); 104 dir.mkdir(); 105 for (Blob blob : blobs) { 106 File file = new File(path.append(blob.getFilename()).toString()); 107 blob.transferTo(file); 108 } 109 return dir.getAbsolutePath(); 110 } 111 } 112 113 /** 114 * Rearrange blobs to have smallest index.html page as the first blob. 115 * 116 * @since 5.7.1 117 */ 118 protected void orderIndexPageFirst(List<Blob> blobs) { 119 Blob indexPage = null; 120 for (Blob blob : blobs) { 121 if (blob.getFilename().contains("index.html") 122 && (indexPage == null || blob.getFilename().compareTo(indexPage.getFilename()) < 0)) { 123 indexPage = blob; 124 } 125 } 126 127 if (indexPage != null) { 128 blobs.remove(indexPage); 129 blobs.add(0, indexPage); 130 } 131 } 132 133}