*/
package mircoders.media;
+import java.io.ByteArrayInputStream;
import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
import java.io.InputStream;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.util.List;
-import java.util.Vector;
+import java.io.OutputStream;
+import java.sql.SQLException;
import mir.config.MirPropertiesConfiguration;
import mir.entity.Entity;
import mir.media.MediaExc;
import mir.media.MediaFailure;
import mir.media.MediaHandler;
-import mir.misc.FileUtil;
import mir.misc.StringUtil;
-import mir.session.UploadedFile;
import mir.session.SessionExc;
+import mir.session.UploadedFile;
+import mir.util.FileRoutines;
+import mir.util.IORoutines;
import mircoders.entity.EntityImages;
/**
*
* @see mir.media.MediaHandler
* @author mh
+ * @version $Id: MediaHandlerImages.java,v 1.26 2007/04/08 21:46:35 idfx Exp $
*/
}
public InputStream getMedia(Entity ent, Entity mediaTypeEnt) throws MediaExc, MediaFailure {
- InputStream inputStream;
-
try {
- inputStream = ((EntityImages)ent).getImage();
+ return new ByteArrayInputStream(((EntityImages) ent).getImage());
}
catch (Throwable e) {
logger.error("MediaHandlerImages.getImage: " + e.toString());
throw new MediaFailure(e);
}
-
- return inputStream;
}
public void store(UploadedFile anUploadedFile, Entity aMedia, Entity aMediaType) throws MediaExc, MediaFailure {
public void store(InputStream in, Entity ent, Entity mediaTypeEnt) throws MediaExc, MediaFailure {
try {
- ((EntityImages)ent).setImage(in, getType());
+ ((EntityImages) ent).setImage(in, getType());
}
- catch (Throwable e) {
- logger.error("MediaHandlerImages.store: "+e.toString());
- e.printStackTrace(logger.asPrintWriter(LoggerWrapper.ERROR_MESSAGE));
+ catch (IOException e) {
+ logger.error("MediaHandlerImages.store", e);
- throw new MediaExc("A problem has occurred processing the media file: " + e.toString());
+ throw new MediaFailure("A problem has occurred processing the media file", e);
}
- }
+ catch (SQLException e) {
+ logger.error("MediaHandlerImages.store", e);
- public void store(File aFile, Entity aMedia, Entity aMediaType) throws MediaExc, MediaFailure {
- try {
- store(new FileInputStream(aFile), aMedia, aMediaType);
- }
- catch (FileNotFoundException e) {
- throw new MediaFailure(e);
+ throw new MediaFailure("A problem has occurred processing the media file", e);
}
}
String datePath = StringUtil.webdbDate2path(date);
String ext = "."+mediaTypeEnt.getFieldValue("name");
String filepath = datePath+ent.getId()+ext;
- String iconFilePath = configuration.getString("Producer.StorageRoot")
- +getIconStoragePath() + filepath;
- String productionFilePath = getStoragePath() + File.separator + filepath;
+ String iconFilePath = configuration.getString("Producer.StorageRoot") + getBaseIconStoragePath() + filepath;
+ String productionFilePath = getBaseStoragePath() + File.separator + filepath;
if (ent.getFieldValue("icon_data")!= null &&
ent.getFieldValue("image_data")!= null) {
// make icon
try {
- InputStream in = ((EntityImages) ent).getIcon();
- FileUtil.write(iconFilePath, in);
- in = ((EntityImages)ent).getImage();
- FileUtil.write(productionFilePath, in);
- ent.setFieldValue("icon_path",getIconStoragePath()+filepath);
- ent.setFieldValue("publish_path",filepath);
+ OutputStream out;
+ InputStream in = new ByteArrayInputStream(((EntityImages) ent).getIcon());
+ try {
+ File iconFile = new File(iconFilePath);
+
+ FileRoutines.createParentDirectories(iconFile);
+
+ out = new FileOutputStream(iconFile);
+ try {
+ IORoutines.copyStream(in, out);
+ reportChange(iconFile.getAbsolutePath());
+ }
+ finally {
+ out.close();
+ }
+ }
+ finally {
+ in.close();
+ }
+
+ in = new ByteArrayInputStream(((EntityImages) ent).getImage());
+ try {
+ File imageFile = new File(productionFilePath);
+
+ FileRoutines.createParentDirectories(imageFile);
+
+ out = new FileOutputStream(imageFile);
+ try {
+ IORoutines.copyStream(in, out);
+ reportChange(imageFile.getAbsolutePath());
+ }
+ finally {
+ out.close();
+ }
+ }
+ finally {
+ in.close();
+ }
+
+ ent.setFieldValue("icon_path", getBaseIconStoragePath() + filepath);
+ ent.setFieldValue("publish_path", filepath);
ent.update();
}
catch (Throwable e) {
- logger.error("MediaHandlerImages.produce: " + e.toString());
- throw new MediaFailure("MediaHandlerImages.produce: " + e.toString(), e);
+ logger.error("Error producing image", e);
+
+ throw new MediaFailure("Error producing image", e);
+ }
+ finally {
}
}
else {
- logger.error("MediaHandlerImages.produce: missing image or icon OID for: " + ent.getId());
+ logger.error("Can't produce image " + ent.getId() + ": missing image or icon OID");
- throw new MediaExc("MediaHandlerImages.produce: missing image or icon OID for: " + ent.getId());
+ throw new MediaExc("Can't produce image " + ent.getId() + ": missing image or icon OID");
}
}
-
+
public InputStream getThumbnail(Entity ent) throws MediaExc, MediaFailure {
- InputStream in;
try {
- in = ((EntityImages) ent).getIcon();
+ return new ByteArrayInputStream(((EntityImages) ent).getIcon());
}
catch (Throwable e) {
logger.error("MediaHandlerImages.getIcon: " + e.toString());
throw new MediaFailure(e);
}
-
- return in;
- }
-
- public List getURL(Entity ent, Entity mediaTypeEnt) {
- List theList = new Vector();
- theList.add(ent);
- return theList;
}
- public String getStoragePath() {
+ public String getBaseStoragePath() {
return configuration.getString("Producer.Image.Path");
}
- public String getIconStoragePath() {
+ public String getBaseIconStoragePath() {
return configuration.getString("Producer.Image.IconPath");
}