1 package com.openedit.archive.generators; 2 3 import java.awt.Dimension ; 4 import java.io.File ; 5 6 import org.apache.commons.logging.Log; 7 import org.apache.commons.logging.LogFactory; 8 9 import com.openedit.OpenEditException; 10 import com.openedit.WebPageRequest; 11 import com.openedit.archive.Archive; 12 import com.openedit.config.Configuration; 13 import com.openedit.generators.Output; 14 import com.openedit.modules.archive.ArchiveModule; 15 import com.openedit.page.Page; 16 import com.openedit.store.Product; 17 import com.openedit.store.images.ImageMagickResizer; 18 import com.openedit.util.PathUtilities; 19 20 26 public class PreviewGenerator extends TempFileGenerator 27 { 28 private static final Log log = LogFactory.getLog(PreviewGenerator.class); 29 30 public void generate( WebPageRequest inReq, Page inPage, Output inOut ) 31 throws OpenEditException 32 { 33 Archive archive = (Archive) inReq.getPageValue("archive"); 34 String productId = PathUtilities.extractPageName( inPage.getPath() ); 35 Product product = archive.getProduct( productId ); 36 if ( product == null ) 37 { 38 throw new OpenEditException( "No product with ID " + productId ); 39 } 40 File previewFile = archive.makeCacheFile(product, "/preview/" + product.getId() + ".jpg"); 42 if ( previewFile.exists() && previewFile.length() > 0) 43 { 44 super.generate(inReq,previewFile, inOut); 45 return; 46 } 47 try 48 { 49 previewFile.getParentFile().mkdirs(); 50 previewFile.createNewFile(); 52 File originalDocumentFile = archive.getOriginalDocument( product ); 54 if ( originalDocumentFile == null ) 55 { 56 log.error( "Could not determine original document filename for " + product.getName() ); 57 inReq.redirect(archive.getCatalogHome() + "/missingfile.html" ); 58 return; 59 } 60 if ( !originalDocumentFile.exists() ) 61 { 62 log.error( "Original document not found:" + originalDocumentFile ); 63 inReq.redirect(archive.getCatalogHome() + "/missingfile.jpg"); 64 return; 65 } 66 67 ImageMagickResizer resizer = new ImageMagickResizer(); 68 69 String w = inReq.getRequestParameter("prefwidth"); 70 String h = inReq.getRequestParameter("prefheight"); 71 if( w == null) 72 { 73 w = "800"; 74 } 75 if( h == null) 76 { 77 h = "600"; 78 } 79 resizer.setMaxScaledSize(new Dimension (Integer.parseInt(w),Integer.parseInt(h))); 80 Configuration child = archive.getSettings().getChild("convertpath"); 81 if( child != null) 82 { 83 resizer.setCommandName(child.getValue()); 84 } 85 child = archive.getSettings().getChild("ghostscriptpath"); 86 if( child != null) 87 { 88 resizer.setGhostScriptCommand(child.getValue()); 89 } 90 resizer.resizeImage(originalDocumentFile,previewFile); 91 super.generate(inReq,previewFile, inOut); 92 93 } 94 catch ( Exception ioe ) 95 { 96 if( ignoreError(ioe)) 97 { 98 return; 100 } 101 throw new OpenEditException(ioe); 102 } 103 finally 104 { 105 if ( previewFile.length() == 0) 106 { 107 previewFile.delete(); 108 } 110 } 111 } 112 } 113 | Popular Tags |