X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=source%2Fmircoders%2Fmedia%2FMediaHandlerImagesExtern.java;h=4b52f1f958b14da91323fbfb336c31a0c7c0df68;hb=67a8db9b40b101db851e6f30617c12f7691f9794;hp=5f17d18a656dac064c3839f52a60b4406bde0177;hpb=0bae3282355557eb9f6869eb266de9947b728d09;p=mir.git diff --git a/source/mircoders/media/MediaHandlerImagesExtern.java b/source/mircoders/media/MediaHandlerImagesExtern.java index 5f17d18a..4b52f1f9 100755 --- a/source/mircoders/media/MediaHandlerImagesExtern.java +++ b/source/mircoders/media/MediaHandlerImagesExtern.java @@ -33,104 +33,151 @@ 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.config.MirPropertiesConfiguration; import mir.entity.Entity; import mir.log.LoggerWrapper; import mir.media.MediaExc; import mir.media.MediaFailure; +import mir.media.image.ImageMagickImageProcessor; +import mir.media.image.ImageProcessor; import mir.misc.StringUtil; - +import mir.util.FileRoutines; /** - * Image handler that stores images outside of the database. Will be replaced by the new - * media handling system. + * Image handler that stores images outside of the database. + * * @author Zapata * @version 1.0 */ public class MediaHandlerImagesExtern extends MediaHandlerGeneric { + private int maxSize; private int maxIconSize; private float minDescaleRatio; private int minDescaleReduction; - + private boolean scaleImages; + public MediaHandlerImagesExtern() { - logger = new LoggerWrapper("Media.Images.Extern"); - try { - MirPropertiesConfiguration configuration = MirPropertiesConfiguration.instance(); - } - catch (Throwable t) { - logger.fatal("MediaHandlerImagesExtern: can't get configuration"); - - throw new RuntimeException(t.toString()); - } - + 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 { - String date = anImageEntity.getValue("date"); + String date = anImageEntity.getFieldValue("date"); String datePath = StringUtil.webdbDate2path(date); - String ext = "." + mediaTypeEnt.getValue("name"); + String ext = "." + mediaTypeEnt.getFieldValue("name"); String fileBasePath = datePath + anImageEntity.getId(); String filePath = fileBasePath + ext; - String iconPath = getIconStoragePath() + fileBasePath + ".jpg"; - String iconStoragePath = configuration.getString("Producer.StorageRoot") + iconPath; - String imageFilePath = getStoragePath() + File.separator + filePath; + 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.produce(): " + filePath + " does not exist!"); - } - else { - ImageProcessor processor = new ImageProcessor(imageFile); - - processor.descaleImage(maxIconSize, minDescaleRatio, minDescaleReduction); - File dir = new File(iconFile.getParent()); - if (dir!=null && !dir.exists()){ - dir.mkdirs(); + 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()); } - processor.writeScaledData(iconFile, "JPEG"); - - anImageEntity.setValueForProperty("img_height", new Integer(processor.getHeight()).toString()); - anImageEntity.setValueForProperty("img_width", new Integer(processor.getWidth()).toString()); - - anImageEntity.setValueForProperty("icon_height", new Integer(processor.getScaledHeight()).toString()); - anImageEntity.setValueForProperty("icon_width", new Integer(processor.getScaledWidth()).toString()); - - anImageEntity.setValueForProperty("icon_path", iconPath); - anImageEntity.setValueForProperty("publish_path", filePath); - + processor.cleanup(); + anImageEntity.setFieldValue("publish_path", filePath); anImageEntity.update(); - - + reportChange(iconStoragePath); + reportChange(imageFilePath); } } catch(Throwable t) { - logger.error("MediaHandlerImagesExtern.produce: " + t.getMessage()); - t.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE)); + 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; + } - public InputStream getIcon(Entity anImageEntity) throws MediaExc, MediaFailure { +/** {@inheritDoc} */ + public InputStream getThumbnail(Entity anImageEntity) throws MediaExc, MediaFailure { try { - String filePath = - configuration.getString("Producer.StorageRoot") + anImageEntity.getValue("icon_path"); + File file = new File(configuration.getString("Producer.StorageRoot") + anImageEntity.getFieldValue("icon_path")); - logger.info(filePath); + if (!file.exists()) { + // hackish + file = new File(configuration.getHome(), "../img/photo_big.gif"); + } return new BufferedInputStream( - new FileInputStream(new File(filePath)),8192); + new FileInputStream(file),8192); } catch (Throwable t) { return null; @@ -141,12 +188,12 @@ public class MediaHandlerImagesExtern extends MediaHandlerGeneric return "image/jpeg"; } - public String getStoragePath() + public String getBaseStoragePath() { return configuration.getString("Producer.Image.Path"); } - public String getIconStoragePath() + public String getBaseIconStoragePath() { return configuration.getString("Producer.Image.IconPath"); } @@ -171,21 +218,6 @@ public class MediaHandlerImagesExtern extends MediaHandlerGeneric return "Image"; } - public boolean isVideo() - { - return false; - } - - public boolean isAudio() - { - return false; - } - - public boolean isImage () - { - return true; - } - public String getDescr(Entity mediaType) { return "image/jpeg";