1 24 package org.riotfamily.components.editor; 25 26 import java.io.File ; 27 import java.io.IOException ; 28 import java.util.HashMap ; 29 30 import org.riotfamily.common.image.ImageCropper; 31 import org.riotfamily.common.util.PasswordGenerator; 32 import org.riotfamily.common.web.file.FileStore; 33 34 38 public class UploadManagerImpl implements UploadManager { 39 40 private PasswordGenerator tokenGenerator = 41 new PasswordGenerator(16, true, true, true); 42 43 private HashMap filePaths = new HashMap (); 44 45 private FileStore fileStore; 46 47 private ImageCropper imageCropper; 48 49 public UploadManagerImpl(FileStore fileStore, ImageCropper imageCropper) { 50 this.fileStore = fileStore; 51 this.imageCropper = imageCropper; 52 } 53 54 public String generateToken() { 55 String token = tokenGenerator.generate(); 56 filePaths.put(token, null); 57 return token; 58 } 59 60 public String getFilePath(String token) { 61 return (String ) filePaths.get(token); 62 } 63 64 boolean isValidToken(String token) { 65 return filePaths.containsKey(token); 66 } 67 68 public void invalidateToken(String token) { 69 filePaths.remove(token); 70 } 71 72 void storeFile(String uploadId, File file, String originalFileName) 73 throws IOException { 74 75 String path = fileStore.store(file, originalFileName); 76 filePaths.put(uploadId, path); 77 } 78 79 public String cropImage(String path, int width, int height, int x, int y, 80 int scaledWidth) throws IOException { 81 82 File src = fileStore.retrieve(path); 83 File dest = File.createTempFile("crop", ".img"); 84 imageCropper.cropImage(src, dest, width, height, x, y, scaledWidth); 85 fileStore.delete(path); 86 return fileStore.store(dest, src.getName()); 87 } 88 } 89 | Popular Tags |