refactor(file): 文件存储

This commit is contained in:
sol 2025-09-19 22:30:53 +08:00
parent 9849aecfa2
commit 068fd54e1e
2 changed files with 14 additions and 13 deletions

View file

@ -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

View file

@ -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()
);
}
}