try {
for (final FileItem item : fileItems) {
File fileObject = null;
if (item.getContentType().contains("image")) {
fileObject = new Photo();
} else {
fileObject = new File();
}
fileObject.setOwner(owner);
String parentFolderId = request.getParameter("parentFolder");
Folder parentFolder = null;
if (parentFolderId != null && !"".equals(parentFolderId)) {
parentFolder = (Folder) session.load(Folder.class, new Long(parentFolderId));
if (!SecurityHelper.doesUserHavePermission(session, owner, parentFolder, PERM.WRITE)) {
Cookie cookie = new Cookie(item.getFieldName(), "");
cookie.setMaxAge(0);
cookie.setPath("/");
response.addCookie(cookie);
response.sendError(HttpServletResponse.SC_FORBIDDEN);
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
response.flushBuffer();
Logger.log("Unauthorized upload requested: " + request.getRemoteAddr());
return;
}
}
fileObject.setParent(parentFolder);
fileObject.setContentType(item.getContentType());
String name = item.getName();
if (name.lastIndexOf("/") >= 0) {
name = name.substring(name.lastIndexOf("/") + 1);
}
if (name.lastIndexOf("\\") >= 0) {
name = name.substring(name.lastIndexOf("\\") + 1);
}
fileObject.setName(name);
fileObject.setDescription(name);
session.save(fileObject);
status.setStatus(FileUploadStatus.WRITING_FILE);
java.io.File outputPath = new java.io.File(java.io.File.separatorChar + "tmp" + java.io.File.separatorChar + BaseSystem.getDomainName(request));
java.io.File outputFile = new java.io.File(outputPath, fileObject.getId() + "_" + fileObject.getName());
outputPath.mkdirs();
item.write(outputFile);
tx = session.beginTransaction();
fileObject.setNameOnDisk(fileObject.getId() + "_" + fileObject.getName());
fileObject.setSize(outputFile.length());
session.save(fileObject);
tx.commit();
Logger.log("Wrote to file: " + outputFile.getCanonicalPath());
status.setStatus(FileUploadStatus.WRITING_DATABASE);
saveFileFromStream(fileObject, new FileInputStream(outputFile));
try {
// if we are uploading a photo, create slideshow (sane size) image and thumbnail
if (fileObject instanceof Photo) {
// convert file to png for faster inclusion in thumbnails
BufferedImage bi = javax.imageio.ImageIO.read(outputFile);
ByteArrayOutputStream pngOut = new ByteArrayOutputStream();
ImageIO.write(bi, "png", pngOut);
tx = session.beginTransaction();
Photo photo = (Photo) fileObject;
photo.setWidth(bi.getWidth());
photo.setHeight(bi.getHeight());
PhotoThumbnail thumbFile = new PhotoThumbnail();
thumbFile.setHidden(true);
thumbFile.setOwner(owner);
thumbFile.setParent(parentFolder);
thumbFile.setName(createFileName("", name, "_thumb"));
thumbFile.setDescription("Thumbnail for " + name);
session.save(thumbFile);
PhotoThumbnail previewFile = new PhotoThumbnail();
previewFile.setHidden(true);
previewFile.setOwner(owner);
previewFile.setParent(parentFolder);
previewFile.setName(createFileName("", name, "_preview"));
previewFile.setDescription("Preview image for " + name);
session.save(previewFile);
PhotoThumbnail slideFile = new PhotoThumbnail();
slideFile.setHidden(true);
slideFile.setOwner(owner);
slideFile.setParent(parentFolder);
slideFile.setName(createFileName("", name, "_slideshow"));
slideFile.setDescription("Slideshow image for " + name);
session.save(slideFile);
photo.setThumbnailImage(thumbFile);
photo.setSlideshowImage(slideFile);
photo.setPreviewImage(previewFile);
ByteArrayInputStream pngIn = new ByteArrayInputStream(pngOut.toByteArray());
ByteArrayOutputStream thumbOutStream = new ByteArrayOutputStream();