X-Git-Url: http://erislabs.net/gitweb/?p=mir.git;a=blobdiff_plain;f=source%2Fmircoders%2Fmedia%2FMediaHandlerImagesExtern.java;h=61f6017e6f91fe33d85d28e0767446e51e0b9efa;hp=bb00c94fb06d4d3f8362d59d7ae62aafd45bef30;hb=d06e23e638f2538f263af76bd32da6b140f20ac6;hpb=881863484ff95a2a7e395987156120ab0f7943c8 diff --git a/source/mircoders/media/MediaHandlerImagesExtern.java b/source/mircoders/media/MediaHandlerImagesExtern.java index bb00c94f..61f6017e 100755 --- a/source/mircoders/media/MediaHandlerImagesExtern.java +++ b/source/mircoders/media/MediaHandlerImagesExtern.java @@ -1,177 +1,187 @@ -/* - * Copyright (C) 2001, 2002 The Mir-coders group - * - * This file is part of Mir. - * - * Mir is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * Mir is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Mir; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * In addition, as a special exception, The Mir-coders gives permission to link - * the code of this program with any library licensed under the Apache Software License, - * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library - * (or with modified versions of the above that use the same license as the above), - * and distribute linked combinations including the two. You must obey the - * GNU General Public License in all respects for all of the code used other than - * the above mentioned libraries. If you modify this file, you may extend this - * exception to your version of the file, but you are not obligated to do so. - * If you do not wish to do so, delete this exception statement from your version. - */ -package mircoders.media; - - -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 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. - * - * @author Zapata - * @version 1.0 - */ - -public class MediaHandlerImagesExtern extends MediaHandlerGeneric { - private int maxIconSize; - private float minDescaleRatio; - private int minDescaleReduction; - - public MediaHandlerImagesExtern() { - - logger = new LoggerWrapper("Media.Images.Extern"); - - maxIconSize = configuration.getInt("Producer.Image.MaxIconSize"); - minDescaleRatio = configuration.getFloat("Producer.Image.MinDescalePercentage")/100; - minDescaleReduction = configuration.getInt("Producer.Image.MinDescaleReduction"); - } - - public void produce(Entity anImageEntity, Entity aMediaTypeEntity) throws MediaExc, MediaFailure { - String date = anImageEntity.getFieldValue("date"); - String datePath = StringUtil.webdbDate2path(date); - 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; - 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); - } - } - - - /** {@inheritDoc} */ - public InputStream getThumbnail(Entity anImageEntity) throws MediaExc, MediaFailure { - try { - File file = new File(configuration.getString("Producer.StorageRoot") + anImageEntity.getFieldValue("icon_path")); - - if (!file.exists()) { - // hackish - file = new File(configuration.getHome(), "../img/photo_big.gif"); - } - - return new BufferedInputStream( - new FileInputStream(file),8192); - } - catch (Throwable t) { - return null; - } - } - - public String getIconMimeType(Entity anImageEntity, Entity aMediaType) { - return "image/jpeg"; - } - - public String getBaseStoragePath() - { - return configuration.getString("Producer.Image.Path"); - } - - public String getBaseIconStoragePath() - { - return configuration.getString("Producer.Image.IconPath"); - } - - public String getPublishHost() - { - return StringUtil.removeSlash(configuration.getString("Producer.Image.Host")); - } - - public String getTinyIconName() - { - return configuration.getString("Producer.Icon.TinyImage"); - } - - public String getBigIconName() - { - return configuration.getString("Producer.Icon.BigImage"); - } - - public String getIconAltName() - { - return "Image"; - } - - public String getDescr(Entity mediaType) - { - return "image/jpeg"; - } -} +/* + * Copyright (C) 2001, 2002 The Mir-coders group + * + * This file is part of Mir. + * + * Mir is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Mir is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Mir; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * In addition, as a special exception, The Mir-coders gives permission to link + * the code of this program with any library licensed under the Apache Software License, + * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library + * (or with modified versions of the above that use the same license as the above), + * and distribute linked combinations including the two. You must obey the + * GNU General Public License in all respects for all of the code used other than + * the above mentioned libraries. If you modify this file, you may extend this + * exception to your version of the file, but you are not obligated to do so. + * If you do not wish to do so, delete this exception statement from your version. + */ +package mircoders.media; + + +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 java.io.BufferedInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; + +/** + * Image handler that stores images outside of the database. + * + * @author Zapata + * @version 1.0 + */ + +public class MediaHandlerImagesExtern extends MediaHandlerGeneric { + private int maxIconSize; + private float minDescaleRatio; + private int minDescaleReduction; + + public MediaHandlerImagesExtern() { + + logger = new LoggerWrapper("Media.Images.Extern"); + + maxIconSize = configuration.getInt("Producer.Image.MaxIconSize"); + minDescaleRatio = configuration.getFloat("Producer.Image.MinDescalePercentage")/100; + minDescaleReduction = configuration.getInt("Producer.Image.MinDescaleReduction"); + } + + public void produce(Entity anImageEntity, Entity aMediaTypeEntity) throws MediaExc, MediaFailure { + String date = anImageEntity.getFieldValue("date"); + String datePath = StringUtil.webdbDate2path(date); + 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; + 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(); + } + try { + processor.writeScaledData(iconFile, "JPEG"); + } catch (FileNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + + 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); + } + } + + + /** {@inheritDoc} */ + public InputStream getThumbnail(Entity anImageEntity) throws MediaExc, MediaFailure { + try { + File file = new File(configuration.getString("Producer.StorageRoot") + anImageEntity.getFieldValue("icon_path")); + + if (!file.exists()) { + // hackish + file = new File(configuration.getHome(), "../img/photo_big.gif"); + } + + return new BufferedInputStream( + new FileInputStream(file),8192); + } + catch (Throwable t) { + return null; + } + } + + public String getIconMimeType(Entity anImageEntity, Entity aMediaType) { + return "image/jpeg"; + } + + public String getBaseStoragePath() + { + return configuration.getString("Producer.Image.Path"); + } + + public String getBaseIconStoragePath() + { + return configuration.getString("Producer.Image.IconPath"); + } + + public String getPublishHost() + { + return StringUtil.removeSlash(configuration.getString("Producer.Image.Host")); + } + + public String getTinyIconName() + { + return configuration.getString("Producer.Icon.TinyImage"); + } + + public String getBigIconName() + { + return configuration.getString("Producer.Icon.BigImage"); + } + + public String getIconAltName() + { + return "Image"; + } + + public String getDescr(Entity mediaType) + { + return "image/jpeg"; + } +}