1
0
mirror of https://gitee.com/bookshelfplus/bookshelfplus synced 2025-09-22 01:30:40 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee

/file/list; /file/object/list 修改为POST提交;完善上传文件部分代码;清理多余import;修正一些代码Bug

This commit is contained in:
2022-04-15 21:38:57 +08:00
parent 0f6f148076
commit 5c1a935697
29 changed files with 1387 additions and 426 deletions

View File

@@ -4,15 +4,21 @@ import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import plus.bookshelf.Common.Enum.FileStorageMediumEnum;
import plus.bookshelf.Common.Error.BusinessErrorCode;
import plus.bookshelf.Common.Error.BusinessException;
import plus.bookshelf.Dao.DO.FileObjectDO;
import plus.bookshelf.Dao.Mapper.FileObjectDOMapper;
import plus.bookshelf.Service.Model.FileModel;
import plus.bookshelf.Service.Model.FileObjectModel;
import plus.bookshelf.Service.Model.UserModel;
import plus.bookshelf.Service.Service.FileObjectService;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
@Service
@@ -27,6 +33,9 @@ public class FileObjectServiceImpl implements FileObjectService {
@Autowired
UserServiceImpl userService;
@Autowired
FileServiceImpl fileService;
/**
* 列出所有文件对象
*
@@ -54,4 +63,139 @@ public class FileObjectServiceImpl implements FileObjectService {
BeanUtils.copyProperties(fileObjectDO, fileObjectModel);
return fileObjectModel;
}
/**
* 添加文件对象
* 返回是否添加成功
*
* @param fileObjectModel
* @return
* @throws InvocationTargetException
* @throws IllegalAccessException
*/
@Override
public Boolean addFileObject(FileObjectModel fileObjectModel) throws InvocationTargetException, IllegalAccessException {
FileObjectDO fileObjectDO = convertFromFileObjectModel(fileObjectModel);
int affectRows = fileObjectDOMapper.insertSelective(fileObjectDO);
return affectRows > 0;
}
private FileObjectDO convertFromFileObjectModel(FileObjectModel fileObjectModel) throws InvocationTargetException, IllegalAccessException {
FileObjectDO fileObjectDO = new FileObjectDO();
BeanUtils.copyProperties(fileObjectModel, fileObjectDO);
return fileObjectDO;
}
/**
* 向数据库中插入文件信息
*
* @param fileName 文件名
* @param filePath 文件路径
* @param fileSize 文件大小
* @param fileSHA1 文件SHA1
* @param fileExt 文件扩展名
* @param fileNameWithoutExt 文件名(不包含扩展名)
* @param fileStorageMediumEnum 文件存储介质
* @param bookOrigin 文件来源
* @return 是否插入成功
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws BusinessException
*/
@Override
@Transactional
public Boolean uploadFile(Integer fileId, String fileName, String filePath, Long fileSize, String fileSHA1,
String fileExt, String fileNameWithoutExt, FileStorageMediumEnum fileStorageMediumEnum,
String bookOrigin
) throws InvocationTargetException, IllegalAccessException, BusinessException {
if (fileId == 0) {
// 在数据库中创建新文件
FileModel fileModel = new FileModel();
fileModel.setFileName(fileName);
fileModel.setFileSize(fileSize);
fileModel.setHashSha1(fileSHA1);
fileModel.setFileFormat(fileExt);
fileModel.setFileDisplayName(fileNameWithoutExt);
fileModel.setBookOrigin(bookOrigin);
// 其余使用默认设置
fileModel.setBookId(0);
fileModel.setNumberOfPages(0);
fileModel.setWatermark(false);
fileModel.setAdvertising(false);
// 获取时间戳为 0 的时间 1970-01-01
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(0);
Date time = calendar.getTime();
fileModel.setFileCreateAt(time);
fileModel.setFileModifiedAt(time);
Boolean isSuccess = fileService.addFile(fileModel);
if (!isSuccess) {
throw new BusinessException(BusinessErrorCode.UNKNOWN_ERROR, "文件创建失败");
}
fileId = fileService.getLastInsertId();
}
FileObjectModel fileObjectModel = new FileObjectModel();
fileObjectModel.setFileId(fileId);
fileObjectModel.setFileName(fileName);
fileObjectModel.setFileSize(fileSize);
fileObjectModel.setFileType(fileExt);
fileObjectModel.setStorageMediumType(fileStorageMediumEnum.getStorageMediumName());
fileObjectModel.setFilePath(filePath);
fileObjectModel.setHashSha1(fileSHA1);
fileObjectModel.setUploadStatus("UPLOADING");
// 其余使用默认设置
fileObjectModel.setFilePwd("");
fileObjectModel.setFileShareCode("");
fileObjectModel.setAdditionalFields("");
Boolean isSuccess = addFileObject(fileObjectModel);
if (!isSuccess) {
throw new BusinessException(BusinessErrorCode.UNKNOWN_ERROR, "文件对象创建失败");
}
return true;
}
/**
* 修改文件对象上传状态信息
*
* @param fileObjectId
* @param fileStatus
*/
@Override
public Boolean updateFileStatus(Integer fileObjectId, String fileStatus) throws InvocationTargetException, IllegalAccessException {
if (fileObjectId == null || fileObjectId == 0) {
return false;
}
FileObjectModel fileObjectModel = new FileObjectModel();
fileObjectModel.setId(fileObjectId);
fileObjectModel.setUploadStatus(fileStatus);
FileObjectDO fileObjectDO = convertFromFileObjectModel(fileObjectModel);
int affectRows = fileObjectDOMapper.updateByPrimaryKeySelective(fileObjectDO);
return affectRows > 0;
}
/**
* 通过文件路径获取文件
*
* @param filePath 文件路径
* @return
*/
@Override
public FileObjectModel getFileObjectByFilePath(String filePath) throws InvocationTargetException, IllegalAccessException {
FileObjectDO fileObjectDO = fileObjectDOMapper.selectByFilePath(filePath);
FileObjectModel fileObjectModel = convertFromDataObject(fileObjectDO);
return fileObjectModel;
}
}

View File

@@ -1,14 +1,17 @@
package plus.bookshelf.Service.Impl;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import plus.bookshelf.Common.Error.BusinessException;
import plus.bookshelf.Config.QCloudCosConfig;
import plus.bookshelf.Dao.DO.FileDO;
import plus.bookshelf.Dao.Mapper.FileDOMapper;
import plus.bookshelf.Dao.Mapper.FileObjectDOMapper;
import plus.bookshelf.Service.Model.FileModel;
import plus.bookshelf.Service.Model.UserModel;
import plus.bookshelf.Service.Service.CosPresignedUrlGenerateLogService;
import plus.bookshelf.Service.Service.FileService;
import java.lang.reflect.InvocationTargetException;
@@ -27,18 +30,22 @@ public class FileServiceImpl implements FileService {
@Autowired
UserServiceImpl userService;
@Autowired
FileObjectDOMapper fileObjectDOMapper;
// @Autowired
// ScheduleTaskServiceImpl scheduleTaskService;
@Autowired
QCloudCosConfig qCloudCosConfig;
@Autowired
CosPresignedUrlGenerateLogService cosPresignedUrlGenerateLogService;
/**
* 列出所有文件
*
* @return
*/
@SneakyThrows
@Override
public List<FileModel> list(String token) throws InvocationTargetException, IllegalAccessException {
public List<FileModel> list(String token) throws InvocationTargetException, IllegalAccessException, BusinessException {
// 已经在 getUserByToken 方法中判断了 token 为空、不合法;用户不存在情况,此处无需再判断
UserModel userModel = userService.getUserByToken(redisTemplate, token);
@@ -60,26 +67,35 @@ public class FileServiceImpl implements FileService {
return fileModel;
}
// /**
// * 向数据库中添加一个 scheduleTask
// *
// * @param expireMinute
// * @param fileName
// * @param urlGUID
// * @param userId
// */
// @Override
// public void addScheduleTask(Integer expireMinute, String fileName, String urlGUID, Integer userId) {
// ScheduleTaskModel scheduleTaskModel = new ScheduleTaskModel();
// Calendar now = Calendar.getInstance();
// scheduleTaskModel.setCreateTime(now.getTime());
// now.add(Calendar.MILLISECOND, expireMinute * 60 * 1000);
// scheduleTaskModel.setScheduleTime(now.getTime());
// scheduleTaskModel.setAction(ScheduleTaskActionEnum.CHECK_FILE_IS_UPLOADED);
// scheduleTaskModel.setData(fileName);
// scheduleTaskModel.setTaskGuid(urlGUID);
// scheduleTaskModel.setAssociatedUserId(userId);
// scheduleTaskModel.setFailTime((byte) 0);
// scheduleTaskService.insertScheduleTask(scheduleTaskModel);
// }
/**
* 添加文件信息
* 返回是否添加成功
*
* @param fileModel
* @return
* @throws InvocationTargetException
* @throws IllegalAccessException
*/
@Override
public Boolean addFile(FileModel fileModel) throws InvocationTargetException, IllegalAccessException {
FileDO fileDO = copyFileToDataObject(fileModel);
int affectRows = fileDOMapper.insertSelective(fileDO);
return affectRows > 0;
}
private FileDO copyFileToDataObject(FileModel fileModel) throws InvocationTargetException, IllegalAccessException {
FileDO fileDO = new FileDO();
BeanUtils.copyProperties(fileModel, fileDO);
return fileDO;
}
/**
* 获取上一步添加的文件Id
*
* @return
*/
@Override
public Integer getLastInsertId() {
return fileDOMapper.getLastInsertId();
}
}

View File

@@ -7,7 +7,6 @@ import org.springframework.transaction.annotation.Transactional;
import plus.bookshelf.Common.Error.BusinessErrorCode;
import plus.bookshelf.Common.Error.BusinessException;
import plus.bookshelf.Common.SessionManager.RedisSessionManager;
import plus.bookshelf.Controller.VO.UserVO;
import plus.bookshelf.Dao.DO.ThirdPartyUserDO;
import plus.bookshelf.Dao.DO.UserDO;
import plus.bookshelf.Dao.Mapper.ThirdPartyUserAuthDOMapper;
@@ -18,7 +17,6 @@ import plus.bookshelf.Service.Service.UserService;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Service
public class UserServiceImpl implements UserService {