diff --git a/src/main/java/com/peteralbus/config/PermissionConfig.java b/src/main/java/com/peteralbus/config/PermissionConfig.java index dd1faac..424d57b 100644 --- a/src/main/java/com/peteralbus/config/PermissionConfig.java +++ b/src/main/java/com/peteralbus/config/PermissionConfig.java @@ -7,6 +7,7 @@ public class PermissionConfig { public static final String MODIFY_ARTICLE = "modify-article"; public static final String DELETE_COMMENT = "delete-comment"; public static final String COMMENT = "comment"; + public static final String DELETE_IMAGE = "delete-image"; public static final String ROLE_OWNER = "owner"; public static final String ROLE_ADMIN = "admin"; diff --git a/src/main/java/com/peteralbus/config/StpInterfaceImpl.java b/src/main/java/com/peteralbus/config/StpInterfaceImpl.java index 9cd97d0..5bbabac 100644 --- a/src/main/java/com/peteralbus/config/StpInterfaceImpl.java +++ b/src/main/java/com/peteralbus/config/StpInterfaceImpl.java @@ -32,9 +32,11 @@ public class StpInterfaceImpl implements StpInterface { list.add(PermissionConfig.USER_MANAGEMENT); list.add(PermissionConfig.WRITE_ARTICLE); list.add(PermissionConfig.MODIFY_ARTICLE); + list.add(PermissionConfig.DELETE_IMAGE); } if (user.getUserIdentity() <= 1) { list.add(PermissionConfig.DELETE_COMMENT); + list.add(PermissionConfig.DELETE_IMAGE); } if (user.getUserIdentity() <= 5) { list.add(PermissionConfig.COMMENT); diff --git a/src/main/java/com/peteralbus/controller/PhotoController.java b/src/main/java/com/peteralbus/controller/PhotoController.java index a1b2c4c..b45d3b6 100644 --- a/src/main/java/com/peteralbus/controller/PhotoController.java +++ b/src/main/java/com/peteralbus/controller/PhotoController.java @@ -1,5 +1,7 @@ package com.peteralbus.controller; +import cn.dev33.satoken.stp.StpUtil; +import com.peteralbus.config.PermissionConfig; import com.peteralbus.domain.Photo; import com.peteralbus.domain.Result; import com.peteralbus.service.PhotoService; @@ -26,6 +28,9 @@ public class PhotoController { @RequestMapping("/upload") public Result upload(@RequestParam("file") MultipartFile file, String imgName) { + if(file.getSize() > 1024 * 1024 * 512) { + return ResultUtil.error(500,"上传失败,文件大小超过512MB"); + } Map savedUrl = photoService.savePhoto(file, "blog/imgs/photo", true); if(savedUrl == null || savedUrl.get("url") == null || savedUrl.get("thumbnailUrl") == null) { return ResultUtil.error(500,"上传失败(上传或压缩失败),请联系管理员"); @@ -44,10 +49,39 @@ public class PhotoController { @PostMapping("/uploadOriginImg") public Result uploadOriginImg(@RequestParam("file") MultipartFile file, String path) { /*pathExample:blog/imgs/photo/*/ + // 文件大于1GB不允许上传 + if(file.getSize() > 1024 * 1024 * 512) { + return ResultUtil.error(500,"上传失败,文件大小超过512MB"); + } Map savedUrl = photoService.savePhoto(file, path, false); if(savedUrl == null || savedUrl.get("url") == null) { return ResultUtil.error(500,"上传失败,请联系管理员"); } return ResultUtil.success(savedUrl.get("url")); } + + @GetMapping("/deletePhotoByUrl") + public Result deletePhotoByUrl(String photoUrl) { + if(!StpUtil.hasPermission(PermissionConfig.DELETE_IMAGE)) { + return ResultUtil.error(403, "没有删除图片的权限!"); + } + return photoService.deletePhotoByUrl(photoUrl); + } + + @GetMapping("/deletePhotoById") + public Result deletePhotoById(Long photoId) { + if(!StpUtil.hasPermission(PermissionConfig.DELETE_IMAGE)) { + return ResultUtil.error(403, "没有删除图片的权限!"); + } + Photo photo = photoService.queryById(photoId); + int result = photoService.deletePhotoById(photoId); + Result deleteResult = photoService.deletePhotoByUrl(photo.getImgSrc()); + if(result == 0) { + return ResultUtil.error(500,"删除失败,请联系管理员"); + } + if(deleteResult.getCode() != 200) { + return ResultUtil.success(null, "删除成功,文件删除失败"); + } + return ResultUtil.success(null, "删除成功"); + } } diff --git a/src/main/java/com/peteralbus/service/PhotoService.java b/src/main/java/com/peteralbus/service/PhotoService.java index f7a63e0..ba4f736 100644 --- a/src/main/java/com/peteralbus/service/PhotoService.java +++ b/src/main/java/com/peteralbus/service/PhotoService.java @@ -1,6 +1,7 @@ package com.peteralbus.service; import com.peteralbus.domain.Photo; +import com.peteralbus.domain.Result; import org.springframework.web.multipart.MultipartFile; import java.util.List; @@ -21,6 +22,14 @@ public interface PhotoService */ List queryAll(); + /** + * Query by id photo. + * + * @param id the id + * @return the photo + */ + Photo queryById(Long id); + /** * Add int. * @@ -39,4 +48,18 @@ public interface PhotoService * @return the map */ Map savePhoto(MultipartFile file, String savePath, boolean isThumbnail); + + /** + * Delete photo by url. + * + * @param photoUrl the photo url + */ + Result deletePhotoByUrl(String photoUrl); + + /** + * Delete photo by id (in database). + * + * @param photoId the photo id + */ + int deletePhotoById(Long photoId); } diff --git a/src/main/java/com/peteralbus/service/impl/PhotoServiceImpl.java b/src/main/java/com/peteralbus/service/impl/PhotoServiceImpl.java index c6fd5a3..3b464ad 100644 --- a/src/main/java/com/peteralbus/service/impl/PhotoServiceImpl.java +++ b/src/main/java/com/peteralbus/service/impl/PhotoServiceImpl.java @@ -1,8 +1,10 @@ package com.peteralbus.service.impl; import com.peteralbus.domain.Photo; +import com.peteralbus.domain.Result; import com.peteralbus.mapper.PhotoMapper; import com.peteralbus.service.PhotoService; +import com.peteralbus.util.ResultUtil; import com.peteralbus.util.TypeUtil; import lombok.RequiredArgsConstructor; import net.coobird.thumbnailator.Thumbnails; @@ -34,6 +36,11 @@ public class PhotoServiceImpl implements PhotoService return photoMapper.selectList(null); } + @Override + public Photo queryById(Long id) { + return photoMapper.selectById(id); + } + @Override public int add(Photo photo) { @@ -64,4 +71,33 @@ public class PhotoServiceImpl implements PhotoService } return urlMap; } + + @Override + public Result deletePhotoByUrl(String photoUrl) { + if(!photoUrl.startsWith(BASE_URL)) { + return ResultUtil.error(400,"Not a deletable photo."); + } + String photoPath = photoUrl.replace(BASE_URL, BASE_PATH); + File file = new File(photoPath); + File thumbFile = new File(photoPath + "_THUMB.jpg"); + if(file.exists()) { + boolean result = file.delete(); + if(!result) { + return ResultUtil.error(500,"Unknown file system error."); + } + if(thumbFile.exists()) { + boolean thumbResult = thumbFile.delete(); + if(!thumbResult) { + return ResultUtil.success(null, "Delete success, but thumbnail delete failed."); + } + } + return ResultUtil.success(null, "Delete success."); + } + return ResultUtil.error(400,"File not exists."); + } + + @Override + public int deletePhotoById(Long photoId) { + return photoMapper.deleteById(photoId); + } } diff --git a/src/main/java/com/peteralbus/util/FileUtil.java b/src/main/java/com/peteralbus/util/FileUtil.java new file mode 100644 index 0000000..f40e212 --- /dev/null +++ b/src/main/java/com/peteralbus/util/FileUtil.java @@ -0,0 +1,49 @@ +package com.peteralbus.util; + +import com.peteralbus.domain.Photo; +import com.peteralbus.service.PhotoService; +import lombok.RequiredArgsConstructor; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.io.File; +import java.util.List; + +@Component +@RequiredArgsConstructor(onConstructor = @__(@Autowired)) +public class FileUtil { + private final PhotoService photoService; + static final String BASE_PATH = "/home/PeterAlbus/assets/"; + static final String BASE_URL = "https://file.peteralbus.com/assets/"; + + public Integer cleanPhotoTrash() { + // 列出BASE_PATH+blog/imgs/photo/文件夹下所有文件 + File folder = new File(BASE_PATH + "blog/imgs/photo/"); + File[] files = folder.listFiles(); + List photoList = photoService.queryAll(); + int count = 0; + if(files==null) { + return 0; + } + for (File file : files) { + boolean isExist = false; + for (Photo photo : photoList) { + if (file.getName().equals(photo.getImgSrc().replace(BASE_URL + "blog/imgs/photo/", ""))) { + isExist = true; + break; + } + if (file.getName().equals(photo.getImgThumb().replace(BASE_URL + "blog/imgs/photo/", ""))) { + isExist = true; + break; + } + } + if (!isExist) { + boolean result = file.delete(); + if(result) { + count++; + } + } + } + return 0; + } +}