?? attachmentwebhandler.java
字號:
}
public void processDelete(GenericRequest request)
throws BadInputException, DatabaseException, AuthenticationException, AssertionException, ObjectNotFoundException {
OnlineUser onlineUser = userManager.getOnlineUser(request);
MVNForumPermission permission = onlineUser.getPermission();
Locale locale = I18nUtil.getLocaleInRequest(request);
// user must have been authenticated before he can delete
permission.ensureIsAuthenticated();
// primary key column(s)
int attachID = GenericParamUtil.getParameterInt(request, "attach");
AttachmentBean attachmentBean = DAOFactory.getAttachmentDAO().getAttachment(attachID);
int postID = attachmentBean.getPostID();
PostBean postBean = null;
try {
postBean = DAOFactory.getPostDAO().getPost(postID);// can throw DatabaseException
} catch (ObjectNotFoundException ex) {
String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.ObjectNotFoundException.postid_not_exists", new Object[] {new Integer(postID)});
throw new ObjectNotFoundException(localizedMessage);
}
ForumCache.getInstance().getBean(postBean.getForumID()).ensureNotDisabledForum();
ForumCache.getInstance().getBean(postBean.getForumID()).ensureNotLockedForum();
// now, check the permission
permission.ensureCanDeletePost(postBean.getForumID());
// now check the password
MyUtil.ensureCorrectCurrentPassword(request);
// delete on disk
//AttachmentUtil.deleteAttachFilenameOnDisk(attachID);
BinaryStorage binaryStorage = ManagerFactory.getBinaryStorage();
try {
binaryStorage.deleteData(BinaryStorage.CATEGORY_POST_ATTACHMENT, String.valueOf(attachID), null);
} catch (IOException ex) {
log.error("Cannot delete file", ex);
// actually this exception is never existed
}
// delete in database
DAOFactory.getAttachmentDAO().delete(attachID);
// we dont want the exception to throw below this
int attachCount = DAOFactory.getAttachmentDAO().getNumberOfAttachments_inPost(postID);
DAOFactory.getPostDAO().updateAttachCount(postID, attachCount);
// Now update the post because the attachment count in this post is changed
postBean.setPostAttachCount(attachCount);
PostIndexer.scheduleUpdatePostTask(postBean);
int threadID = postBean.getThreadID();
int attachCountInThread = DAOFactory.getAttachmentDAO().getNumberOfAttachments_inThread(threadID);
DAOFactory.getThreadDAO().updateThreadAttachCount(threadID, attachCountInThread);
// Now clear the cache
PostCache.getInstance().clear();
request.setAttribute("ThreadID", String.valueOf(threadID));
}
/*
* @todo find a way to cache the file based on the http protocal
* @todo check permission
*/
public void downloadAttachment(HttpServletRequest request, HttpServletResponse response)
throws BadInputException, DatabaseException, ObjectNotFoundException, IOException,
AuthenticationException, AssertionException {
Locale locale = I18nUtil.getLocaleInRequest(request);
OnlineUser onlineUser = userManager.getOnlineUser(request);
MVNForumPermission permission = onlineUser.getPermission();
int attachID = ParamUtil.getParameterInt(request, "attach");
AttachmentBean attachBean = null;
try {
attachBean = DAOFactory.getAttachmentDAO().getAttachment(attachID);
} catch (ObjectNotFoundException e) {
String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.ObjectNotFoundException.attachmentid_not_exists", new Object[] {new Integer(attachID)});
throw new ObjectNotFoundException(localizedMessage);
}
int postID = attachBean.getPostID();
PostBean postBean = DAOFactory.getPostDAO().getPost(postID);
int forumID = postBean.getForumID();
ForumCache.getInstance().getBean(forumID).ensureNotDisabledForum();
//ForumCache.getInstance().getBean(forumID).ensureNotLockedForum(); // lock forum should allow download
if (MVNForumConfig.getEnableGuestViewImageAttachment() &&
attachBean.getAttachMimeType().startsWith("image/")) {
// When guest can view image attachment AND this attachment is image
// This is for security, at least in this case user must have permission to view post
permission.ensureCanReadPost(forumID);
} else {
// Please note that user does not have to have read permission
permission.ensureCanGetAttachment(forumID);
}
BinaryStorage binaryStorage = ManagerFactory.getBinaryStorage();
InputStream inputStream = binaryStorage.getInputStream(BinaryStorage.CATEGORY_POST_ATTACHMENT, String.valueOf(attachID), null);
/*
String attachFilename = AttachmentUtil.getAttachFilenameOnDisk(attachID);
File attachFile = new File(attachFilename);
if ((!attachFile.exists()) || (!attachFile.isFile())) {
log.error("Can't find a file " + attachFile + " to be downloaded (or maybe it's directory).");
String localizedMessage = MVNForumResourceBundle.getString(locale, "java.io.IOException.not_exist_or_not_file_to_be_downloaded");
//throw new IOException("Can't find a file to be downloaded (or maybe it's directory).");
}
*/
// we should not call this method after done the outputStream
// because we dont want exception after download
DAOFactory.getAttachmentDAO().increaseDownloadCount(attachID);
OutputStream outputStream = null;
try {
response.setContentType(attachBean.getAttachMimeType());
response.setHeader("Location", attachBean.getAttachFilename());
// now use Cache-Control if the MIME type are image
if (attachBean.getAttachMimeType().startsWith("image/")) {
long cacheTime = DateUtil.DAY * 30 / 1000;// 30 days
response.setHeader("Cache-Control", "max-age=" + cacheTime);
}
//added by Dejan
response.setHeader("Content-Disposition", "attachment; filename=" + attachBean.getAttachFilename());
// now, the header inited, just write the file content on the output
outputStream = response.getOutputStream();
//write using popFile when the file size's so large
try {
//FileUtil.popFile(attachFile, outputStream);
IOUtils.copy(inputStream, outputStream);
} catch (IOException ex) {
// CANNOT throw Exception after we output to the response
log.error("Error while trying to send attachment file from server: attachID = " + attachID + ".", ex);
}
//log.debug("Downloading attachment using FileUtil.popFile method");
outputStream.flush();
outputStream.close();
outputStream = null;// no close twice
} catch (IOException ex) {
throw ex;
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException ex) { }
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException ex) { }
}
}
}
/**
* NOTE: This method should be called before any attemp to delete a post
* because it require the post is exited
* After calling this method, go ahead and delete the post
*/
static void deleteAttachments_inPost(int postID) throws DatabaseException {
BinaryStorage binaryStorage = ManagerFactory.getBinaryStorage();
// First, try to delete attachment in database
Collection attachmentBeans = DAOFactory.getAttachmentDAO().getAttachments_inPost(postID);
DAOFactory.getAttachmentDAO().delete_inPost(postID);
//now delete files on disk
for (Iterator iter = attachmentBeans.iterator(); iter.hasNext(); ) {
AttachmentBean attachmentBean = (AttachmentBean)iter.next();
int attachID = attachmentBean.getAttachID();
//AttachmentUtil.deleteAttachFilenameOnDisk(attachmentBean.getAttachID());
try {
binaryStorage.deleteData(BinaryStorage.CATEGORY_POST_ATTACHMENT, String.valueOf(attachID), null);
} catch (IOException ex) {
log.error("Cannot delete file", ex);
// actually this exception is never existed
}
}
}
/**
* NOTE: This method should be called before any attemp to delete a thread
* because it require the thread is exited
* After calling this method, go ahead and delete the thread
*/
static void deleteAttachments_inThread(int threadID) throws DatabaseException {
BinaryStorage binaryStorage = ManagerFactory.getBinaryStorage();
// First, try to delete attachment in database
Collection attachmentBeans = DAOFactory.getAttachmentDAO().getAttachments_inThread(threadID);
//now delete files on disk
for (Iterator iter = attachmentBeans.iterator(); iter.hasNext(); ) {
AttachmentBean attachmentBean = (AttachmentBean)iter.next();
int attachID = attachmentBean.getAttachID();
//AttachmentUtil.deleteAttachFilenameOnDisk(attachID);
try {
binaryStorage.deleteData(BinaryStorage.CATEGORY_POST_ATTACHMENT, String.valueOf(attachID), null);
} catch (IOException ex) {
log.error("Cannot delete file", ex);
// actually this exception is never existed
}
try {
DAOFactory.getAttachmentDAO().delete(attachID);
} catch (Exception ex) {
log.warn("Cannot delete attachment (id = " + attachID + ") in database", ex);
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -