1 17 package org.alfresco.repo.content.transform.magick; 18 19 import java.io.File ; 20 import java.io.InputStream ; 21 import java.util.Collections ; 22 import java.util.Map ; 23 24 import org.alfresco.error.AlfrescoRuntimeException; 25 import org.alfresco.repo.content.MimetypeMap; 26 import org.alfresco.repo.content.filestore.FileContentWriter; 27 import org.alfresco.repo.content.transform.AbstractContentTransformer; 28 import org.alfresco.service.cmr.repository.ContentIOException; 29 import org.alfresco.service.cmr.repository.ContentReader; 30 import org.alfresco.service.cmr.repository.ContentWriter; 31 import org.alfresco.util.TempFileProvider; 32 import org.apache.commons.logging.Log; 33 import org.apache.commons.logging.LogFactory; 34 35 40 public abstract class AbstractImageMagickContentTransformer extends AbstractContentTransformer 41 { 42 43 public static final String MIMETYPE_IMAGE_PREFIX = "image/"; 44 45 private static final Log logger = LogFactory.getLog(AbstractImageMagickContentTransformer.class); 46 47 private boolean available; 48 49 public AbstractImageMagickContentTransformer() 50 { 51 this.available = false; 52 } 53 54 57 public boolean isAvailable() 58 { 59 return available; 60 } 61 62 66 protected void setAvailable(boolean available) 67 { 68 this.available = available; 69 } 70 71 78 public void init() 79 { 80 if (getMimetypeService() == null) 81 { 82 throw new AlfrescoRuntimeException("MimetypeMap not present"); 83 } 84 try 85 { 86 String resourcePath = "org/alfresco/repo/content/transform/magick/alfresco.gif"; 88 InputStream imageStream = getClass().getClassLoader().getResourceAsStream(resourcePath); 89 if (imageStream == null) 90 { 91 throw new AlfrescoRuntimeException("Sample image not found: " + resourcePath); 92 } 93 File inputFile = TempFileProvider.createTempFile( 95 getClass().getSimpleName() + "_init_source_", 96 ".gif"); 97 FileContentWriter writer = new FileContentWriter(inputFile); 98 writer.putContent(imageStream); 99 100 File outputFile = TempFileProvider.createTempFile( 102 getClass().getSimpleName() + "_init_target_", 103 ".png"); 104 105 Map <String , Object > options = Collections.emptyMap(); 107 transformInternal(inputFile, outputFile, options); 108 109 if (!outputFile.exists()) 111 { 112 throw new Exception ("Image conversion failed: \n" + 113 " from: " + inputFile + "\n" + 114 " to: " + outputFile); 115 } 116 setAvailable(true); 118 119 super.register(); 121 } 122 catch (Throwable e) 123 { 124 logger.error( 125 getClass().getSimpleName() + " not available: " + 126 (e.getMessage() != null ? e.getMessage() : "")); 127 logger.debug(e); 129 } 130 } 131 132 138 public static boolean isSupported(String mimetype) 139 { 140 if (!mimetype.startsWith(MIMETYPE_IMAGE_PREFIX)) 141 { 142 return false; } 144 else if (mimetype.equals(MimetypeMap.MIMETYPE_IMAGE_RGB)) 145 { 146 return false; } 148 else 149 { 150 return true; 151 } 152 } 153 154 158 public double getReliability(String sourceMimetype, String targetMimetype) 159 { 160 if (!available) 161 { 162 return 0.0; 163 } 164 if (!AbstractImageMagickContentTransformer.isSupported(sourceMimetype) || 165 !AbstractImageMagickContentTransformer.isSupported(targetMimetype)) 166 { 167 return 0.0; 169 } 170 else 171 { 172 return 1.0; 173 } 174 } 175 176 179 protected final void transformInternal( 180 ContentReader reader, 181 ContentWriter writer, 182 Map <String , Object > options) throws Exception 183 { 184 String sourceMimetype = getMimetype(reader); 186 String targetMimetype = getMimetype(writer); 187 188 String sourceExtension = getMimetypeService().getExtension(sourceMimetype); 190 String targetExtension = getMimetypeService().getExtension(targetMimetype); 191 if (sourceExtension == null || targetExtension == null) 192 { 193 throw new AlfrescoRuntimeException("Unknown extensions for mimetypes: \n" + 194 " source mimetype: " + sourceMimetype + "\n" + 195 " source extension: " + sourceExtension + "\n" + 196 " target mimetype: " + targetMimetype + "\n" + 197 " target extension: " + targetExtension); 198 } 199 200 File sourceFile = TempFileProvider.createTempFile( 202 getClass().getSimpleName() + "_source_", 203 "." + sourceExtension); 204 File targetFile = TempFileProvider.createTempFile( 205 getClass().getSimpleName() + "_target_", 206 "." + targetExtension); 207 208 reader.getContent(sourceFile); 210 211 transformInternal(sourceFile, targetFile, options); 213 214 if (!targetFile.exists()) 216 { 217 throw new ContentIOException("JMagick transformation failed to write output file"); 218 } 219 writer.putContent(targetFile); 221 if (logger.isDebugEnabled()) 223 { 224 logger.debug("Transformation completed: \n" + 225 " source: " + reader + "\n" + 226 " target: " + writer + "\n" + 227 " options: " + options); 228 } 229 } 230 231 239 protected abstract void transformInternal( 240 File sourceFile, 241 File targetFile, 242 Map <String , Object > options) throws Exception ; 243 } 244 | Popular Tags |