mirror of
https://gitee.com/bookshelfplus/bookshelfplus
synced 2025-09-01 22:53:29 +08:00
文件上传成功首先调用Api更新一下文件上传状态;文件上传前,检查文件是否存在,如果存在则跳转到文件详情页
This commit is contained in:
@@ -28,6 +28,9 @@ public enum BusinessErrorCode implements CommonError {
|
||||
BOOK_FAVORITES_ALREADY_EXIST(50002, "书籍已经在收藏夹中"),
|
||||
BOOK_FAVORITES_NOT_EXIST(50003, "书籍不在收藏夹中"),
|
||||
|
||||
// 60000开头为文件、文件对象相关错误定义
|
||||
FILE_ALREADY_EXIST(60001, "文件已存在"),
|
||||
|
||||
// 占位
|
||||
PLACE_HOLDER(99999, "这是一个占位符错误");
|
||||
|
||||
|
@@ -100,10 +100,10 @@ public class FileController extends BaseController {
|
||||
}
|
||||
|
||||
@ApiOperation(value = "【管理员】查询文件列表(匹配文件哈希)", notes = "查询文件列表,返回文件哈希为空或者相同的文件")
|
||||
@RequestMapping(value = "list/MatchfileHash", method = {RequestMethod.POST}, consumes = {CONTENT_TYPE_FORMED})
|
||||
@RequestMapping(value = "list/MatchfileHashWithNullValue", method = {RequestMethod.POST}, consumes = {CONTENT_TYPE_FORMED})
|
||||
@ResponseBody
|
||||
public CommonReturnType matchfileHash(@RequestParam(value = "token", required = false) String token,
|
||||
@RequestParam(value = "fileSha1", required = true) String fileSha1) throws InvocationTargetException, IllegalAccessException, BusinessException {
|
||||
public CommonReturnType matchfileHashWithNullValue(@RequestParam(value = "token", required = false) String token,
|
||||
@RequestParam(value = "fileSha1", required = true) String fileSha1) throws InvocationTargetException, IllegalAccessException, BusinessException {
|
||||
|
||||
UserModel userModel = userService.getUserByToken(redisTemplate, token);
|
||||
if (userModel == null || !Objects.equals(userModel.getGroup(), "ADMIN")) {
|
||||
@@ -119,6 +119,22 @@ public class FileController extends BaseController {
|
||||
return CommonReturnType.create(fileVOS);
|
||||
}
|
||||
|
||||
@ApiOperation(value = "【管理员】通过文件SHA1哈希查找文件Id", notes = "查询文件列表,返回文件哈希匹配的文件Id")
|
||||
@RequestMapping(value = "getFileByHash", method = {RequestMethod.POST}, consumes = {CONTENT_TYPE_FORMED})
|
||||
@ResponseBody
|
||||
public CommonReturnType getFileByHash(@RequestParam(value = "token", required = false) String token,
|
||||
@RequestParam(value = "fileSha1", required = true) String fileSha1) throws InvocationTargetException, IllegalAccessException, BusinessException {
|
||||
|
||||
UserModel userModel = userService.getUserByToken(redisTemplate, token);
|
||||
if (userModel == null || !Objects.equals(userModel.getGroup(), "ADMIN")) {
|
||||
throw new BusinessException(BusinessErrorCode.OPERATION_NOT_ALLOWED, "非管理员用户无权进行此操作");
|
||||
}
|
||||
|
||||
FileModel fileModel = fileService.selectBySha1(fileSha1);
|
||||
FileVO fileVO = convertFileVOFromModel(fileModel);
|
||||
return CommonReturnType.create(fileVO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建文件操作预授权URL
|
||||
*
|
||||
@@ -182,13 +198,17 @@ public class FileController extends BaseController {
|
||||
switch (httpMethodName) {
|
||||
case PUT:
|
||||
// 上传文件
|
||||
if (isExist) throw new BusinessException(BusinessErrorCode.PARAMETER_VALIDATION_ERROR, "文件已存在");
|
||||
if (isExist) throw new BusinessException(BusinessErrorCode.FILE_ALREADY_EXIST, "文件已存在");
|
||||
|
||||
Integer realFileId = fileObjectService.uploadFile(fileId, fileName, bookSaveFolder + fileSha1, fileSize,
|
||||
Integer[] integers = fileObjectService.uploadFile(fileId, fileName, bookSaveFolder + fileSha1, fileSize,
|
||||
fileSha1, fileExt, FileStorageMediumEnum.QCLOUD_COS, "", lastModified);
|
||||
Integer realFileId = integers[0];
|
||||
Integer fileObjectId = integers[1];
|
||||
|
||||
// fileId 可能为 0 (创建新文件)
|
||||
// realFileId 是从数据库中查询出来的,真实的文件id
|
||||
resultMap.put("fileId", realFileId);
|
||||
resultMap.put("fileObjectId", fileObjectId);
|
||||
url = qCloudCosUtils.generatePresignedUrl(userModel.getId(), httpMethodName, bookSaveFolder, fileSha1, expireMinute, urlGUID);
|
||||
break;
|
||||
case GET:
|
||||
|
@@ -61,12 +61,19 @@ public interface FileDOMapper {
|
||||
FileDO[] selectAll();
|
||||
|
||||
/**
|
||||
* 查询系统中的所有文件
|
||||
* 查询系统中所有 SHA1匹配 和 未设置SHA1 的文件
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
FileDO[] selectBySha1WithNullValue(String fileSha1);
|
||||
|
||||
/**
|
||||
* 查询系统中一个 SHA1匹配 的文件
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
FileDO selectBySha1(String fileSha1);
|
||||
|
||||
/**
|
||||
* 列出文件支持的下载方式
|
||||
*
|
||||
|
@@ -75,4 +75,11 @@ public interface FileObjectDOMapper {
|
||||
* @return
|
||||
*/
|
||||
FileObjectDO[] selectFileObjectByBookId(Integer bookId);
|
||||
|
||||
/**
|
||||
* 获取上一次插入的主键Id
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
int getLastInsertId();
|
||||
}
|
@@ -116,7 +116,7 @@ public class FileObjectServiceImpl implements FileObjectService {
|
||||
* @param fileSize 文件大小
|
||||
* @param fileSHA1 文件SHA1
|
||||
* @param fileExt 文件扩展名
|
||||
* @param fileNameWithoutExt 文件名(不包含扩展名)
|
||||
* @param fileName 文件名(不包含扩展名)
|
||||
* @param fileStorageMediumEnum 文件存储介质
|
||||
* @param source 文件来源
|
||||
* @return 返回文件Id
|
||||
@@ -126,9 +126,9 @@ public class FileObjectServiceImpl implements FileObjectService {
|
||||
*/
|
||||
@Override
|
||||
@Transactional
|
||||
public Integer uploadFile(Integer fileId, String fileName, String filePath, Long fileSize, String fileSHA1,
|
||||
String fileExt, FileStorageMediumEnum fileStorageMediumEnum,
|
||||
String source, Long lastModified
|
||||
public Integer[] uploadFile(Integer fileId, String fileName, String filePath, Long fileSize, String fileSHA1,
|
||||
String fileExt, FileStorageMediumEnum fileStorageMediumEnum,
|
||||
String source, Long lastModified
|
||||
) throws InvocationTargetException, IllegalAccessException, BusinessException {
|
||||
|
||||
if (fileId == 0) {
|
||||
@@ -179,7 +179,11 @@ public class FileObjectServiceImpl implements FileObjectService {
|
||||
if (!isSuccess) {
|
||||
throw new BusinessException(BusinessErrorCode.UNKNOWN_ERROR, "文件对象创建失败");
|
||||
}
|
||||
return fileId;
|
||||
|
||||
int lastInsertId = fileObjectDOMapper.getLastInsertId();
|
||||
|
||||
// fileId, fileObjectId
|
||||
return new Integer[]{fileId, lastInsertId};
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -8,11 +8,9 @@ import plus.bookshelf.Common.Error.BusinessErrorCode;
|
||||
import plus.bookshelf.Common.Error.BusinessException;
|
||||
import plus.bookshelf.Config.QCloudCosConfig;
|
||||
import plus.bookshelf.Dao.DO.FileDO;
|
||||
import plus.bookshelf.Dao.DO.FileObjectDO;
|
||||
import plus.bookshelf.Dao.Mapper.FileDOMapper;
|
||||
import plus.bookshelf.Dao.Mapper.FileObjectDOMapper;
|
||||
import plus.bookshelf.Service.Model.FileModel;
|
||||
import plus.bookshelf.Service.Model.FileObjectModel;
|
||||
import plus.bookshelf.Service.Service.CosPresignedUrlGenerateLogService;
|
||||
import plus.bookshelf.Service.Service.FileService;
|
||||
|
||||
@@ -102,6 +100,18 @@ public class FileServiceImpl implements FileService {
|
||||
return fileModels;
|
||||
}
|
||||
|
||||
/**
|
||||
* 列出一个 SHA1匹配 的文件
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public FileModel selectBySha1(String fileSha1) {
|
||||
FileDO fileDO = fileDOMapper.selectBySha1(fileSha1);
|
||||
FileModel fileModel = convertFromDataObject(fileDO);
|
||||
return fileModel;
|
||||
}
|
||||
|
||||
private FileModel convertFromDataObject(FileDO fileDO) {
|
||||
if (fileDO == null) {
|
||||
return null;
|
||||
|
@@ -55,7 +55,7 @@ public interface FileObjectService {
|
||||
* @throws BusinessException
|
||||
*/
|
||||
@Transactional
|
||||
Integer uploadFile(Integer fileId, String fileName, String filePath, Long fileSize, String fileSHA1, String fileExt, FileStorageMediumEnum fileStorageMediumEnum, String source, Long lastModified) throws InvocationTargetException, IllegalAccessException, BusinessException;
|
||||
Integer[] uploadFile(Integer fileId, String fileName, String filePath, Long fileSize, String fileSHA1, String fileExt, FileStorageMediumEnum fileStorageMediumEnum, String source, Long lastModified) throws InvocationTargetException, IllegalAccessException, BusinessException;
|
||||
|
||||
/**
|
||||
* 修改文件对象上传状态信息
|
||||
|
@@ -32,6 +32,14 @@ public interface FileService {
|
||||
*/
|
||||
List<FileModel> selectBySha1WithNullValue(String token) throws InvocationTargetException, IllegalAccessException, BusinessException;
|
||||
|
||||
/**
|
||||
* 列出一个 SHA1匹配 的文件
|
||||
*
|
||||
* @param fileSha1
|
||||
* @return
|
||||
*/
|
||||
FileModel selectBySha1(String fileSha1);
|
||||
|
||||
/**
|
||||
* 添加文件信息
|
||||
* 返回是否添加成功
|
||||
|
Reference in New Issue
Block a user