1
0
mirror of https://gitee.com/bookshelfplus/bookshelfplus synced 2025-09-01 22:53:29 +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

@@ -1,18 +1,20 @@
package plus.bookshelf.Common.Enum;
public enum FileStorageMediumEnum {
LOCAL (10000, "本地"),
BAIDU_NETDISK (20001, "百度网盘"),
ALIYUN_DRIVE (20002, "阿里网盘");
LOCAL("LOCAL", "本地"),
QCLOUD_COS("QCLOUD_COS", "腾讯云对象存储"),
BAIDU_NETDISK("BAIDU_NETDISK", "百度网盘"),
ALIYUN_DRIVE("ALIYUN_DRIVE", "阿里网盘");
private FileStorageMediumEnum(int storageMediumIndex, String storageMediumDisplayName) {
private FileStorageMediumEnum(String storageMediumIndex, String storageMediumDisplayName) {
this.storageMediumIndex = storageMediumIndex;
this.storageMediumDisplayName = storageMediumDisplayName;
}
private Integer storageMediumIndex;
private String storageMediumIndex;
private String storageMediumDisplayName;
public Integer getStorageMediumIndex() {
public String getStorageMediumName() {
return storageMediumIndex;
}

View File

@@ -1,6 +1,5 @@
package plus.bookshelf.Common.FileManager;
import com.aventrix.jnanoid.jnanoid.NanoIdUtils;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;

View File

@@ -2,7 +2,6 @@ package plus.bookshelf.Common.SessionManager;
import org.springframework.data.redis.core.RedisTemplate;
import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;
public class RedisSessionManager implements SessionManager {

View File

@@ -6,7 +6,6 @@ import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

View File

@@ -1,5 +1,8 @@
package plus.bookshelf.Controller.Controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aventrix.jnanoid.jnanoid.NanoIdUtils;
import com.qcloud.cos.http.HttpMethodName;
import io.swagger.annotations.Api;
@@ -25,10 +28,7 @@ import plus.bookshelf.Service.Model.UserModel;
import plus.bookshelf.Service.Service.CosPresignedUrlGenerateLogService;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
@Api(tags = "文件管理")
@Controller("file")
@@ -42,10 +42,10 @@ public class FileController extends BaseController {
UserServiceImpl userService;
@Autowired
CosPresignedUrlGenerateLogService cosPresignedUrlGenerateLogService;
FileServiceImpl fileService;
@Autowired
FileServiceImpl fileService;
CosPresignedUrlGenerateLogService cosPresignedUrlGenerateLogService;
@Autowired
FileObjectServiceImpl fileObjectService;
@@ -54,7 +54,7 @@ public class FileController extends BaseController {
// ScheduleTaskServiceImpl scheduleTaskService;
@ApiOperation(value = "查询文件列表", notes = "查询文件列表")
@RequestMapping(value = "list", method = {RequestMethod.GET})
@RequestMapping(value = "list", method = {RequestMethod.POST}, consumes = {CONTENT_TYPE_FORMED})
@ResponseBody
public CommonReturnType list(@RequestParam(value = "token", required = false) String token) throws InvocationTargetException, IllegalAccessException, BusinessException {
List<FileModel> fileModels = fileService.list(token);
@@ -75,7 +75,7 @@ public class FileController extends BaseController {
}
@ApiOperation(value = "查询文件对象列表", notes = "查询文件列表")
@RequestMapping(value = "object/list", method = {RequestMethod.GET})
@RequestMapping(value = "object/list", method = {RequestMethod.POST}, consumes = {CONTENT_TYPE_FORMED})
@ResponseBody
public CommonReturnType objectList(@RequestParam(value = "token", required = false) String token) throws InvocationTargetException, IllegalAccessException, BusinessException {
List<FileObjectModel> fileObjectModels = fileObjectService.list(token);
@@ -113,8 +113,16 @@ public class FileController extends BaseController {
@ResponseBody
public CommonReturnType cos(@PathVariable(value = "httpMethod") String httpMethod,
@RequestParam(value = "token") String token,
@RequestParam(value = "fileName") String fileName,
@RequestParam(value = "expireMinute", required = false) Integer expireMinute) throws BusinessException {
@RequestParam(value = "fileName") String fileName, // 不含扩展名
@RequestParam(value = "expireMinute", required = false) Integer expireMinute,
// 以下为 PUT 请求必传参数
@RequestParam(value = "fileSize", required = false) Long fileSize,
@RequestParam(value = "fileType", required = false) String fileType,
@RequestParam(value = "fileSha1", required = false) String fileSha1,
@RequestParam(value = "fileExt", required = false) String fileExt,
@RequestParam(value = "fileId", required = false) Integer fileId // 关联的文件ID创建新文件则为0
) throws BusinessException, InvocationTargetException, IllegalAccessException {
if (expireMinute == null) {
expireMinute = 30;
} else if (expireMinute > 60) {
@@ -144,10 +152,11 @@ public class FileController extends BaseController {
Boolean isExist = qCloudCosUtils.doesObjectExist(bookSaveFolder, fileName);
switch (httpMethodName) {
case PUT:
// 上传文件
if (isExist) throw new BusinessException(BusinessErrorCode.PARAMETER_VALIDATION_ERROR, "文件已存在");
// 添加一个scheduleTask用于检测用户是否上传了文件然后更新数据库中信息
// TODO
// fileService.addScheduleTask(expireMinute, bookSaveFolder, urlGUID, userModel.getId());
fileObjectService.uploadFile(fileId, fileName, bookSaveFolder + fileName, fileSize,
fileSha1, fileExt, fileName, FileStorageMediumEnum.QCLOUD_COS, "");
break;
case GET:
if (!isExist) throw new BusinessException(BusinessErrorCode.PARAMETER_VALIDATION_ERROR, "文件不存在");
@@ -163,26 +172,135 @@ public class FileController extends BaseController {
Map map = new HashMap();
map.put("url", url);
map.put("urlGUID", urlGUID);
map.put("guid", urlGUID);
return CommonReturnType.create(map);
}
@ApiOperation(value = "", notes = "客户端向腾讯云 COS 存储桶上传文件完毕")
@RequestMapping(value = "/upload/finish-upload", method = {RequestMethod.POST}, consumes = {CONTENT_TYPE_FORMED})
@ApiOperation(value = "【COS】腾讯云 COS 文件上传成功回调", notes = "客户端向腾讯云 COS 存储桶上传文件完毕,有云函数触发此请求")
@RequestMapping(value = "/upload/cos-check-file-state", method = {RequestMethod.POST}, consumes = {CONTENT_TYPE_FORMED})
@ResponseBody
public CommonReturnType cosFinishUpload(@RequestParam(value = "fileName") String fileName,
@RequestParam(value = "urlGUID") String urlGUID) throws BusinessException {
// // 从数据库中找到该任务
// ScheduleTaskModel task = scheduleTaskService.findTaskByGuid(urlGUID);
// if (task == null) {
// throw new BusinessException(BusinessErrorCode.PARAMETER_VALIDATION_ERROR, "urlGUID 参数校验失败");
// }
//
// // 完成这个任务
// Boolean isSuccess = scheduleTaskService.doneScheduleTask(task.getId());
public CommonReturnType cosCheckFileStatus(
// @RequestParam() Map<String, String> params,
@RequestParam(value = "event") String eventStr,
@RequestParam(value = "context", required = false) String contextStr
) throws BusinessException {
// TODO
Boolean isSuccess = true;
return CommonReturnType.create(isSuccess);
JSONObject eventObject;
try {
eventObject = JSON.parseObject(eventStr);
} catch (Exception e) {
throw new BusinessException(BusinessErrorCode.PARAMETER_VALIDATION_ERROR, "event参数不合法");
}
try {
// 获取 Records 节点
JSONArray records = eventObject.getJSONArray("Records");
JSONObject record = records.getJSONObject(0);
JSONObject cos = record.getJSONObject("cos");
JSONObject cosBucket = cos.getJSONObject("cosBucket");
String appid = cosBucket.getString("appid");
String cosBucketName = cosBucket.getString("name");
if (Objects.equals(appid, "1253970026") && Objects.equals(cosBucketName, "testpic")) {
// 执行的是腾讯云云函数的测试请求
return CommonReturnType.create("您的云函数配置成功。");
}
String cosRegion = cosBucket.getString("cosRegion");
JSONObject cosObject = cos.getJSONObject("cosObject");
String key = cosObject.getString("key");
// 获取 /1000000000/bookshelfplus/fileName 中的 fileName 部分
key = key.substring(key.indexOf("/", key.indexOf("/", key.indexOf("/") + 1) + 1) + 1);
JSONObject event = record.getJSONObject("event");
String eventName = event.getString("eventName");
// 判断是否是由系统的存储桶触发
if (qCloudCosConfig.getBucketName().equals(cosBucketName + "-" + appid) &&
qCloudCosConfig.getRegionName().equals(cosRegion)) {
// 是由系统的存储桶触发的,则认为是文件上传成功
// 通过文件 key 获取文件对象
FileObjectModel fileObject = fileObjectService.getFileObjectByFilePath(QCloudCosUtils.BOOK_SAVE_FOLDER + key);
// 更新文件状态
Boolean isSuccess = fileObjectService.updateFileStatus(fileObject.getId(), "SUCCESS");
if (!isSuccess) {
throw new BusinessException(BusinessErrorCode.UNKNOWN_ERROR, "更新文件状态失败");
}
} else {
// 不是由系统的存储桶触发的
return CommonReturnType.create("Not triggered by the bucket specified in the configuration file, skip.");
}
} catch (Exception e) {
throw new BusinessException(BusinessErrorCode.PARAMETER_VALIDATION_ERROR, "JSON解析出错");
}
return CommonReturnType.create("success");
}
}
/*
示例返回
event
{
"Records": [
{
"cos": {
"cosBucket": {
"appid": "1302260381",
"cosRegion": "ap-shanghai",
"name": "bookshelfplus",
"region": "sh",
"s3Region": "ap-shanghai"
},
"cosNotificationId": "unkown",
"cosObject": {
"key": "/1302260381/bookshelfplus/!!!!!!!",
"meta": {
"Content-Type": "text/plain",
"ETag": "\"0d7316832fef232e5dcdcf81f39bfdba\"",
"x-cos-request-id": "NjI1OTNmOWNfNGIzN2YyMDlfMmJhZjVfNDJkZTBmYQ==",
"x-cos-storage-class": "Standard"
},
"size": 557,
"url": "http://bookshelfplus-1302260381.cos.ap-shanghai.myqcloud.com/%21%21%21%21%21%21%21",
"vid": ""
},
"cosSchemaVersion": "1.0"
},
"event": {
"eventName": "cos:ObjectCreated:Put",
"eventQueue": "qcs:0:scf:ap-shanghai:appid/1302260381:default.bookshelf-scf.$DEFAULT",
"eventSource": "qcs::cos",
"eventTime": 1650016156,
"eventVersion": "1.0",
"reqid": 0,
"requestParameters": {
"requestHeaders": {
"Authorization": "q-sign-algorithm=sha1&q-ak=AKIDgEWYJo2yd7KGvIPFn45pJWT9YgX8RTEi&q-sign-time=1650016155;1650017955&q-key-time=1650016155;1650017955&q-header-list=host&q-url-param-list=by;guid;userid&q-signature=0ceb85180d6b0b665662d5d139d4276cdc0fbbbd"
},
"requestSourceIP": "117.154.65.144"
},
"reservedInfo": ""
}
}
]
}
context
{
"callbackWaitsForEmptyEventLoop": true,
"memory_limit_in_mb": 512,
"time_limit_in_ms": 3000,
"request_id": "279ba5cc-e435-4af9-8ede-baa71373d75b",
"environment": "{\"SCF_NAMESPACE\":\"default\"}",
"environ": "SCF_NAMESPACE=default;SCF_NAMESPACE=default",
"function_version": "$LATEST",
"function_name": "bookshelf-scf",
"namespace": "default",
"tencentcloud_region": "ap-shanghai",
"tencentcloud_appid": "1302260381",
"tencentcloud_uin": "100014397291"
}
*/

View File

@@ -4,9 +4,10 @@ import com.sun.management.OperatingSystemMXBean;
import com.sun.management.ThreadMXBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import plus.bookshelf.Common.Response.CommonReturnType;
import java.lang.management.ManagementFactory;

View File

@@ -6,7 +6,10 @@ import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import plus.bookshelf.Common.Error.BusinessErrorCode;
import plus.bookshelf.Common.Error.BusinessException;
import plus.bookshelf.Common.Response.CommonReturnType;

View File

@@ -11,6 +11,15 @@ public class FileObjectVO {
// 存储的文件Id
private Integer fileId;
// 文件名
private String fileName;
// 文件大小
private Long fileSize;
// 文件类型
private String fileType;
// 文件存储介质类型
String storageMediumType;
@@ -24,6 +33,12 @@ public class FileObjectVO {
// 文件提取码
String fileShareCode;
// 文件上传状态
String uploadStatus;
// 文件哈希 - SHA1
String hashSha1;
// 附加字段(JSON存储)
String additionalFields;
}

View File

@@ -2,8 +2,6 @@ package plus.bookshelf.Controller.VO;
import lombok.Data;
import java.util.Date;
@Data
public class FileVO {

View File

@@ -19,6 +19,33 @@ public class FileObjectDO {
*/
private Integer fileId;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column file_object_info.file_name
*
* @mbg.generated
*/
private String fileName;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column file_object_info.file_size
*
* @mbg.generated
*/
private Long fileSize;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column file_object_info.file_type
*
* @mbg.generated
*/
private String fileType;
/**
*
* This field was generated by MyBatis Generator.
@@ -55,6 +82,24 @@ public class FileObjectDO {
*/
private String fileShareCode;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column file_object_info.upload_status
*
* @mbg.generated
*/
private String uploadStatus;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column file_object_info.file_sha1
*
* @mbg.generated
*/
private String fileSha1;
/**
*
* This field was generated by MyBatis Generator.
@@ -112,6 +157,78 @@ public class FileObjectDO {
this.fileId = fileId;
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column file_object_info.file_name
*
* @return the value of file_object_info.file_name
*
* @mbg.generated
*/
public String getFileName() {
return fileName;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column file_object_info.file_name
*
* @param fileName the value for file_object_info.file_name
*
* @mbg.generated
*/
public void setFileName(String fileName) {
this.fileName = fileName == null ? null : fileName.trim();
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column file_object_info.file_size
*
* @return the value of file_object_info.file_size
*
* @mbg.generated
*/
public Long getFileSize() {
return fileSize;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column file_object_info.file_size
*
* @param fileSize the value for file_object_info.file_size
*
* @mbg.generated
*/
public void setFileSize(Long fileSize) {
this.fileSize = fileSize;
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column file_object_info.file_type
*
* @return the value of file_object_info.file_type
*
* @mbg.generated
*/
public String getFileType() {
return fileType;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column file_object_info.file_type
*
* @param fileType the value for file_object_info.file_type
*
* @mbg.generated
*/
public void setFileType(String fileType) {
this.fileType = fileType == null ? null : fileType.trim();
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column file_object_info.storage_medium_type
@@ -208,6 +325,54 @@ public class FileObjectDO {
this.fileShareCode = fileShareCode == null ? null : fileShareCode.trim();
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column file_object_info.upload_status
*
* @return the value of file_object_info.upload_status
*
* @mbg.generated
*/
public String getUploadStatus() {
return uploadStatus;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column file_object_info.upload_status
*
* @param uploadStatus the value for file_object_info.upload_status
*
* @mbg.generated
*/
public void setUploadStatus(String uploadStatus) {
this.uploadStatus = uploadStatus == null ? null : uploadStatus.trim();
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column file_object_info.file_sha1
*
* @return the value of file_object_info.file_sha1
*
* @mbg.generated
*/
public String getFileSha1() {
return fileSha1;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column file_object_info.file_sha1
*
* @param fileSha1 the value for file_object_info.file_sha1
*
* @mbg.generated
*/
public void setFileSha1(String fileSha1) {
this.fileSha1 = fileSha1 == null ? null : fileSha1.trim();
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column file_object_info.additional_fields

View File

@@ -1,7 +1,6 @@
package plus.bookshelf.Dao.Mapper;
import org.springframework.stereotype.Repository;
import plus.bookshelf.Dao.DO.FileDO;
import plus.bookshelf.Dao.DO.FileObjectDO;
@Repository // 添加这个注解Autowired的时候idea就不会报错了
@@ -60,4 +59,12 @@ public interface FileObjectDOMapper {
* @return
*/
FileObjectDO[] selectAll();
/**
* 通过文件路径获取文件
*
* @param filePath 文件路径
* @return
*/
FileObjectDO selectByFilePath(String filePath);
}

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 {

View File

@@ -2,9 +2,6 @@ package plus.bookshelf.Service.Model;
import lombok.Data;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
@Data
public class BookModel {

View File

@@ -11,6 +11,15 @@ public class FileObjectModel {
// 存储的文件Id
private Integer fileId;
// 文件名
private String fileName;
// 文件大小
private Long fileSize;
// 文件类型
private String fileType;
// 文件存储介质类型
String storageMediumType;
@@ -24,6 +33,12 @@ public class FileObjectModel {
// 文件提取码
String fileShareCode;
// 文件上传状态
String uploadStatus;
// 文件哈希 - SHA1
String hashSha1;
// 附加字段(JSON存储)
String additionalFields;
}

View File

@@ -1,5 +1,7 @@
package plus.bookshelf.Service.Service;
import org.springframework.transaction.annotation.Transactional;
import plus.bookshelf.Common.Enum.FileStorageMediumEnum;
import plus.bookshelf.Common.Error.BusinessException;
import plus.bookshelf.Service.Model.FileObjectModel;
@@ -7,10 +9,57 @@ import java.lang.reflect.InvocationTargetException;
import java.util.List;
public interface FileObjectService {
/**
* 列出所有文件对象
*
* @return
*/
List<FileObjectModel> list(String token) throws InvocationTargetException, IllegalAccessException, BusinessException;
/**
* 添加文件对象
* 返回是否添加成功
*
* @param fileObjectModel
* @return
* @throws InvocationTargetException
* @throws IllegalAccessException
*/
Boolean addFileObject(FileObjectModel fileObjectModel) throws InvocationTargetException, IllegalAccessException;
/**
* 向数据库中插入文件信息
*
* @param fileName 文件名
* @param filePath 文件路径
* @param fileSize 文件大小
* @param fileSHA1 文件SHA1
* @param fileExt 文件扩展名
* @param fileNameWithoutExt 文件名(不包含扩展名)
* @param fileStorageMediumEnum 文件存储介质
* @param bookOrigin 文件来源
* @return 是否插入成功
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws BusinessException
*/
@Transactional
Boolean uploadFile(Integer fileId, String fileName, String filePath, Long fileSize, String fileSHA1, String fileExt, String fileNameWithoutExt, FileStorageMediumEnum fileStorageMediumEnum, String bookOrigin) throws InvocationTargetException, IllegalAccessException, BusinessException;
/**
* 修改文件对象上传状态信息
*
* @param fileId
* @param fileStatus
*/
Boolean updateFileStatus(Integer fileId, String fileStatus) throws InvocationTargetException, IllegalAccessException;
/**
* 通过文件路径获取文件
*
* @param filePath 文件路径
* @return
*/
FileObjectModel getFileObjectByFilePath(String filePath) throws InvocationTargetException, IllegalAccessException;
}

View File

@@ -1,5 +1,6 @@
package plus.bookshelf.Service.Service;
import plus.bookshelf.Common.Error.BusinessException;
import plus.bookshelf.Service.Model.FileModel;
import java.lang.reflect.InvocationTargetException;
@@ -11,14 +12,23 @@ public interface FileService {
*
* @return
*/
List<FileModel> list(String token) throws InvocationTargetException, IllegalAccessException, BusinessException;
// /**
// * 向数据库中添加一个 scheduleTask
// *
// * @param expireMinute
// * @param fileName
// * @param urlGUID
// * @param userId
// */
// void addScheduleTask(Integer expireMinute, String fileName, String urlGUID, Integer userId);
/**
* 添加文件信息
* 返回是否添加成功
*
* @param fileModel
* @return
* @throws InvocationTargetException
* @throws IllegalAccessException
*/
Boolean addFile(FileModel fileModel) throws InvocationTargetException, IllegalAccessException;
/**
* 获取上一步添加的文件Id
*
* @return
*/
Integer getLastInsertId();
}