1 24 package org.riotfamily.common.web.file; 25 26 import java.io.File ; 27 import java.io.IOException ; 28 29 import javax.servlet.ServletContext ; 30 31 import org.apache.commons.logging.Log; 32 import org.apache.commons.logging.LogFactory; 33 import org.springframework.beans.factory.InitializingBean; 34 import org.springframework.util.Assert; 35 import org.springframework.util.FileCopyUtils; 36 import org.springframework.util.StringUtils; 37 import org.springframework.web.context.ServletContextAware; 38 39 44 public class DefaultFileStore implements FileStore, ServletContextAware, 45 InitializingBean { 46 47 private Log log = LogFactory.getLog(DefaultFileStore.class); 48 49 private String uriPrefix; 50 51 private String storagePath; 52 53 private File baseDir; 54 55 private File storageDir; 56 57 private int storageDirIndex; 58 59 private int maxFilesPerDir = 500; 60 61 private ServletContext servletContext; 62 63 64 public void setServletContext(ServletContext servletContext) { 65 this.servletContext = servletContext; 66 } 67 68 75 public void setStoragePath(String storagePath) { 76 this.storagePath = storagePath; 77 } 78 79 83 public void setUriPrefix(String uriPrefix) { 84 Assert.notNull(uriPrefix, "The uriPrefix must not be null"); 85 if (uriPrefix.endsWith("/")) { 86 uriPrefix = uriPrefix.substring(0, uriPrefix.length() - 1); 87 } 88 this.uriPrefix = uriPrefix; 89 } 90 91 95 public void setMaxFilesPerDir(int maxFilesPerDir) { 96 this.maxFilesPerDir = maxFilesPerDir; 97 } 98 99 102 public void afterPropertiesSet() throws IOException { 103 Assert.notNull(uriPrefix, "The uriPrefix must not be null"); 104 if (storagePath == null) { 105 storagePath = servletContext.getRealPath(uriPrefix); 106 } 107 else if (!storagePath.startsWith(File.separator) 108 && storagePath.indexOf(":") != 1) { 109 110 storagePath = servletContext.getRealPath(storagePath); 111 } 112 113 baseDir = createDir(new File (storagePath)); 114 log.info("Files will be stored in " 115 + baseDir.getCanonicalPath()); 116 117 getStorageDir(); 118 } 119 120 125 protected File createDir(File dir) { 126 if (!(dir.exists() || dir.mkdirs())) { 127 log.error("Error creating directory: " + dir.getPath()); 128 } 129 if (!dir.canWrite()) { 130 log.error("Directory " + dir.getPath() 131 + " is not writable for user " 132 + System.getProperty("user.name")); 133 } 134 return dir; 135 } 136 137 140 private boolean storageExceeded() { 141 return storageDir == null || storageDir.list().length >= maxFilesPerDir; 142 } 143 144 150 protected File getStorageDir() { 151 if (storageExceeded()) { 152 synchronized (this) { 153 while (storageExceeded()) { 154 String name = String.valueOf(storageDirIndex++); 155 storageDir = createDir(new File (baseDir, name)); 156 } 157 } 158 } 159 return storageDir; 160 } 161 162 166 protected File getUniqueDir() { 167 File parent = getStorageDir(); 168 for (int i = 0; i < maxFilesPerDir; i++) { 169 File dir = new File (parent, String.valueOf( 170 System.currentTimeMillis()) + i); 171 172 if (!dir.exists()) { 173 dir.mkdir(); 174 return dir; 175 } 176 } 177 throw new RuntimeException ("Failed to create a unique directory name."); 179 } 180 181 185 public String store(File file, String fileName) throws IOException { 186 if (fileName == null) { 187 fileName = file.getName(); 188 } 189 File dest = new File (getUniqueDir(), fileName); 190 if (!file.renameTo(dest)) { 191 FileCopyUtils.copy(file, dest); 192 file.delete(); 193 } 194 String path = dest.getPath().substring(baseDir.getPath().length()); 195 path = StringUtils.replace(path, File.separator, "/"); 196 return uriPrefix + path; 197 } 198 199 203 public File retrieve(String uri) { 204 log.debug("Retrieving file for URI: " + uri); 205 if (!uri.startsWith(uriPrefix)) { 206 return null; 207 } 208 uri = stripQueryString(uri.substring(uriPrefix.length() + 1)); 209 uri = StringUtils.replace(uri, "/", File.separator); 210 File file = new File (baseDir, uri); 211 log.debug("File: " + file); 212 return file; 213 } 214 215 220 private String stripQueryString(String uri) { 221 int i = uri.indexOf('?'); 222 if (i != -1) { 223 uri = uri.substring(0, i); 224 } 225 return uri; 226 } 227 228 231 public void delete(String uri) { 232 File file = retrieve(uri); 233 file.delete(); 234 File dir = file.getParentFile(); 235 if (dir.isDirectory() && dir.list().length == 0 && 236 !dir.equals(baseDir)) { 237 238 dir.delete(); 239 } 240 } 241 242 } 243 | Popular Tags |