Skip to content

Commit

Permalink
fix: s3 link error when filename renaming enable (#91)
Browse files Browse the repository at this point in the history
fixes #90
请测试 `上传时重命名文件方式` 选项为**非**保留原文件名时,是否能正常关联s3文件
![image](https://github.com/halo-dev/plugin-s3/assets/28662535/22ab0467-60bd-4c4c-a486-38bc922b7f37)

```release-note
当上传时文件重命名功能开启时关联S3文件错误
```
  • Loading branch information
longjuan committed Oct 26, 2023
1 parent c79fee9 commit c0fb2b1
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/main/java/run/halo/s3os/S3LinkServiceImpl.java
Expand Up @@ -184,7 +184,7 @@ public Mono<LinkResult.LinkResultItem> addAttachmentRecord(String policyName,
.map(headObjectResponse -> {
var objectDetail = new S3OsAttachmentHandler.ObjectDetail(
new S3OsAttachmentHandler.UploadState(properties,
FileNameUtils.extractFileNameFromS3Key(objectKey)),
FileNameUtils.extractFileNameFromS3Key(objectKey), false),
headObjectResponse);
return handler.buildAttachment(properties, objectDetail);
})
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/run/halo/s3os/S3OsAttachmentHandler.java
Expand Up @@ -304,7 +304,7 @@ ByteBuffer deepCopy(ByteBuffer src) {
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();

Expand Down Expand Up @@ -492,12 +492,14 @@ static class UploadState {
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);
Expand Down

0 comments on commit c0fb2b1

Please sign in to comment.