?? attachmentwebhandler.java
字號:
String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_process_upload_with_file_size_is_zero");
throw new BadInputException(localizedMessage);
//throw new BadInputException("Cannot process an attach file with size = 0. Please check the file size or check if your file is missing.");
}
String fullFilePath = currentFileItem.getName();
attachFilename = FileUtil.getFileName(fullFilePath);
attachFilename = DisableHtmlTagFilter.filter(attachFilename);
log.debug("attachFilename = " + attachFilename);
// now save to attachFileItem
attachFileItem = currentFileItem;
} else {
// maybe, we don't care about the redundant fields.
// Should we uncomment the exception statement ?
String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.AssertionException.cannot_process_field_name", new Object[] {fieldName});
throw new AssertionException(localizedMessage);
//throw new AssertionException("Cannot process field name = " + fieldName);
}
}
Timestamp now = DateUtil.getCurrentGMTTimestamp();
// check constraint
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);
}
int forumID = postBean.getForumID();
permission.ensureCanAddAttachment(forumID);
ForumCache.getInstance().getBean(forumID).ensureNotDisabledForum();
ForumCache.getInstance().getBean(forumID).ensureNotLockedForum();
ForumCache.getInstance().getBean(forumID).ensureNotClosedForum();
int logonMemberID = onlineUser.getMemberID();
//String logonMemberName = onlineUser.getMemberName();
int authorID = postBean.getMemberID();
// check constraint
if (permission.canEditPost(forumID)) { //@todo is this the correct permission checking ??? Igor: yes it is
// have permission, just do nothing, that is dont check the max day contraint
} else if ( (logonMemberID==authorID) && onlineUser.isMember() ) {
// same author, but not guest
// check date here, usually must not older than 1 days
Timestamp postDate = postBean.getPostCreationDate();
int maxDays = MVNForumConfig.getMaxAttachDays();
if ( (now.getTime() - postDate.getTime()) > (DateUtil.DAY * maxDays) ) {
/** @todo choose a better Exception here */
String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.BadInputException.attach_into_old_post", new Object[] {new Integer(maxDays)});
throw new BadInputException(localizedMessage);
//throw new BadInputException("You cannot attach a file to a post which is older than " + maxDays + " days.");
}
/** @todo check status of first post of thread always enable */
if (postBean.getPostStatus() == PostBean.POST_STATUS_DISABLED) {
//@todo : localize me
throw new BadInputException("Cannot attach a file to disabled post.");
}
} else {//not an author, so this user must have Edit Permission
//@todo is this the correct permission checking ??? Igor: yes it is
permission.ensureCanEditPost(forumID);// this method ALWAYS throws AuthenticationException
}
// now all contraints/permission have been checked
// values that we can init now
String attachCreationIP = request.getRemoteAddr();
Timestamp attachCreationDate= now;
Timestamp attachModifiedDate= now;
int attachDownloadCount = 0;
int attachOption = 0;// check it
int attachStatus = 0;// check it
int attachID = DAOFactory.getAttachmentDAO().createAttachment(postID, logonMemberID, attachFilename,
attachFileSize, attachMimeType, attachDesc,
attachCreationIP, attachCreationDate, attachModifiedDate,
attachDownloadCount, attachOption, attachStatus);
try {
// String filename = AttachmentUtil.getAttachFilenameOnDisk(attachID);
// log.debug("Attach filename to save to file system = " + filename);
// attachFileItem.write(new File(filename));
BinaryStorage binaryStorage = ManagerFactory.getBinaryStorage();
binaryStorage.storeData(BinaryStorage.CATEGORY_POST_ATTACHMENT, String.valueOf(attachID), attachFilename,
attachFileItem.getInputStream(), attachFileSize, 0, 0, attachMimeType, attachCreationIP);
} catch (Exception ex) {
log.error("Cannot save the attachment file", ex);
DAOFactory.getAttachmentDAO().delete(attachID);
String localizedMessage = MVNForumResourceBundle.getString(locale, "java.io.IOException.cannot_save_attach_file");
throw new IOException(localizedMessage);
//throw new IOException("Cannot save the attachment file to the file system.");
}
// we dont want the exception to throw below this
int threadID = postBean.getThreadID();
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 attachCountInThread = DAOFactory.getAttachmentDAO().getNumberOfAttachments_inThread(threadID);
DAOFactory.getThreadDAO().updateThreadAttachCount(threadID, attachCountInThread);
// Now clear the cache
PostCache.getInstance().clear();
request.setAttribute("ForumID", String.valueOf(forumID));
request.setAttribute("ThreadID", String.valueOf(threadID));
request.setAttribute("PostID", String.valueOf(postID));
request.setAttribute("offset", String.valueOf(offset));
request.setAttribute("AttachMore", new Boolean(attachMore));
}
public void prepareEdit(GenericRequest request)
throws ObjectNotFoundException, BadInputException, DatabaseException, AuthenticationException, AssertionException {
OnlineUser onlineUser = userManager.getOnlineUser(request);
MVNForumPermission permission = onlineUser.getPermission();
Locale locale = I18nUtil.getLocaleInRequest(request);
// primary key column(s)
int attachID = GenericParamUtil.getParameterInt(request, "attach");
AttachmentBean attachmentBean = null;
try {
attachmentBean = 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 = 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);
}
// now, check the permission
permission.ensureCanEditPost(postBean.getForumID());
ForumCache.getInstance().getBean(postBean.getForumID()).ensureNotDisabledForum();
ForumCache.getInstance().getBean(postBean.getForumID()).ensureNotLockedForum();
request.setAttribute("AttachmentBean", attachmentBean);
request.setAttribute("PostBean", postBean);
}
public void processEdit(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.ensureCanEditPost(postBean.getForumID());
// now check the password
MyUtil.ensureCorrectCurrentPassword(request);
int threadID = postBean.getThreadID();
// delete in database
String newDesc = GenericParamUtil.getParameter(request, "newdesc");
DAOFactory.getAttachmentDAO().updateAttachDesc(attachID, newDesc);
request.setAttribute("ThreadID", String.valueOf(threadID));
}
public void prepareDelete(GenericRequest request)
throws ObjectNotFoundException, BadInputException, DatabaseException, AuthenticationException, AssertionException {
OnlineUser onlineUser = userManager.getOnlineUser(request);
MVNForumPermission permission = onlineUser.getPermission();
Locale locale = I18nUtil.getLocaleInRequest(request);
// primary key column(s)
int attachID = GenericParamUtil.getParameterInt(request, "attach");
AttachmentBean attachmentBean = null;
try {
attachmentBean = 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 = 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);
}
// now, check the permission
permission.ensureCanDeletePost(postBean.getForumID());
ForumCache.getInstance().getBean(postBean.getForumID()).ensureNotDisabledForum();
ForumCache.getInstance().getBean(postBean.getForumID()).ensureNotLockedForum();
request.setAttribute("AttachmentBean", attachmentBean);
request.setAttribute("PostBean", postBean);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -