📄 binarystorageimpldisk.java
字号:
log.error("DatabaseException Error", e);
throw new IOException(e.getMessage());
} catch (AssertionException e) {
log.error("AssertionException Error", e);
throw new IOException(e.getMessage());
} finally {
try {
inputStream.close();
} catch (IOException e) {
log.debug("Cannot close input file", e);
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
log.debug("Cannot close output file", e);
}
}
}
} else {
throw new IllegalArgumentException("Not support category = " + category);
}
}
public InputStream getInputStream(String category, String nameId, BinaryStorageHandle handle)
throws IOException {
if (category.equals(CATEGORY_POST_ATTACHMENT)) {
int attachID = 0;
try {
attachID = Integer.parseInt(nameId);
} catch (NumberFormatException e) {
log.error("Cannot parse to an int value " + nameId, e);
throw new IllegalArgumentException("Cannot parse to an int value " + nameId);
}
String attachFilename = AttachmentUtil.getAttachFilenameOnDisk(attachID);
File attachFile = new File(attachFilename);
if (attachFile.exists() == false) {
throw new IOException("Can't find the file to be downloaded (" + attachFile + ")");
}
if (attachFile.isFile() == false) {
throw new IOException("The file to download is a directory.");
}
InputStream inputStream = new FileInputStream(attachFile);
return inputStream;
} else if (category.equals(CATEGORY_PM_ATTACHMENT)) {
int attachID = 0;
try {
attachID = Integer.parseInt(nameId);
} catch (NumberFormatException e) {
log.error("Cannot parse to an int value " + nameId, e);
throw new IllegalArgumentException("Cannot parse to an int value " + nameId);
}
String pmAttachFilename = AttachmentUtil.getPmAttachFilenameOnDisk(attachID);
File attachFile = new File(pmAttachFilename);
if (attachFile.exists() == false) {
throw new IOException("Can't find the file to be downloaded (" + attachFile + ")");
}
if (attachFile.isFile() == false) {
throw new IOException("The file to download is a directory.");
}
InputStream inputStream = new FileInputStream(attachFile);
return inputStream;
} else if (category.equals(CATEGORY_AVATAR)) {
int memberID = 0;
try {
memberID = Integer.parseInt(nameId);
} catch (NumberFormatException e) {
log.error("Cannot parse to an int value " + nameId, e);
throw new IllegalArgumentException("Cannot parse to an int value " + nameId);
}
try {
MemberBean memberBean = (MemberBean) DAOFactory.getMemberDAO().getMember_forPublic(memberID);
String memberAvatar = memberBean.getMemberAvatar();
if (memberAvatar.equals(MemberBean.MEMBER_AVATAR_USING_UPLOAD) ||
memberAvatar.startsWith(BinaryStorage.BINARY_STORAGE)||
memberAvatar.startsWith(MVNForumGlobal.UPLOADED_AVATAR_DIR)) {
memberAvatar = memberBean.getMemberName() + ".jpg";
} else {
throw new IOException("Assertion: Bad request for memberID = " + memberID);
}
File avatarFile = new File(MVNForumConfig.getAvatarDir() + File.separator + memberAvatar);
if (avatarFile.exists() == false) {
throw new IOException("Can't find the file to be downloaded (" + avatarFile + ")");
}
if (avatarFile.isFile() == false) {
throw new IOException("The file to download is a directory.");
}
InputStream inputStream = new FileInputStream(avatarFile);
return inputStream;
} catch (ObjectNotFoundException e) {
log.error("ObjectNotFoundException Error", e);
throw new IOException(e.getMessage());
} catch (DatabaseException e) {
log.error("DatabaseException Error", e);
throw new IOException(e.getMessage());
}
} else {
throw new IllegalArgumentException("Not support category = " + category);
}
}
public void deleteData(String category, String nameId, BinaryStorageHandle handle)
throws IOException {
if (category.equals(CATEGORY_POST_ATTACHMENT)) {
int attachID = 0;
try {
attachID = Integer.parseInt(nameId);
} catch (NumberFormatException e) {
log.error("Cannot parse to an int value " + nameId, e);
throw new IllegalArgumentException("Cannot parse to an int value " + nameId);
}
String filename = AttachmentUtil.getAttachFilenameOnDisk(attachID);
try {
log.info("About to delete post attachment = " + filename);
FileUtil.deleteFile(filename);
} catch (Exception ex) {
log.warn("Cannot delete post attachment file " + filename, ex);
//@todo schedule to delete it later
}
} else if (category.equals(CATEGORY_PM_ATTACHMENT)) {
int attachID = 0;
try {
attachID = Integer.parseInt(nameId);
} catch (NumberFormatException e) {
log.error("Cannot parse to an int value " + nameId, e);
throw new IllegalArgumentException("Cannot parse to an int value " + nameId);
}
String filename = AttachmentUtil.getPmAttachFilenameOnDisk(attachID);
try {
log.info("About to delete pmAttachment = " + filename);
FileUtil.deleteFile(filename);
} catch (Exception ex) {
log.warn("Cannot delete pmAttachment file " + filename, ex);
//@todo schedule to delete it later
}
} else if (category.equals(CATEGORY_AVATAR)) {
int memberID = 0;
try {
memberID = Integer.parseInt(nameId);
} catch (NumberFormatException e) {
log.error("Cannot parse to an int value " + nameId, e);
throw new IllegalArgumentException("Cannot parse to an int value " + nameId);
}
try {
MemberBean memberBean = DAOFactory.getMemberDAO().getMember_forPublic(memberID);
String memberName = memberBean.getMemberName();
StringBuffer bufferPicFile = new StringBuffer(128);
bufferPicFile.append(MVNForumConfig.getAvatarDir());
bufferPicFile.append(File.separatorChar).append(memberName).append(".jpg");
String picFile = bufferPicFile.toString();
log.debug("Delete avatar = " + picFile);
File file = new File(picFile);
file.delete();// we dont need to check the returned value
} catch (Exception ex) {
log.warn("Cannot delete avatar file ", ex);
throw new IOException(ex.getMessage());
}
} else {
throw new IllegalArgumentException("Not support category = " + category);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -