From d3e3f0ffeefd8692277d9a82af477a64ae85e118 Mon Sep 17 00:00:00 2001 From: yossarian Date: Sat, 16 Dec 2006 15:38:35 +0000 Subject: [PATCH] I've split out the image scaling code into a separate class and returned this class to its original state - i.e. Zapata's code from revision 1.12.2.11 The only thing that should be different is that I've also added the reportChange() lines to the bottom of the produce() method so that the ChangeTracker gets change reports. --- .../mircoders/media/MediaHandlerImagesExtern.java | 142 +++++++-------------- 1 file changed, 47 insertions(+), 95 deletions(-) diff --git a/source/mircoders/media/MediaHandlerImagesExtern.java b/source/mircoders/media/MediaHandlerImagesExtern.java index 4b52f1f9..bb00c94f 100755 --- a/source/mircoders/media/MediaHandlerImagesExtern.java +++ b/source/mircoders/media/MediaHandlerImagesExtern.java @@ -30,12 +30,6 @@ package mircoders.media; -import java.io.BufferedInputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; - import mir.entity.Entity; import mir.log.LoggerWrapper; import mir.media.MediaExc; @@ -43,7 +37,12 @@ import mir.media.MediaFailure; import mir.media.image.ImageMagickImageProcessor; import mir.media.image.ImageProcessor; import mir.misc.StringUtil; -import mir.util.FileRoutines; + +import java.io.BufferedInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; /** * Image handler that stores images outside of the database. @@ -52,121 +51,74 @@ import mir.util.FileRoutines; * @version 1.0 */ -public class MediaHandlerImagesExtern extends MediaHandlerGeneric -{ - private int maxSize; +public class MediaHandlerImagesExtern extends MediaHandlerGeneric { private int maxIconSize; private float minDescaleRatio; private int minDescaleReduction; - private boolean scaleImages; - + public MediaHandlerImagesExtern() { + logger = new LoggerWrapper("Media.Images.Extern"); - maxSize = configuration.getInt("Producer.Image.MaxSize"); + maxIconSize = configuration.getInt("Producer.Image.MaxIconSize"); minDescaleRatio = configuration.getFloat("Producer.Image.MinDescalePercentage")/100; minDescaleReduction = configuration.getInt("Producer.Image.MinDescaleReduction"); - scaleImages = configuration.getBoolean("Producer.Image.ScaleImages"); } - public void produce(Entity anImageEntity, Entity mediaTypeEnt) throws MediaExc, MediaFailure { - try { + public void produce(Entity anImageEntity, Entity aMediaTypeEntity) throws MediaExc, MediaFailure { String date = anImageEntity.getFieldValue("date"); String datePath = StringUtil.webdbDate2path(date); - String ext = "." + mediaTypeEnt.getFieldValue("name"); + String ext = "." + aMediaTypeEntity.getFieldValue("name"); String fileBasePath = datePath + anImageEntity.getId(); String filePath = fileBasePath + ext; - + String iconPath = getBaseIconStoragePath() + fileBasePath + ".jpg"; + String iconStoragePath = configuration.getString("Producer.StorageRoot") + iconPath; String imageFilePath = getBaseStoragePath() + File.separator + filePath; + File imageFile = new File(imageFilePath); - + File iconFile = new File(iconStoragePath); + if (!imageFile.exists()) { - throw new MediaExc("error in MediaHandlerImagesExtern.execute(): " - + filePath + " does not exist!"); - } else { - ImageProcessor processor = new ImageMagickImageProcessor(imageFile); - String iconPath = getBaseIconStoragePath() + fileBasePath + ".jpg"; - String iconStoragePath = doIconScaling(processor, iconPath); - anImageEntity.setFieldValue("icon_height", new Integer(processor.getScaledHeight()).toString()); - anImageEntity.setFieldValue("icon_width", new Integer(processor.getScaledWidth()).toString()); - anImageEntity.setFieldValue("icon_path", iconPath); - - if (scaleImages) { - String imageOriginalRelPath = doImageScaling(filePath, imageFile, processor); - anImageEntity.setFieldValue("original_file_path", imageOriginalRelPath); - anImageEntity.setFieldValue("img_height", Integer.toString(processor.getScaledHeight())); - anImageEntity.setFieldValue("img_width", Integer.toString(processor.getScaledWidth())); - } else { - anImageEntity.setFieldValue("img_height", new Integer(processor.getHeight()).toString()); - anImageEntity.setFieldValue("img_width", new Integer(processor.getWidth()).toString()); + throw new MediaExc("error in MediaHandlerImagesExtern.execute(): " + filePath + " does not exist!"); + } + else { + ImageProcessor processor; + try { + processor = new ImageMagickImageProcessor(imageFile); + } + catch (IOException e) { + throw new MediaFailure(e); } + + processor.descaleImage(maxIconSize, minDescaleRatio, minDescaleReduction); + File dir = new File(iconFile.getParent()); + if (dir!=null && !dir.exists()){ + dir.mkdirs(); + } + processor.writeScaledData(iconFile, "JPEG"); + + anImageEntity.setFieldValue("img_height", + Integer.toString(processor.getHeight())); + anImageEntity.setFieldValue("img_width", + Integer.toString(processor.getWidth())); + + anImageEntity.setFieldValue("icon_height", + Integer.toString(processor.getScaledHeight())); + anImageEntity.setFieldValue("icon_width", + Integer.toString(processor.getScaledWidth())); + processor.cleanup(); + anImageEntity.setFieldValue("icon_path", iconPath); anImageEntity.setFieldValue("publish_path", filePath); + anImageEntity.update(); reportChange(iconStoragePath); reportChange(imageFilePath); } - } - catch(Throwable t) { - logger.error("MediaHandlerImagesExtern.execute: " + t.getMessage(), t); - throw new MediaFailure(t.getMessage(), t); - } } - /** - * Scale an icon image and write it to the file system. - * @param processor - * @param iconPath - * @return - * @throws MediaExc - */ - private String doIconScaling(ImageProcessor processor, String iconPath) throws MediaExc { - String iconStoragePath = configuration.getString("Producer.StorageRoot") + iconPath; - File iconFile = new File(iconStoragePath); - processor.descaleImage(maxIconSize, minDescaleRatio, minDescaleReduction); - File dir = new File(iconFile.getParent()); - if (dir != null && !dir.exists()) { - dir.mkdirs(); - } - processor.writeScaledData(iconFile, "JPEG"); - return iconStoragePath; - } - - /** - * Make the resized file. - * @param filePath - * @param imageFile - * @param processor - * @return - * @throws MediaExc - * @throws IOException - */ - private String doImageScaling(String filePath, File imageFile, ImageProcessor processor) throws MediaExc, IOException { - // get a file path where the the original image should be saved if image resizing is turned on - String imagesOriginalDir = configuration.getString("Producer.ImagesOriginalDir.Path"); - String imagesOriginalDirRelPath = configuration.getString("Producer.ImagesOriginalDir.RelPath"); - String imageOriginalFilePath = imagesOriginalDir + filePath; - String imageOriginalRelPath = imagesOriginalDirRelPath + filePath; - File originalFile = new File(imageOriginalFilePath); - processor.descaleImage(maxSize, minDescaleRatio, minDescaleReduction); - File originalDir = new File(originalFile.getParent()); - if (originalDir != null && !originalDir.exists()) { - originalDir.mkdirs(); - } - if (!originalFile.exists()) { - FileRoutines.copy(imageFile, originalFile); - reportChange(originalFile.getAbsolutePath()); - } - // yoss: don't write the scaled data again if it's the same size as - // the file that's there right now. Image producer runs are 4 times - // faster this way. - if (processor.getScaledFileSize() != imageFile.length()) { - processor.writeScaledData(imageFile, "JPEG"); - } - return imageOriginalRelPath; - } -/** {@inheritDoc} */ + /** {@inheritDoc} */ public InputStream getThumbnail(Entity anImageEntity) throws MediaExc, MediaFailure { try { File file = new File(configuration.getString("Producer.StorageRoot") + anImageEntity.getFieldValue("icon_path")); -- 2.11.0