|
@@ -8,7 +8,6 @@ import com.example.fms.common.utils.ExcuteConfigUtils;
|
|
import com.example.fms.domain.SystemFile;
|
|
import com.example.fms.domain.SystemFile;
|
|
import com.example.fms.exception.XiaoShiException;
|
|
import com.example.fms.exception.XiaoShiException;
|
|
import com.example.fms.mapper.SystemFileMapper;
|
|
import com.example.fms.mapper.SystemFileMapper;
|
|
-import io.swagger.v3.oas.models.security.SecurityScheme;
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.beans.BeanUtils;
|
|
import org.springframework.beans.BeanUtils;
|
|
@@ -20,6 +19,7 @@ import java.io.IOException;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
|
|
/**
|
|
/**
|
|
|
|
+ * 文件管理外部调用接口Service层
|
|
* @Author xiexiang
|
|
* @Author xiexiang
|
|
* @Date 2023/8/9
|
|
* @Date 2023/8/9
|
|
*/
|
|
*/
|
|
@@ -27,7 +27,7 @@ import java.util.List;
|
|
@Slf4j
|
|
@Slf4j
|
|
@Service
|
|
@Service
|
|
public class FileMangerService {
|
|
public class FileMangerService {
|
|
- private final FileFactoryService fileFactoryService;
|
|
|
|
|
|
+ private final OldFileFactoryService oldFileFactoryService;
|
|
private final SystemFileService systemFileService;
|
|
private final SystemFileService systemFileService;
|
|
private final SystemFileMapper systemFileMapper;
|
|
private final SystemFileMapper systemFileMapper;
|
|
private final FileFactory fileFactory;
|
|
private final FileFactory fileFactory;
|
|
@@ -35,19 +35,21 @@ public class FileMangerService {
|
|
|
|
|
|
/**
|
|
/**
|
|
* 上传文件的最外部接口
|
|
* 上传文件的最外部接口
|
|
- * @param files
|
|
|
|
- * @param sourceId
|
|
|
|
- * @return
|
|
|
|
|
|
+ * @param files 需要上传的文件
|
|
|
|
+ * @param sourceId configSettingVO的主键id,判断存储在哪个服务器路径之下
|
|
|
|
+ * @return insertIds 文件在数据库中的id集合
|
|
* @throws IOException
|
|
* @throws IOException
|
|
*/
|
|
*/
|
|
public List<Integer> uploadFile(List<MultipartFile> files, Integer sourceId) throws IOException {
|
|
public List<Integer> uploadFile(List<MultipartFile> files, Integer sourceId) throws IOException {
|
|
if (files != null && files.size() != 0) {
|
|
if (files != null && files.size() != 0) {
|
|
//1.调用解析配置方法,获取配置信息
|
|
//1.调用解析配置方法,获取配置信息
|
|
List<ConfigSettingVO> configSettingVOS = ExcuteConfigUtils.excuteConfigVO();
|
|
List<ConfigSettingVO> configSettingVOS = ExcuteConfigUtils.excuteConfigVO();
|
|
- //2.根据配置id获得配置
|
|
|
|
- ConfigSettingVO configSettingVO = configSettingVOS.stream().filter(item -> item.getId().equals(sourceId)).findFirst().orElse(null);
|
|
|
|
|
|
+ //2.根据传入id,获得配置类,根据配置类选择使用的上传方法
|
|
|
|
+ ConfigSettingVO configSettingVO = configSettingVOS.stream().filter(item -> item.getId().equals(sourceId)).findFirst().orElse(null);
|
|
//3.获取文件上传到服务器,调用工厂类
|
|
//3.获取文件上传到服务器,调用工厂类
|
|
- IFileFactory fileFactoryObject = fileFactory.createObject(sourceId);
|
|
|
|
|
|
+ String sourceName = configSettingVO.getSourceName();
|
|
|
|
+ //TODO 调用工厂方法,工厂方法会根据sourceName创建并返回对应的方法的对象
|
|
|
|
+ IFileFactory fileFactoryObject = fileFactory.createObject(sourceName);
|
|
List<SystemFileDTO> systemFileDTOS = fileFactoryObject.uploadFile(files, configSettingVO);
|
|
List<SystemFileDTO> systemFileDTOS = fileFactoryObject.uploadFile(files, configSettingVO);
|
|
//4.根据上传后的返回实体类信息,将实体类信息入库,调用systemFileService add
|
|
//4.根据上传后的返回实体类信息,将实体类信息入库,调用systemFileService add
|
|
if (systemFileDTOS != null && systemFileDTOS.size() != 0) {
|
|
if (systemFileDTOS != null && systemFileDTOS.size() != 0) {
|
|
@@ -61,40 +63,42 @@ public class FileMangerService {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+
|
|
/**
|
|
/**
|
|
- * 上传文件
|
|
|
|
- *
|
|
|
|
- * @param files
|
|
|
|
- * @param sourceId
|
|
|
|
|
|
+ * 下载文件
|
|
|
|
+ * @param fileId 文件id
|
|
|
|
+ * @return fileData 文件流
|
|
|
|
+ * @throws Exception
|
|
*/
|
|
*/
|
|
- public List<Integer> add(List<MultipartFile> files, Integer sourceId) {
|
|
|
|
- if (files != null && files.size() != 0) {
|
|
|
|
- //1.获取文件上传到服务器,调用FileFactory的uploadFile
|
|
|
|
-
|
|
|
|
- List<SystemFileDTO> systemFileDTOS = fileFactoryService.uploadFiles(files, sourceId);
|
|
|
|
- //2.根据上传后的返回实体类信息,将实体类信息入库,调用systemFileService add
|
|
|
|
- if (systemFileDTOS != null && systemFileDTOS.size() != 0) {
|
|
|
|
- List<Integer> insertIds = systemFileService.add(systemFileDTOS, sourceId);
|
|
|
|
- return insertIds;
|
|
|
|
- } else {
|
|
|
|
- throw new XiaoShiException("入表信息为空");
|
|
|
|
- }
|
|
|
|
- } else {
|
|
|
|
- throw new XiaoShiException("上传文件为空");
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- //取系统文件
|
|
|
|
- public byte[] download(Integer fileId) throws Exception {
|
|
|
|
- DownloadSysFileDTO downloadSysFileDTO = new DownloadSysFileDTO();
|
|
|
|
|
|
+ public byte[] downloadFile(Integer fileId) {
|
|
if (fileId != null) {
|
|
if (fileId != null) {
|
|
|
|
+ //1.新建一个对象
|
|
|
|
+ DownloadSysFileDTO downloadSysFileDTO = new DownloadSysFileDTO();
|
|
|
|
+ //2.根据传入文件id查询到文件信息
|
|
SystemFileVO systemFileVO = systemFileService.query(fileId);
|
|
SystemFileVO systemFileVO = systemFileService.query(fileId);
|
|
|
|
+ //3.文件信息标记为已经删除的,返回null
|
|
if (systemFileVO.getIsDelete().equals(1)) {
|
|
if (systemFileVO.getIsDelete().equals(1)) {
|
|
return null;
|
|
return null;
|
|
} else {
|
|
} else {
|
|
|
|
+ //3.对象赋值给新的
|
|
BeanUtils.copyProperties(systemFileVO, downloadSysFileDTO);
|
|
BeanUtils.copyProperties(systemFileVO, downloadSysFileDTO);
|
|
- downloadSysFileDTO.setSourceId(systemFileVO.getPType());
|
|
|
|
- return fileFactoryService.download(downloadSysFileDTO);
|
|
|
|
|
|
+ //4.设置pType(存储在哪条路径下)
|
|
|
|
+ Integer id = downloadSysFileDTO.getPType();
|
|
|
|
+ //5.调用解析配置方法,获取配置信息
|
|
|
|
+ List<ConfigSettingVO> configSettingVOS = ExcuteConfigUtils.excuteConfigVO();
|
|
|
|
+ if(configSettingVOS == null && configSettingVOS.size() == 0){
|
|
|
|
+ throw new XiaoShiException("缺失配置文件");
|
|
|
|
+ }
|
|
|
|
+ //6.根据id获取需要的配置类
|
|
|
|
+ ConfigSettingVO configSettingVO = configSettingVOS.stream().filter(item -> item.getId().equals(id)).findFirst().orElse(null);
|
|
|
|
+ //7.设置sourceId为数据库中的sourceId字段
|
|
|
|
+ downloadSysFileDTO.setSourceId(systemFileVO.getSourceId());
|
|
|
|
+ //8.调用工厂类下载方法
|
|
|
|
+ String sourceName = configSettingVO.getSourceName();
|
|
|
|
+ //TODO 调用工厂方法,工厂方法会根据sourceName创建并返回对应的方法的对象
|
|
|
|
+ IFileFactory fileFactoryObject = fileFactory.createObject(sourceName);
|
|
|
|
+ byte[] fileData = fileFactoryObject.downloadFile(downloadSysFileDTO, configSettingVO);
|
|
|
|
+ return fileData;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
return null;
|
|
@@ -102,54 +106,67 @@ public class FileMangerService {
|
|
|
|
|
|
/**
|
|
/**
|
|
* 删除系统文件
|
|
* 删除系统文件
|
|
- *
|
|
|
|
- * @param ids
|
|
|
|
- * @param type
|
|
|
|
|
|
+ * 分为物理删除和逻辑删除,逻辑删除只更新数据库中isDelete字段,物理删除需要调用工厂类方法
|
|
|
|
+ * @param ids 需要删除的id集合
|
|
|
|
+ * @param type 删除方式判断字段,1为逻辑删除,2为物理删除
|
|
*/
|
|
*/
|
|
- public void delete(List<Integer> ids, Integer type) {
|
|
|
|
- //首先判断传入的需要删除的id集合不能为空
|
|
|
|
|
|
+ public String deleteFile(List<Integer> ids, Integer type) {
|
|
|
|
+ //1.首先判断传入的需要删除的id集合不能为空
|
|
if (CollectionUtils.isEmpty(ids)) {
|
|
if (CollectionUtils.isEmpty(ids)) {
|
|
throw new XiaoShiException("需要删除的id集合不能为空");
|
|
throw new XiaoShiException("需要删除的id集合不能为空");
|
|
}
|
|
}
|
|
|
|
+ //2.其次判断传入的删除类型不能为空
|
|
if (type == null) {
|
|
if (type == null) {
|
|
throw new XiaoShiException("删除类型不能为空");
|
|
throw new XiaoShiException("删除类型不能为空");
|
|
}
|
|
}
|
|
- //有逻辑删除和物理删除两种,需要进行判定,传入类型为2时,物理删除;传入类型为1时,逻辑删除
|
|
|
|
- //逻辑删除(更新isDelete字段为1)
|
|
|
|
|
|
+ //3.1 判断如果是逻辑删除(更新isDelete字段为1)
|
|
if (type.equals(1)) {
|
|
if (type.equals(1)) {
|
|
- //遍历传入的id集合,获取需要更新的对象,赋值给实体类
|
|
|
|
|
|
+ //3.1.1 遍历传入的id集合
|
|
for (int i = 0; i < ids.size(); i++) {
|
|
for (int i = 0; i < ids.size(); i++) {
|
|
|
|
+ //3.1.2 获取需要更新的对象
|
|
SystemFileVO systemFileVO = systemFileMapper.query(ids.get(i));
|
|
SystemFileVO systemFileVO = systemFileMapper.query(ids.get(i));
|
|
|
|
+ //3.1.3 将对象的是否删除字段置为1
|
|
systemFileVO.setIsDelete(1);
|
|
systemFileVO.setIsDelete(1);
|
|
- //将查询出来的vo赋值给实体类
|
|
|
|
|
|
+ //3.1.4 将查询出来的vo赋值给实体类
|
|
SystemFile systemFile = new SystemFile();
|
|
SystemFile systemFile = new SystemFile();
|
|
BeanUtils.copyProperties(systemFileVO, systemFile);
|
|
BeanUtils.copyProperties(systemFileVO, systemFile);
|
|
- //更新表
|
|
|
|
|
|
+ //3.1.5 数据入表
|
|
int row = systemFileMapper.update(systemFile);
|
|
int row = systemFileMapper.update(systemFile);
|
|
|
|
+ //3.1.6 判断是否成功
|
|
if (row != 1) {
|
|
if (row != 1) {
|
|
String mes = "逻辑删除系统文件第" + i + "条失败";
|
|
String mes = "逻辑删除系统文件第" + i + "条失败";
|
|
- log.info("逻辑删除失败,{}", mes);
|
|
|
|
throw new XiaoShiException(mes);
|
|
throw new XiaoShiException(mes);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- }
|
|
|
|
- //物理删除(删除服务器文件,删除数据库中记录)
|
|
|
|
- //sftp删除文件
|
|
|
|
- if (type.equals(2)) {
|
|
|
|
|
|
+ String mes = "逻辑删除成功";
|
|
|
|
+ return mes;
|
|
|
|
+ } else if (type.equals(2)) { //3.2 物理删除(先删除服务器文件,删除数据库中记录)
|
|
|
|
+ //3.2.1 遍历传入的id集合
|
|
for (int i = 0; i < ids.size(); i++) {
|
|
for (int i = 0; i < ids.size(); i++) {
|
|
- //根据id到表中查询该文件记录的数据
|
|
|
|
|
|
+ //3.2.2 根据id到表中查询该文件记录的数据
|
|
SystemFileVO systemFileVO = systemFileService.query(ids.get(i));
|
|
SystemFileVO systemFileVO = systemFileService.query(ids.get(i));
|
|
- //pType判断是服务器还是阿里云OSS
|
|
|
|
|
|
+ //3.2.3 根据pType判断存储在服务器的哪个地址下
|
|
int pType = systemFileVO.getPType();
|
|
int pType = systemFileVO.getPType();
|
|
|
|
+ //3.2.4 文件路径
|
|
String filePath = systemFileVO.getFilePath();
|
|
String filePath = systemFileVO.getFilePath();
|
|
- fileFactoryService.delete(filePath, pType);
|
|
|
|
|
|
+ //3.2.5 调用解析配置方法,获取指定配置信息
|
|
|
|
+ List<ConfigSettingVO> configSettingVOS = ExcuteConfigUtils.excuteConfigVO();
|
|
|
|
+ ConfigSettingVO configSettingVO = configSettingVOS.stream().filter(item -> item.getId().equals(pType)).findFirst().orElse(null);
|
|
|
|
+ //3.2.6 配置类的删除方法
|
|
|
|
+ String sourceName = configSettingVO.getSourceName();
|
|
|
|
+ //TODO 调用工厂方法,工厂方法会根据sourceName创建并返回对应的方法的对象
|
|
|
|
+ IFileFactory fileFactoryObject = fileFactory.createObject(sourceName);
|
|
|
|
+ fileFactoryObject.deleteFile(filePath, configSettingVO);
|
|
int row = systemFileMapper.deleteById(ids.get(i));
|
|
int row = systemFileMapper.deleteById(ids.get(i));
|
|
if (row != 1) {
|
|
if (row != 1) {
|
|
- throw new XiaoShiException("删除异常");
|
|
|
|
- } else {
|
|
|
|
- log.info("删除成功");
|
|
|
|
|
|
+ throw new XiaoShiException("第" + i + "条删除异常");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+ String mes = "物理删除成功";
|
|
|
|
+ return mes;
|
|
|
|
+ } else {
|
|
|
|
+ String mes = "删除类型错误";
|
|
|
|
+ return mes;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|