| | |
| | | import com.ruoyi.common.utils.uuid.IdUtils; |
| | | import lombok.RequiredArgsConstructor; |
| | | import org.apache.commons.io.FilenameUtils; |
| | | import org.apache.poi.util.TempFile; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import java.io.IOException; |
| | | import java.nio.file.Files; |
| | | import java.nio.file.Path; |
| | | import java.nio.file.Paths; |
| | | import java.time.LocalDateTime; |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | | import java.util.UUID; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | |
| | | @Autowired |
| | | private MinioUtils minioUtils; |
| | | |
| | | @Value("${file.file-url}") |
| | | private String fileUrl; |
| | | |
| | | @Override |
| | | public List<StorageBlobDTO> uploads(List<MultipartFile> files, String bucketName, Long type) { |
| | | // 若没传入bucketName,则使用默认bucketName |
| | | if (StringUtils.isEmpty(bucketName)) { |
| | | bucketName = minioUtils.getDefaultBucket(); |
| | | } |
| | | List<StorageBlobDTO> storageBlobDTOs = new ArrayList<>(); |
| | | for (MultipartFile file : files) { |
| | | try { |
| | | validateFileExtension(file); |
| | | // MinioResult res = minioUtils.upload(bucketName, file, false); |
| | | // 不使用minio,使用本地文件上传 |
| | | String res = uploadFile(file, type); |
| | | StorageBlobDTO dto = buildStorageBlobDTO(file, res, bucketName, type); |
| | | dto.setFileUrl(res); |
| | | storageBlobMapper.insert(dto); |
| | | storageBlobDTOs.add(dto); |
| | | |
| | | } catch (InvalidExtensionException e) { |
| | | throw new RuntimeException("不支持的文件类型:" + file.getOriginalFilename(), e); |
| | | } catch (Exception e) { |
| | | throw new RuntimeException("上传文件失败:" + file.getOriginalFilename(), e); |
| | | } |
| | | } |
| | | return storageBlobDTOs; |
| | | } |
| | | |
| | | @Override |
| | | public List<StorageBlobDTO> updateStorageBlobs(List<MultipartFile> files, String bucketName,Long type) { |
| | | |
| | |
| | | for (MultipartFile file : files) { |
| | | try { |
| | | validateFileExtension(file); |
| | | |
| | | MinioResult res = minioUtils.upload(bucketName, file, false); |
| | | |
| | | // String res = uploadFile(file, type); |
| | | StorageBlobDTO dto = buildStorageBlobDTO(file, res, bucketName, type); |
| | | |
| | | storageBlobMapper.insert(dto); |
| | |
| | | } |
| | | } |
| | | return storageBlobDTOs; |
| | | } |
| | | |
| | | @Value("${file.upload-dir}") |
| | | private String tempDir; |
| | | |
| | | public String uploadFile(MultipartFile file, Long type) throws IOException { |
| | | // 1. 生成临时文件ID和路径 |
| | | String tempId = UUID.randomUUID().toString(); |
| | | String originalFilename = file.getOriginalFilename(); |
| | | if(originalFilename == null) throw new IOException("文件名不能为空"); |
| | | |
| | | Path tempFilePath = Paths.get(tempDir, tempId + "_" + file.getOriginalFilename()); //windows上传路径 |
| | | Path prodFilePath = Paths.get(fileUrl, tempId + "_" + file.getOriginalFilename()); //nginx代理路径 |
| | | |
| | | // 2. 确保目录存在 |
| | | Path parentDir = tempFilePath.getParent(); |
| | | if (parentDir != null) { |
| | | Files.createDirectories(parentDir); // 递归创建目录 |
| | | } |
| | | |
| | | // 3. 保存文件到临时目录 |
| | | file.transferTo(tempFilePath.toFile()); |
| | | |
| | | return prodFilePath.toString(); |
| | | } |
| | | |
| | | private void validateFileExtension(MultipartFile file) throws InvalidExtensionException { |
| | |
| | | return dto; |
| | | } |
| | | |
| | | private StorageBlobDTO buildStorageBlobDTO(MultipartFile file, String res, String bucketName, Long type) { |
| | | StorageBlobDTO dto = new StorageBlobDTO(); |
| | | dto.setContentType(file.getContentType()); |
| | | dto.setBucketFilename("sys"); |
| | | dto.setOriginalFilename(file.getOriginalFilename()); |
| | | dto.setByteSize(file.getSize()); |
| | | dto.setKey(IdUtils.simpleUUID()); |
| | | dto.setBucketName(bucketName); |
| | | dto.setUrl(res); |
| | | dto.setDownloadUrl(res); |
| | | |
| | | if (type != null) { |
| | | dto.setType(type); |
| | | } |
| | | |
| | | return dto; |
| | | } |
| | | |
| | | @Override |
| | | public int deleteStorageBlobs(StorageAttachment attachment) { |
| | | List<StorageAttachment> attachments = storageAttachmentMapper.selectList(new LambdaQueryWrapper<StorageAttachment>() |
| | |
| | | } |
| | | return 0; |
| | | } |
| | | |
| | | |
| | | } |