From 068fd54e1e5ad6fd48e28d231d70c9399d098a2c Mon Sep 17 00:00:00 2001 From: sol Date: Fri, 19 Sep 2025 22:30:53 +0800 Subject: [PATCH] =?UTF-8?q?refactor(file):=20=E6=96=87=E4=BB=B6=E5=AD=98?= =?UTF-8?q?=E5=82=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../model/file/file/entity/FileEntity.java | 4 +++- .../web/file/file/service/OssService.java | 23 +++++++++---------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/cloud/model/model-file/src/main/java/com/bgasol/model/file/file/entity/FileEntity.java b/cloud/model/model-file/src/main/java/com/bgasol/model/file/file/entity/FileEntity.java index 4f34a34..4467588 100644 --- a/cloud/model/model-file/src/main/java/com/bgasol/model/file/file/entity/FileEntity.java +++ b/cloud/model/model-file/src/main/java/com/bgasol/model/file/file/entity/FileEntity.java @@ -4,7 +4,9 @@ import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableName; import com.bgasol.common.core.base.entity.BaseEntity; import io.swagger.v3.oas.annotations.media.Schema; -import lombok.*; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; import lombok.experimental.SuperBuilder; @Setter diff --git a/cloud/web/web-file-8082/src/main/java/com/bgasol/web/file/file/service/OssService.java b/cloud/web/web-file-8082/src/main/java/com/bgasol/web/file/file/service/OssService.java index 86b8ce5..f2eb375 100644 --- a/cloud/web/web-file-8082/src/main/java/com/bgasol/web/file/file/service/OssService.java +++ b/cloud/web/web-file-8082/src/main/java/com/bgasol/web/file/file/service/OssService.java @@ -20,7 +20,6 @@ import java.security.NoSuchAlgorithmException; import java.time.LocalDate; import java.time.ZoneId; import java.time.format.DateTimeFormatter; -import java.util.Date; @Service @RequiredArgsConstructor @@ -29,8 +28,6 @@ import java.util.Date; public class OssService { private final MinioClient minioClient; - private static final String FILE_SEPARATOR = ":"; - /** * 写入文件流到对象存储 */ @@ -46,7 +43,7 @@ public class OssService { PutObjectArgs objectArgs = PutObjectArgs .builder() .bucket(file.getBucket()) - .object(dateStr + source + file.getId() + FILE_SEPARATOR + file.getName()) + .object(buildObjectPath(file)) .stream(inputStream, file.getSize(), -1) .contentType(file.getType()) .build(); @@ -108,13 +105,15 @@ public class OssService { * 构建对象存储路径 */ private String buildObjectPath(FileEntity file) { - Date createTime = file.getCreateTime(); - LocalDate localDate = createTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); - String dateStr = localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + "/"; - - String source = file.getSource(); - source = ObjectUtils.isEmpty(source) ? "" : source + "/"; - - return dateStr + source + file.getId() + FILE_SEPARATOR + file.getName(); + return "%s/%s/%s_%s".formatted( + file.getSource(), + file.getCreateTime() + .toInstant() + .atZone(ZoneId.systemDefault()) + .toLocalDate() + .format(DateTimeFormatter.ISO_LOCAL_DATE), + file.getId(), + file.getName() + ); } }