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

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