String id = request.getParameter("fileId");
Contexts.getSessionContext().set("LAST_ACCESS_ACTION", "File: " + id);
WikiUpload file = null;
if (!"".equals(id)) {
Long fileId = null;
try {
fileId = Long.valueOf(id);
} catch (NumberFormatException ex) {
response.sendError(HttpServletResponse.SC_NOT_FOUND, "File" + id);
}
WikiNodeDAO wikiNodeDAO = (WikiNodeDAO)org.jboss.seam.Component.getInstance(WikiNodeDAO.class);
file = wikiNodeDAO.findWikiUpload(fileId);
}
String contentType = null;
byte[] data = null;
String thumbnail = request.getParameter("thumbnail");
if (file != null
&& thumbnail != null
&& Boolean.valueOf(thumbnail)
&& file.isInstance(WikiUploadImage.class)
&& ((WikiUploadImage)file).getThumbnailData() != null
&& ((WikiUploadImage)file).getThumbnailData().length >0) {
// Render thumbnail picture
contentType = file.getContentType();
data = ((WikiUploadImage)file).getThumbnailData();
} else if (file != null && file.getData() != null && file.getData().length > 0) {
// Render file regularly
contentType = file.getContentType();
data = file.getData();
} else if (fileNotFoundImage != null) {
contentType = "image/png";
data = fileNotFoundImage;
}
if (data != null) {
response.setContentType(contentType);
response.setContentLength(data.length);
// If it's not a picture or if it's a picture that is an attachment, tell the browser to download
// the file instead of displaying it
// TODO: What about PDFs? Lot's of people want to show PDFs inline...
if ( file != null &&
( !file.isInstance(WikiUploadImage.class) || file.isAttachedToDocuments() )
) {
response.setHeader("Content-Disposition", "attachement; filename=\"" + file.getFilename() + "\"" );
}
response.getOutputStream().write(data);
}
response.getOutputStream().flush();