| | |
| | | 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; |
| | |
| | | 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("文件不能为空"); |
| | | } |
| | |
| | | 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("文件元数据保存失败"); |
| | | } |
| | |
| | | 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()); |
| | |
| | | storageBlob.setUidFilename(fileName); |
| | | storageBlob.setByteSize(file.getSize()); |
| | | storageBlob.setPath(relativePath); |
| | | 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("文件元数据保存失败"); |