package org.aplikator.server.data;
import javax.activation.MimeTypeParseException;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.aplikator.server.ImageResizer;
import org.aplikator.server.descriptor.BinaryProperty;
import org.aplikator.utils.IOUtils;
import org.aplikator.utils.PDFLoader;
import com.google.common.io.ByteStreams;
/**
* Wrapper class for holding binary data (byte array) and corresponding
* properties.
* Provides also convenience constructors for initialization of the properties
* based on the filename.
*/
public class BinaryData implements Serializable {
/**
* Property key for filename
*/
public static final String FILENAME_KEY = "filename";
public static final String MIME_TYPE = "mimetype";
public static final String IMG_WIDTH = "imgWidth";
public static final String IMG_HEIGHT = "imgHeight";
private static final long serialVersionUID = 3328753266953957306L;
private static final Logger LOG = Logger.getLogger(BinaryData.class.getName());
/**
* Binary data placeholder - provide inputStream and size of the data
*/
public transient InputStream inputStream;
public long dataLength;
private String tempFileId;
private BinaryProperty binaryProperty;
/**
* Properties placeholder, initialized upon instance creation
*/
public Map<String, String> properties = new HashMap<String, String>();
public byte[] data = null;
public byte[] preview = null;
public byte[] icon = null;
/**
* Default constructor
*/
public BinaryData(BinaryProperty binaryProperty) {
this.binaryProperty = binaryProperty;
}
/**
* Convenience constructor, adds the filename to the field properties
*/
private BinaryData(String filename) {
properties.put(FILENAME_KEY, filename);
}
/**
* Convenience constructor, adds the filename to the field properties and
* sets the temporaryFile field.
*/
public BinaryData(BinaryProperty binaryProperty, String filename, InputStream inputStream, long dataLength, String tempFileId) {
this(filename);
this.inputStream = inputStream;
this.dataLength = dataLength;
this.tempFileId = tempFileId;
this.binaryProperty = binaryProperty;
if (filename != null) {
javax.activation.MimeType mime;
try {
mime = ImageResizer.getMimeType(filename);
} catch (MimeTypeParseException e) {
throw new IllegalStateException("Unable to parse mime type of:" + filename, e);
}
this.properties.put(BinaryData.MIME_TYPE, mime.getPrimaryType() + "/" + mime.getSubType());
}
}
public void preprocess(int thumbnailSize, int previewSize) {
String filename = properties.get(BinaryData.FILENAME_KEY);
if (ImageResizer.isResizeable(binaryProperty, filename)) {
BufferedImage imageIcon = null;
try {
data = ByteStreams.toByteArray(inputStream);
InputStream dataByteStream = new ByteArrayInputStream(data);
if (ImageResizer.isPdf(filename)) {
imageIcon = PDFLoader.load(dataByteStream);
} else {
imageIcon = ImageIO.read(dataByteStream);
}
IOUtils.tryClose(dataByteStream);
properties.put(BinaryData.IMG_HEIGHT, String.valueOf(imageIcon.getHeight()));
properties.put(BinaryData.IMG_WIDTH, String.valueOf(imageIcon.getWidth()));
imageIcon = ImageResizer.removeAlphaChannel(imageIcon);
icon = ImageResizer.resize(imageIcon, thumbnailSize);
preview = ImageResizer.resize(imageIcon, previewSize);
} catch (IOException e) {
LOG.log(Level.SEVERE, "Error while copying image data:", e);
throw new RuntimeException("Error while copying image data:", e);
} finally {
if (imageIcon != null) {
imageIcon.flush();
}
}
}
}
public String getTempFileId() {
return tempFileId;
}
}