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.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.io.IOException; import java.util.ArrayList; import java.util.List; /** * OSS文件管理接口 * * @Author xiexiang * @Date 2024/4/1 */ @Slf4j @Service @RequiredArgsConstructor public class File2OssService implements IFileFactory { private final FileUtils fileUtils; private final OssService ossService; /** * 上传文件 * * @param files * @param configSettingVO 根据传入的id,选择的配置类,选择的上传路径以及上传方法 * @return * @throws IOException */ public List uploadFile(List files, ConfigSettingVO configSettingVO) throws IOException { List systemFileDTOS = new ArrayList<>(); for (MultipartFile file : files) { try { String directoryName = fileUtils.getDirectoryName(); SystemFileDTO systemFileDTO = OssService.upload(configSettingVO.getFilePath() + directoryName, file, configSettingVO); //服务器存储目录位置(1.本地project/pas/prod/file 2.本地project/rms/prod/file 3.生产project/pas/prod/file 4.生产project/rms/prod/file) systemFileDTO.setPType(configSettingVO.getId()); //FSS systemFileDTO.setSourceId(configSettingVO.getSourceId()); systemFileDTO.setOriginalName(file.getOriginalFilename()); systemFileDTO.setFilePath(configSettingVO.getFilePath() + directoryName + "/" + systemFileDTO.getFileName()); systemFileDTOS.add(systemFileDTO); } catch (Exception e) { e.printStackTrace(); } } return systemFileDTOS; } @Override public SystemFileDTO replaceFile(String fileGuid, MultipartFile file, ConfigSettingVO configSettingVO) { try { String directoryName = fileUtils.getDirectoryName(); SystemFileDTO systemFileDTO = ossService.upload(configSettingVO.getFilePath() + directoryName, file, configSettingVO); //服务器存储目录位置(1.本地project/pas/prod/file 2.本地project/rms/prod/file 3.生产project/pas/prod/file 4.生产project/rms/prod/file) systemFileDTO.setPType(configSettingVO.getId()); //FSS systemFileDTO.setSourceId(configSettingVO.getSourceId()); systemFileDTO.setOriginalName(file.getOriginalFilename()); systemFileDTO.setGUID(fileGuid); systemFileDTO.setFilePath(configSettingVO.getFilePath() + directoryName + "/" + systemFileDTO.getFileName()); return systemFileDTO; } catch (Exception e) { } return null; } /** * 下载文件 * * @param downloadSysFileDTO * @param configSettingVO * @return */ public byte[] downloadFile(DownloadSysFileDTO downloadSysFileDTO, ConfigSettingVO configSettingVO) { String filePath = downloadSysFileDTO.getFilePath(); try { byte[] fileData = OssService.download(filePath, configSettingVO); return fileData; } catch (Exception e) { throw new XiaoShiException("下载错误"); } } /** * 删除文件 * * @param filePath * @param configSettingVO */ public void deleteFile(String filePath, ConfigSettingVO configSettingVO) { try { OssService.delete(filePath, configSettingVO); } catch (Exception e) { throw new XiaoShiException("删除错误"); } } }