From ca77a052f3af47697cd3cf0a4f71468feb8455af Mon Sep 17 00:00:00 2001
From: maven <2163098428@qq.com>
Date: 星期三, 31 十二月 2025 13:42:48 +0800
Subject: [PATCH] yys 巡检bug
---
ruoyi-common/src/main/java/com/ruoyi/basic/service/impl/StorageBlobServiceImpl.java | 203 ++++++++++++++++++++++++++++++++++++++++++--------
1 files changed, 170 insertions(+), 33 deletions(-)
diff --git a/ruoyi-common/src/main/java/com/ruoyi/basic/service/impl/StorageBlobServiceImpl.java b/ruoyi-common/src/main/java/com/ruoyi/basic/service/impl/StorageBlobServiceImpl.java
index dc7acff..2f33993 100644
--- a/ruoyi-common/src/main/java/com/ruoyi/basic/service/impl/StorageBlobServiceImpl.java
+++ b/ruoyi-common/src/main/java/com/ruoyi/basic/service/impl/StorageBlobServiceImpl.java
@@ -2,29 +2,37 @@
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
-import com.baomidou.mybatisplus.core.toolkit.Wrappers;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.basic.entity.StorageAttachment;
import com.ruoyi.basic.entity.StorageBlob;
import com.ruoyi.basic.entity.dto.StorageBlobDTO;
import com.ruoyi.basic.mapper.StorageAttachmentMapper;
import com.ruoyi.basic.mapper.StorageBlobMapper;
import com.ruoyi.basic.service.StorageBlobService;
-import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
-import com.ruoyi.common.config.MinioConfig;
-import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.MinioResult;
+import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.exception.file.InvalidExtensionException;
-import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.MinioUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Service;
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;
/**
* <p>
@@ -46,38 +54,155 @@
@Autowired
private MinioUtils minioUtils;
- @Override
- public List<StorageBlobDTO> updateStorageBlobs(List<MultipartFile> files, String bucketName) {
+ @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();
+ bucketName = minioUtils.getDefaultBucket();
}
-
List<StorageBlobDTO> storageBlobDTOs = new ArrayList<>();
for (MultipartFile file : files) {
try {
- MinioResult res = minioUtils.upload(bucketName, file, false);
- StorageBlobDTO dto = new StorageBlobDTO();
- dto.setContentType(file.getContentType());
- dto.setBucketFilename(res.getBucketFileName());
- dto.setOriginalFilename(res.getOriginalName());
- dto.setByteSize(file.getSize());
- dto.setKey(IdUtils.simpleUUID());
- dto.setBucketName(bucketName);
- dto.setCreateTime(DateUtils.getNowDate());
- dto.setUrl(minioUtils.getPreviewUrl(res.getBucketFileName(), bucketName, false));
- // 鎻掑叆鏁版嵁搴�
+ validateFileExtension(file);
+// MinioResult res = minioUtils.upload(bucketName, file, false);
+ // 涓嶄娇鐢╩inio锛屼娇鐢ㄦ湰鍦版枃浠朵笂浼�
+ 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("minio鏂囦欢涓婁紶寮傚父锛�" + 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) {
+
+ // 鑻ユ病浼犲叆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);
+// String res = uploadFile(file, type);
+ StorageBlobDTO dto = buildStorageBlobDTO(file, res, bucketName, type);
+
+ 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;
+ }
+
+ @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浠g悊璺緞
+
+ // 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 {
+ String filename = file.getOriginalFilename();
+ String extension = FilenameUtils.getExtension(filename).toLowerCase();
+ List<String> allowedExtensions = Arrays.asList(
+ // 鍥剧墖
+ "jpg", "jpeg", "png", "gif", "bmp", "webp", "tiff", "ico", "svg",
+
+ // 鏂囨。
+ "pdf", "doc", "docx", "xls", "xlsx", "ppt", "pptx", "txt", "rtf", "md", "csv", "odt",
+
+ // 瑙嗛
+ "mp4", "mov", "avi", "wmv", "flv", "mkv", "webm", "mpeg", "3gp","MOV",
+
+ // 闊抽
+ "mp3", "wav", "ogg", "aac", "flac", "m4a", "wma", "amr",
+
+ // 鍘嬬缉鍖�
+ "zip", "rar", "7z", "tar", "gz", "bz2", "xz",
+
+ // 缂栫▼浠g爜鏂囦欢
+ "java", "py", "js", "ts", "html", "css", "cpp", "c", "cs", "json", "xml", "sql", "yaml", "yml", "sh", "bat",
+
+ // 瀹夎绋嬪簭 & 浜岃繘鍒�
+ "exe", "apk", "dmg", "msi", "bin", "iso",
+
+ // 璁捐绫�
+ "psd", "ai", "xd", "sketch", "fig"
+ );
+
+ if (!allowedExtensions.contains(extension)) {
+ throw new BaseException("鏂囦欢绫诲瀷涓嶈鍏佽锛�" + extension);
+ }
+ }
+
+ private StorageBlobDTO buildStorageBlobDTO(MultipartFile file, MinioResult res, String bucketName, Long type) {
+ StorageBlobDTO dto = new StorageBlobDTO();
+ dto.setContentType(file.getContentType());
+ dto.setBucketFilename(res.getBucketFileName());
+ dto.setOriginalFilename(res.getOriginalName());
+ dto.setByteSize(file.getSize());
+ dto.setKey(IdUtils.simpleUUID());
+ dto.setBucketName(bucketName);
+ dto.setUrl(minioUtils.getPreviewUrl(res.getBucketFileName(), bucketName, false));
+ dto.setDownloadUrl(minioUtils.getDownloadUrl(res.getBucketFileName(), bucketName));
+
+ if (type != null) {
+ dto.setType(type);
+ }
+
+ 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
@@ -86,20 +211,32 @@
.eq(StorageAttachment::getRecordId, attachment.getRecordId())
.eq(StorageAttachment::getRecordType, attachment.getRecordType())
.eq(StorageAttachment::getName, attachment.getName()));
- List<Long> ids = attachments.stream().map(StorageAttachment::getStorageBlobId).toList();
- List<StorageBlob> storageBlobs = storageBlobMapper.selectList(new LambdaQueryWrapper<StorageBlob>()
- .in(StorageBlob::getId, ids));
+ List<Long> ids = attachments.stream()
+ .map(StorageAttachment::getStorageBlobId)
+ .collect(Collectors.toList());
+ List<StorageBlob> storageBlobs = new ArrayList<>();
+ if (!ids.isEmpty()) {
+ // 鍙湪ID鍒楄〃闈炵┖鏃舵墽琛屾煡璇�
+ storageBlobs = storageBlobMapper.selectList(
+ new LambdaQueryWrapper<StorageBlob>().in(StorageBlob::getId, ids)
+ );
+ }
+
+ // 绉婚櫎MinIO涓殑鏂囦欢
if (!storageBlobs.isEmpty()) {
for (StorageBlob storageBlob : storageBlobs) {
- // 绉婚櫎妗跺唴鏂囦欢
- minioUtils.removeObjectsResult(storageBlob.getBucketName(), storageBlob.getBucketName());
+ minioUtils.removeObjectsResult(storageBlob.getBucketName(), storageBlob.getBucketFilename());
}
}
+ // 鍒犻櫎鏁版嵁搴撹褰�
if (!ids.isEmpty()) {
- return storageBlobMapper.delete(new QueryWrapper<StorageBlob>().lambda().in(StorageBlob::getId, ids));
+ return storageBlobMapper.delete(
+ new QueryWrapper<StorageBlob>().lambda().in(StorageBlob::getId, ids)
+ );
}
-
return 0;
}
+
+
}
--
Gitblit v1.9.3