protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
super.processTemplate(request, templateContext);
if (request.getMethod() == Method.POST) {
Sone currentSone = getCurrentSone(request.getToadletContext());
String parentId = request.getHttpRequest().getPartAsStringFailsafe("parent", 36);
Album parent = webInterface.getCore().getAlbum(parentId, false);
if (parent == null) {
/* TODO - signal error */
return;
}
if (!currentSone.equals(parent.getSone())) {
/* TODO - signal error. */
return;
}
String name = request.getHttpRequest().getPartAsStringFailsafe("title", 200);
String description = request.getHttpRequest().getPartAsStringFailsafe("description", 4000);
HTTPUploadedFile uploadedFile = request.getHttpRequest().getUploadedFile("image");
Bucket fileBucket = uploadedFile.getData();
InputStream imageInputStream = null;
ByteArrayOutputStream imageDataOutputStream = null;
net.pterodactylus.sone.data.Image image = null;
try {
imageInputStream = fileBucket.getInputStream();
/* TODO - check length */
imageDataOutputStream = new ByteArrayOutputStream((int) fileBucket.size());
ByteStreams.copy(imageInputStream, imageDataOutputStream);
} catch (IOException ioe1) {
logger.log(Level.WARNING, "Could not read uploaded image!", ioe1);
return;
} finally {
fileBucket.free();
Closer.close(imageInputStream);
Closer.close(imageDataOutputStream);
}
byte[] imageData = imageDataOutputStream.toByteArray();
ByteArrayInputStream imageDataInputStream = null;
Image uploadedImage = null;
try {
imageDataInputStream = new ByteArrayInputStream(imageData);
uploadedImage = ImageIO.read(imageDataInputStream);
if (uploadedImage == null) {
templateContext.set("messages", webInterface.getL10n().getString("Page.UploadImage.Error.InvalidImage"));
return;
}
String mimeType = getMimeType(imageData);
TemporaryImage temporaryImage = webInterface.getCore().createTemporaryImage(mimeType, imageData);
image = webInterface.getCore().createImage(currentSone, parent, temporaryImage);
image.modify().setTitle(name).setDescription(TextFilter.filter(request.getHttpRequest().getHeader("host"), description)).setWidth(uploadedImage.getWidth(null)).setHeight(uploadedImage.getHeight(null)).update();
} catch (IOException ioe1) {
logger.log(Level.WARNING, "Could not read uploaded image!", ioe1);
return;
} finally {
Closer.close(imageDataInputStream);
Closer.flush(uploadedImage);
}
throw new RedirectException("imageBrowser.html?album=" + parent.getId());
}
}