Add function of delete photo.

hikari
PeterAlbus 1 year ago
parent ae59472c0a
commit a652927c9b

@ -7,6 +7,7 @@ public class PermissionConfig {
public static final String MODIFY_ARTICLE = "modify-article"; public static final String MODIFY_ARTICLE = "modify-article";
public static final String DELETE_COMMENT = "delete-comment"; public static final String DELETE_COMMENT = "delete-comment";
public static final String COMMENT = "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_OWNER = "owner";
public static final String ROLE_ADMIN = "admin"; public static final String ROLE_ADMIN = "admin";

@ -32,9 +32,11 @@ public class StpInterfaceImpl implements StpInterface {
list.add(PermissionConfig.USER_MANAGEMENT); list.add(PermissionConfig.USER_MANAGEMENT);
list.add(PermissionConfig.WRITE_ARTICLE); list.add(PermissionConfig.WRITE_ARTICLE);
list.add(PermissionConfig.MODIFY_ARTICLE); list.add(PermissionConfig.MODIFY_ARTICLE);
list.add(PermissionConfig.DELETE_IMAGE);
} }
if (user.getUserIdentity() <= 1) { if (user.getUserIdentity() <= 1) {
list.add(PermissionConfig.DELETE_COMMENT); list.add(PermissionConfig.DELETE_COMMENT);
list.add(PermissionConfig.DELETE_IMAGE);
} }
if (user.getUserIdentity() <= 5) { if (user.getUserIdentity() <= 5) {
list.add(PermissionConfig.COMMENT); list.add(PermissionConfig.COMMENT);

@ -1,5 +1,7 @@
package com.peteralbus.controller; package com.peteralbus.controller;
import cn.dev33.satoken.stp.StpUtil;
import com.peteralbus.config.PermissionConfig;
import com.peteralbus.domain.Photo; import com.peteralbus.domain.Photo;
import com.peteralbus.domain.Result; import com.peteralbus.domain.Result;
import com.peteralbus.service.PhotoService; import com.peteralbus.service.PhotoService;
@ -26,6 +28,9 @@ public class PhotoController {
@RequestMapping("/upload") @RequestMapping("/upload")
public Result<?> upload(@RequestParam("file") MultipartFile file, String imgName) { public Result<?> upload(@RequestParam("file") MultipartFile file, String imgName) {
if(file.getSize() > 1024 * 1024 * 512) {
return ResultUtil.error(500,"上传失败文件大小超过512MB");
}
Map<String,String> savedUrl = photoService.savePhoto(file, "blog/imgs/photo", true); Map<String,String> savedUrl = photoService.savePhoto(file, "blog/imgs/photo", true);
if(savedUrl == null || savedUrl.get("url") == null || savedUrl.get("thumbnailUrl") == null) { if(savedUrl == null || savedUrl.get("url") == null || savedUrl.get("thumbnailUrl") == null) {
return ResultUtil.error(500,"上传失败(上传或压缩失败),请联系管理员"); return ResultUtil.error(500,"上传失败(上传或压缩失败),请联系管理员");
@ -44,10 +49,39 @@ public class PhotoController {
@PostMapping("/uploadOriginImg") @PostMapping("/uploadOriginImg")
public Result<?> uploadOriginImg(@RequestParam("file") MultipartFile file, String path) { public Result<?> uploadOriginImg(@RequestParam("file") MultipartFile file, String path) {
/*pathExample:blog/imgs/photo/*/ /*pathExample:blog/imgs/photo/*/
// 文件大于1GB不允许上传
if(file.getSize() > 1024 * 1024 * 512) {
return ResultUtil.error(500,"上传失败文件大小超过512MB");
}
Map<String,String> savedUrl = photoService.savePhoto(file, path, false); Map<String,String> savedUrl = photoService.savePhoto(file, path, false);
if(savedUrl == null || savedUrl.get("url") == null) { if(savedUrl == null || savedUrl.get("url") == null) {
return ResultUtil.error(500,"上传失败,请联系管理员"); return ResultUtil.error(500,"上传失败,请联系管理员");
} }
return ResultUtil.success(savedUrl.get("url")); 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, "删除成功");
}
} }

@ -1,6 +1,7 @@
package com.peteralbus.service; package com.peteralbus.service;
import com.peteralbus.domain.Photo; import com.peteralbus.domain.Photo;
import com.peteralbus.domain.Result;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.util.List; import java.util.List;
@ -21,6 +22,14 @@ public interface PhotoService
*/ */
List<Photo> queryAll(); List<Photo> queryAll();
/**
* Query by id photo.
*
* @param id the id
* @return the photo
*/
Photo queryById(Long id);
/** /**
* Add int. * Add int.
* *
@ -39,4 +48,18 @@ public interface PhotoService
* @return the map * @return the map
*/ */
Map<String,String> savePhoto(MultipartFile file, String savePath, boolean isThumbnail); Map<String,String> 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);
} }

@ -1,8 +1,10 @@
package com.peteralbus.service.impl; package com.peteralbus.service.impl;
import com.peteralbus.domain.Photo; import com.peteralbus.domain.Photo;
import com.peteralbus.domain.Result;
import com.peteralbus.mapper.PhotoMapper; import com.peteralbus.mapper.PhotoMapper;
import com.peteralbus.service.PhotoService; import com.peteralbus.service.PhotoService;
import com.peteralbus.util.ResultUtil;
import com.peteralbus.util.TypeUtil; import com.peteralbus.util.TypeUtil;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import net.coobird.thumbnailator.Thumbnails; import net.coobird.thumbnailator.Thumbnails;
@ -34,6 +36,11 @@ public class PhotoServiceImpl implements PhotoService
return photoMapper.selectList(null); return photoMapper.selectList(null);
} }
@Override
public Photo queryById(Long id) {
return photoMapper.selectById(id);
}
@Override @Override
public int add(Photo photo) public int add(Photo photo)
{ {
@ -64,4 +71,33 @@ public class PhotoServiceImpl implements PhotoService
} }
return urlMap; 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);
}
} }

@ -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<Photo> 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;
}
}
Loading…
Cancel
Save