chenhj
8 天以前 d55278560d29562b341aafa1652209a8aae0af33
src/main/java/com/ruoyi/basic/service/impl/StorageBlobServiceImpl.java
@@ -20,6 +20,7 @@
import javax.crypto.SecretKey;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
@@ -39,7 +40,7 @@
    private final FileUtil fileUtil;
    @Override
    public List<StorageBlobVO> upload(List<MultipartFile> files) {
    public List<StorageBlobVO> upload(List<MultipartFile> files, Boolean isPublic) {
        if (CollectionUtils.isEmpty(files)) {
            throw new IllegalArgumentException("文件不能为空");
        }
@@ -65,7 +66,7 @@
            StorageBlobVO storageBlob;
            try {
                file.transferTo(dest);
                storageBlob = getStorageBlob(file, originalFileName, fileName, relativePath);
                storageBlob = getStorageBlob(file, originalFileName, fileName, relativePath, isPublic);
                if (storageBlob == null || storageBlob.getId() == null) {
                    throw new RuntimeException("文件元数据保存失败");
                }
@@ -109,13 +110,35 @@
        return new File(new File(properties.getPath(), path), fileName);
    }
    @Override
    public File getPublicFile(String fileName, String publicKey) {
        if (!StringUtils.hasText(fileName)) {
            throw new IllegalArgumentException("文件名不能为空");
        }
        if (!StringUtils.hasText(publicKey)) {
            throw new IllegalArgumentException("publicKey不能为空");
        }
        StorageBlob storageBlob = storageBlobMapper.selectOne(new LambdaQueryWrapper<StorageBlob>()
                .eq(StorageBlob::getUidFilename, fileName)
                .eq(StorageBlob::getResourceKey, publicKey)
                .last("limit 1"));
        if (storageBlob == null) {
            throw new IllegalArgumentException("公开文件不存在或publicKey不匹配");
        }
        String path = storageBlob.getPath();
        if (!StringUtils.hasText(path)) {
            return new File(properties.getPath(), fileName);
        }
        return new File(new File(properties.getPath(), path), fileName);
    }
    private StorageBlob findStorageBlob(String fileName) {
        return storageBlobMapper.selectOne(new LambdaQueryWrapper<StorageBlob>()
                .eq(StorageBlob::getUidFilename, fileName)
                .last("limit 1"));
    }
    private StorageBlobVO getStorageBlob(MultipartFile file, String originalFileName, String fileName, String relativePath) {
    private StorageBlobVO getStorageBlob(MultipartFile file, String originalFileName, String fileName, String relativePath, Boolean isPublic) {
        StorageBlobVO storageBlob = new StorageBlobVO();
        storageBlob.setResourceKey(UUID.randomUUID().toString().replace("-", ""));
        storageBlob.setContentType(file.getContentType());
@@ -123,8 +146,13 @@
        storageBlob.setUidFilename(fileName);
        storageBlob.setByteSize(file.getSize());
        storageBlob.setPath(relativePath);
        storageBlob.setPreviewURL(fileUtil.buildSignedPreviewUrl(storageBlob));
        storageBlob.setDownloadURL(fileUtil.buildSignedDownloadUrl(storageBlob));
        if (isPublic) {
            storageBlob.setPreviewURL(fileUtil.buildSignedUrl(storageBlob, "/preview/", BigDecimal.valueOf(-1)));
            storageBlob.setDownloadURL(fileUtil.buildSignedUrl(storageBlob, "/download/", BigDecimal.valueOf(-1)));
        } else {
            storageBlob.setPreviewURL(fileUtil.buildSignedPreviewUrl(storageBlob));
            storageBlob.setDownloadURL(fileUtil.buildSignedDownloadUrl(storageBlob));
        }
        int affectedRows = storageBlobMapper.insert(storageBlob);
        if (affectedRows <= 0) {
            throw new RuntimeException("文件元数据保存失败");