1 package com.openedit.archive; 2 3 import java.io.File ; 4 import java.io.FileInputStream ; 5 import java.io.FileOutputStream ; 6 import java.io.IOException ; 7 import java.io.OutputStream ; 8 import java.util.ArrayList ; 9 import java.util.Iterator ; 10 import java.util.List ; 11 import java.util.zip.ZipEntry ; 12 import java.util.zip.ZipOutputStream ; 13 14 import org.apache.commons.logging.Log; 15 import org.apache.commons.logging.LogFactory; 16 17 import com.openedit.OpenEditException; 18 import com.openedit.archive.collection.Collection; 19 import com.openedit.archive.collection.CollectionItem; 20 import com.openedit.store.Order; 21 import com.openedit.store.Store; 22 import com.openedit.store.images.ImageMagickResizer; 23 import com.openedit.util.FileUtils; 24 import com.openedit.util.OutputFiller; 25 import com.openedit.util.PathUtilities; 26 import com.openedit.modules.email.*; 27 28 public class ZipCollection 29 { 30 private static final Log log = LogFactory.getLog(ZipCollection.class); 31 protected Archive fieldArchive; 32 private PostMail fieldPostMail; 33 34 43 public void zipCollectionDocuments( Collection inCol, OutputStream inStream , boolean inForPositionOnly ) 44 throws OpenEditException 45 { 46 List missingDocumentItems = new ArrayList (); 47 ZipOutputStream zos = new ZipOutputStream ( inStream ); 48 zos.setLevel(1); WatermarkMaker marker = null;; 50 if( inForPositionOnly ) 51 { 52 File water = new File (getArchive().getArchiveDirectory(),"/layout/media/watermark.png"); 53 if( water.exists() ) 54 { 55 marker = new WatermarkMaker(); 56 marker.setWaterMark(water); 57 } 58 } 59 try 60 { 61 OutputFiller filler = new OutputFiller(); 62 for ( Iterator iter = inCol.getCollectionItems().iterator(); iter.hasNext(); ) 63 { 64 CollectionItem item = (CollectionItem) iter.next(); 65 File documentFile = getArchive().getOriginalDocument( item.getProduct() ); 66 if ( documentFile == null || !documentFile.exists() ) 67 { 68 if ( documentFile != null) 69 { 70 log.info("Image missing:" + documentFile.getAbsolutePath()); 71 } 72 missingDocumentItems.add( item ); 73 } 74 else 75 { 76 if( inForPositionOnly ) 77 { 78 String dir = getArchive().getStore().getProductPathFinder().idToPath(item.getProduct().getId()); 80 File convertFile = getArchive().makeCacheFile(item.getProduct(), "/convert/" + PathUtilities.extractPagePath(dir) + ".jpg" ); 81 if( !getArchive().convert(documentFile, convertFile) ) { 83 log.error("Could not create document " + convertFile.getAbsolutePath() + " from " + documentFile.getAbsolutePath() ); 84 missingDocumentItems.add( item ); 85 } 86 else 87 { 88 if( marker != null) 90 { 91 try 92 { 93 File temp = File.createTempFile(item.getProduct().getName(), "FPO.jpg"); 94 FileOutputStream done = new FileOutputStream (temp); 95 log.info("adding watermarked file: " + temp); 96 marker.addWaterMark(new FileInputStream (convertFile), done); 97 FileUtils.safeClose(done); 98 writeFileToZip( zos, filler, temp ); 99 temp.delete(); 100 } 101 catch (IOException ex) 102 { 103 throw new OpenEditException(ex); 104 } 105 } 106 else 107 { 108 log.info("No watermark found adding size:" + convertFile.length() + " path:" + convertFile.getAbsolutePath() ); 109 writeFileToZip( zos, filler, convertFile ); 110 } 111 } 112 } 113 else 114 { 115 writeFileToZip( zos, filler, documentFile ); 116 } 117 } 118 } 119 120 if ( !missingDocumentItems.isEmpty() ) 121 { 122 String missingProducts = buildMissingDocumentsText( missingDocumentItems ); 123 124 125 writeStringToZip( zos, missingProducts, "missing.txt" ); 126 getArchive().getEmailErrorHandler().sendNotification("Missing File Report", missingProducts); 127 128 } 129 } 130 finally 131 { 132 try 133 { 134 FileUtils.safeClose( zos ); } 136 catch ( Exception ex2) 137 { 138 } 140 } 141 } 142 143 protected void writeFileToZip( ZipOutputStream inZipOutputStream, 144 OutputFiller inOutputFiller, File inFile ) 145 { 146 ZipEntry entry = new ZipEntry ( inFile.getName() ); 147 entry.setSize( inFile.length() ); 148 entry.setTime( inFile.lastModified() ); 149 try 150 { 151 FileInputStream fis = new FileInputStream ( inFile ); 152 inZipOutputStream.putNextEntry( entry ); 153 try 154 { 155 inOutputFiller.fill( fis, inZipOutputStream ); 156 } 157 finally 158 { 159 fis.close(); 160 } 161 inZipOutputStream.closeEntry(); 162 } 163 catch ( Exception ex ) 164 { 165 log.error( ex ); 166 } 167 } 168 169 protected void writeStringToZip( ZipOutputStream inZipOutputStream, 170 String inText, String inFileName ) 171 { 172 try 173 { 174 byte[] bytes = inText.getBytes( "UTF-8" ); 175 ZipEntry entry = new ZipEntry ( inFileName ); 176 entry.setSize( bytes.length ); 177 inZipOutputStream.putNextEntry( entry ); 178 inZipOutputStream.write( bytes ); 179 inZipOutputStream.closeEntry(); 180 } 181 catch ( IOException ex) 182 { 183 log.error( ex ); 184 } 185 } 186 187 protected String buildMissingDocumentsText( List inMissingDocumentItems ) 188 { 189 StringBuffer missingProductsStr = new StringBuffer ( 190 "Original documents for the following could not be retrieved:\n\n" ); 191 for ( Iterator iter = inMissingDocumentItems.iterator(); iter 192 .hasNext(); ) 193 { 194 CollectionItem item = (CollectionItem) iter.next(); 195 missingProductsStr.append( " - " ); 196 missingProductsStr.append( item.getProduct().getName()); 197 missingProductsStr.append( " (" ); 198 File path = getArchive().getOriginalDocument( item.getProduct() ); 199 if ( path == null ) 200 { 201 missingProductsStr.append( "no file name specified" ); 202 } 203 else 204 { 205 missingProductsStr.append( path.getAbsolutePath() ); 206 } 207 missingProductsStr.append( ")\n" ); 208 } 209 return missingProductsStr.toString(); 210 } 211 212 213 public Archive getArchive() 214 { 215 return fieldArchive; 216 } 217 218 public void setArchive(Archive inArchive) 219 { 220 fieldArchive = inArchive; 221 } 222 223 } 224 | Popular Tags |