package com.example.fms.service; import com.example.fms.common.model.dto.DownloadSysFileDTO; import com.example.fms.common.model.dto.SystemFileDTO; import com.example.fms.common.model.vo.ConfigSettingVO; import com.example.fms.common.utils.ExcuteConfigUtils; import com.example.fms.common.utils.FileUtils; import com.example.fms.exception.XiaoShiException; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import java.util.*; /** * 文件工厂类 * * @Author xiexiang * @Date 2023/6/2 */ @Slf4j @Service @RequiredArgsConstructor public class FileFactoryService { private final FileUtils fileUtils; /** * 上传文件 * * @param files * @param sourceId * @return */ public List uploadFiles(List files, Integer sourceId) { if (files != null && files.size() != 0) { //调用解析配置方法,获取配置信息 List configSettingVOS = ExcuteConfigUtils.excuteConfigVO(); ConfigSettingVO configSettingVO = configSettingVOS.stream().filter(item -> item.getSourceId().equals(sourceId)).findFirst().orElse(null); if (configSettingVO == null) { return null; } String sourceName = configSettingVO.getSourceName(); if (sourceName.equals("FSS")) { return this.uploadToFSS(files, configSettingVO); } return null; } throw new XiaoShiException("传入文件为空"); } /** * 上传到服务器 * * @param files * @param configSettingVO * @return */ public List uploadToFSS(List files, ConfigSettingVO configSettingVO) { List systemFileDTOS = new ArrayList<>(); for (MultipartFile file : files) { try { String directoryName = fileUtils.getDirectoryName(); SystemFileDTO systemFileDTO = SftpService.upload(configSettingVO.getFilePath() + directoryName, file, configSettingVO); systemFileDTO.setPType(configSettingVO.getSourceId()); systemFileDTO.setOriginalName(file.getOriginalFilename()); systemFileDTO.setFilePath(configSettingVO.getFilePath() + directoryName + "/" + systemFileDTO.getFileName()); systemFileDTOS.add(systemFileDTO); } catch (Exception e) { e.printStackTrace(); } } return systemFileDTOS; } /** * 下载文件 * * @param downloadSysFileDTO * @throws Exception */ public byte[] download(DownloadSysFileDTO downloadSysFileDTO) throws Exception { Integer sourceId = downloadSysFileDTO.getSourceId(); //调用解析配置方法,获取配置信息 List configSettingVOS = ExcuteConfigUtils.excuteConfigVO(); //根据传入sourceId判断从哪里下载数据 ConfigSettingVO configSettingVO = configSettingVOS.stream().filter(item -> item.getSourceId().equals(sourceId)).findFirst().orElse(null); if (configSettingVO == null) { throw new XiaoShiException("解析错误"); } String sourceName = configSettingVO.getSourceName(); if (sourceName.equals("FSS")) { byte[] file = this.downloadFromFSS(downloadSysFileDTO, configSettingVO); return file; } else { return null; } } /** * @param //directory 下载目录 根据SFTP设置的根目录来进行传输 * @param //downloadFile 下载的文件 * @param //saveFile 存在本地的路径 */ public byte[] downloadFromFSS(DownloadSysFileDTO downloadSysFileDTO, ConfigSettingVO configSettingVO) throws Exception { //下载目录,也是存储路径 String filePath = downloadSysFileDTO.getFilePath(); String directory = filePath.substring(0, filePath.lastIndexOf("\\")); //下载的文件,也就是文件名 String downloadFile = downloadSysFileDTO.getFileName(); //存在本地的路径,随机生成 // String saveFile = "C:\\Users\\Admin\\Desktop\\downloadFile"; // byte[] fileData = SftpService.download(directory, downloadFile, saveFile, configSettingVO); byte[] fileData = SftpService.download(directory, downloadFile, configSettingVO); return fileData; } /** * 删除文件 * * @param filePath * @param pType */ public static void delete(String filePath, Integer pType) { //调用解析配置方法,获取配置信息 List configSettingVOS = ExcuteConfigUtils.excuteConfigVO(); //根据传入类型判断从哪里删除数据 //如果为1,则为服务器 if (pType.equals(1)) { int sourceId = 1; ConfigSettingVO configSettingVO = configSettingVOS.stream().filter(item -> item.getSourceId().equals(sourceId)).findFirst().orElse(null); //拆分路径字符串 int index = filePath.lastIndexOf('/'); //要删除文件所在目录 String directory = filePath.substring(0, index); //要删除的文件 String deleteFile = filePath.substring(index + 1, filePath.length()); try { SftpService.delete(directory, deleteFile, configSettingVO); } catch (Exception e) { throw new XiaoShiException("删除错误"); } } else if (pType.equals(2)) {//类型为2,则为阿里云OSS int sourceId = 2; ConfigSettingVO configSettingVO = configSettingVOS.stream().filter(item -> item.getSourceId().equals(sourceId)).findFirst().orElse(null); } } }