1 24 package org.riotfamily.components.property; 25 26 import java.io.File ; 27 import java.io.IOException ; 28 import java.util.Map ; 29 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 import org.riotfamily.common.image.Thumbnailer; 33 import org.riotfamily.common.web.file.FileStore; 34 35 36 40 public class ThumbnailPropertyProcessor extends PropertyProcessorAdapter { 41 42 private static final Log log = LogFactory.getLog(ThumbnailPropertyProcessor.class); 43 44 private static final String MTIME_SUFFIX = "-mtime"; 45 46 private String source; 47 48 private String dest; 49 50 private int width; 51 52 private int height; 53 54 private String format = "jpg"; 55 56 private FileStore fileStore; 57 58 private Thumbnailer thumbnailer; 59 60 private File tempDir; 61 62 public void setSource(String source) { 63 this.source = source; 64 } 65 66 public void setDest(String dest) { 67 this.dest = dest; 68 } 69 70 public void setFileStore(FileStore fileStore) { 71 this.fileStore = fileStore; 72 } 73 74 public void setThumbnailer(Thumbnailer thumbnailer) { 75 this.thumbnailer = thumbnailer; 76 } 77 78 public void setFormat(String format) { 79 this.format = format; 80 } 81 82 public void setWidth(int width) { 83 this.width = width; 84 } 85 86 public void setHeight(int height) { 87 this.height = height; 88 } 89 90 public void convertToStrings(Map map) { 91 try { 92 String sourceUri = (String ) map.get(source); 93 File sourceFile = fileStore.retrieve(sourceUri); 94 95 String mtime = (String ) map.get(dest + MTIME_SUFFIX); 96 if (mtime == null || Long.parseLong(mtime) < sourceFile.lastModified()) { 97 File tempFile = File.createTempFile("thumb", "." + format, tempDir); 98 thumbnailer.renderThumbnail(sourceFile, tempFile, width, height); 99 String destUri = (String ) map.get(dest); 100 if (destUri != null) { 101 fileStore.delete(destUri); 102 } 103 destUri = fileStore.store(tempFile, null); 104 map.put(dest, destUri); 105 map.put(dest + MTIME_SUFFIX, String.valueOf(System.currentTimeMillis())); 106 } 107 } 108 catch (IOException e) { 109 log.error(e); 110 } 111 } 112 113 } 114 | Popular Tags |