fix: s3 link error when filename renaming enable (#91)

fixes https://github.com/halo-dev/plugin-s3/issues/90
请测试 `上传时重命名文件方式` 选项为**非**保留原文件名时,是否能正常关联s3文件
![image](https://github.com/halo-dev/plugin-s3/assets/28662535/22ab0467-60bd-4c4c-a486-38bc922b7f37)

```release-note
当上传时文件重命名功能开启时关联S3文件错误
```
This commit is contained in:
longjuan
2023-10-26 14:38:14 +08:00
committed by GitHub
parent c79fee9ba1
commit c0fb2b1017
2 changed files with 6 additions and 4 deletions

View File

@@ -184,7 +184,7 @@ public class S3LinkServiceImpl implements S3LinkService {
.map(headObjectResponse -> {
var objectDetail = new S3OsAttachmentHandler.ObjectDetail(
new S3OsAttachmentHandler.UploadState(properties,
FileNameUtils.extractFileNameFromS3Key(objectKey)),
FileNameUtils.extractFileNameFromS3Key(objectKey), false),
headObjectResponse);
return handler.buildAttachment(properties, objectDetail);
})

View File

@@ -304,7 +304,7 @@ public class S3OsAttachmentHandler implements AttachmentHandler {
Mono<ObjectDetail> upload(UploadContext uploadContext, S3OsProperties properties) {
return Mono.using(() -> buildS3Client(properties),
client -> {
var uploadState = new UploadState(properties, uploadContext.file().filename());
var uploadState = new UploadState(properties, uploadContext.file().filename(), true);
var content = uploadContext.file().content();
@@ -492,12 +492,14 @@ public class S3OsAttachmentHandler implements AttachmentHandler {
String objectKey;
boolean needRemoveMapKey = false;
public UploadState(S3OsProperties properties, String fileName) {
public UploadState(S3OsProperties properties, String fileName, boolean needRandomJudge) {
this.properties = properties;
this.originalFileName = fileName;
fileName = FileNameUtils.getRandomFilename(fileName,
if (needRandomJudge) {
fileName = FileNameUtils.getRandomFilename(fileName,
properties.getRandomStringLength(), properties.getRandomFilenameMode());
}
this.fileName = fileName;
this.objectKey = properties.getObjectName(fileName);