🐛 修复附件删除失败的问题

pull/89/head^2
ruibaby 2019-01-31 12:54:16 +08:00
parent 7ffbc93ea5
commit 612504926c
1 changed files with 20 additions and 22 deletions

View File

@ -8,7 +8,7 @@ import cc.ryanc.halo.model.enums.ResultCodeEnum;
import cc.ryanc.halo.service.AttachmentService;
import cc.ryanc.halo.service.LogsService;
import cc.ryanc.halo.utils.LocaleMessageUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.text.StrBuilder;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
@ -181,48 +181,46 @@ public class AttachmentController {
@ResponseBody
public JsonResult removeAttachment(@RequestParam("attachId") Long attachId,
HttpServletRequest request) {
Optional<Attachment> attachment = attachmentService.findByAttachId(attachId);
String attachLocation = attachment.get().getAttachLocation();
String delFileName = attachment.get().getAttachName();
final Attachment attachment = attachmentService.findByAttachId(attachId).orElse(new Attachment());
final String attachLocation = attachment.getAttachLocation();
final String attachName = attachment.getAttachName();
final String attachPath = attachment.getAttachPath();
boolean flag = true;
try {
//删除数据库中的内容
attachmentService.remove(attachId);
if (attachLocation != null) {
if (attachLocation.equals(SERVER.getDesc())) {
String delSmallFileName = delFileName.substring(0, delFileName.lastIndexOf('.')) + "_small" + attachment.get().getAttachSuffix();
//删除文件
String userPath = System.getProperties().getProperty("user.home") + "/halo";
File mediaPath = new File(userPath, attachment.get().getAttachPath().substring(0, attachment.get().getAttachPath().lastIndexOf('/')));
File delFile = new File(new StringBuffer(mediaPath.getAbsolutePath()).append("/").append(delFileName).toString());
File delSmallFile = new File(new StringBuffer(mediaPath.getAbsolutePath()).append("/").append(delSmallFileName).toString());
StrBuilder userPath = new StrBuilder(System.getProperties().getProperty("user.home"));
userPath.append("/halo");
//图片物理地址
StrBuilder delPath = new StrBuilder(userPath);
delPath.append(attachPath);
//缩略图物理地址
StrBuilder delSmallPath = new StrBuilder(userPath);
delSmallPath.append(attachment.getAttachSmallPath());
File delFile = new File(delPath.toString());
File delSmallFile = new File(delSmallPath.toString());
if (delFile.exists() && delFile.isFile()) {
flag = delFile.delete() && delSmallFile.delete();
}
} else if (attachLocation.equals(QINIU.getDesc())) {
//七牛删除
String attachPath = attachment.get().getAttachPath();
String key = attachPath.substring(attachPath.lastIndexOf("/") + 1);
flag = attachmentService.deleteQiNiuAttachment(key);
} else if (attachLocation.equals(UPYUN.getDesc())) {
//又拍删除
String attachPath = attachment.get().getAttachPath();
String fileName = attachPath.substring(attachPath.lastIndexOf("/") + 1);
flag = attachmentService.deleteUpYunAttachment(fileName);
} else {
//..
}
}
if (flag) {
log.info("Delete file {} successfully!", delFileName);
logsService.save(LogsRecord.REMOVE_FILE, delFileName, request);
attachmentService.remove(attachId);
log.info("Delete file {} successfully!", attachName);
logsService.save(LogsRecord.REMOVE_FILE, attachName, request);
} else {
log.error("Deleting attachment {} failed!", delFileName);
log.error("Deleting attachment {} failed!", attachName);
return new JsonResult(ResultCodeEnum.FAIL.getCode(), localeMessageUtil.getMessage("code.admin.common.delete-failed"));
}
} catch (Exception e) {
e.printStackTrace();
log.error("Deleting attachment {} failed: {}", delFileName, e.getMessage());
log.error("Deleting attachment {} failed: {}", attachName, e.getMessage());
return new JsonResult(ResultCodeEnum.FAIL.getCode(), localeMessageUtil.getMessage("code.admin.common.delete-failed"));
}
return new JsonResult(ResultCodeEnum.SUCCESS.getCode(), localeMessageUtil.getMessage("code.admin.common.delete-success"));