Skip to content

Commit

Permalink
feat: Time placeholder of upload path (#74)
Browse files Browse the repository at this point in the history
Fixes #69 

```release-note
文件上传路径支持使用`${year}`,`${month}`,`${day}`占位符
```
  • Loading branch information
AccAutomaton authored Sep 27, 2023
1 parent 38f2018 commit 217f1db
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
23 changes: 23 additions & 0 deletions src/main/java/run/halo/s3os/FilePathUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package run.halo.s3os;

import org.apache.commons.lang3.StringUtils;

import java.time.LocalDate;

public class FilePathUtils {
private FilePathUtils() {

}

public static String getFilePathByPlaceholder(String filename) {
LocalDate localDate = LocalDate.now();
return StringUtils.replaceEach(filename,
new String[] {"${year}","${month}","${day}"},
new String[] {
String.valueOf(localDate.getYear()),
String.valueOf(localDate.getMonthValue()),
String.valueOf(localDate.getDayOfMonth())
}
);
}
}
5 changes: 3 additions & 2 deletions src/main/java/run/halo/s3os/S3LinkServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,13 @@ public Mono<S3ListResult> listObjects(String policyName, String continuationToke
})
.flatMap((configMap) -> {
var properties = handler.getProperties(configMap);
var finalLocation = FilePathUtils.getFilePathByPlaceholder(properties.getLocation());
return Mono.using(() -> handler.buildS3Client(properties),
(s3Client) -> Mono.fromCallable(
() -> s3Client.listObjectsV2(ListObjectsV2Request.builder()
.bucket(properties.getBucket())
.prefix(StringUtils.isNotEmpty(properties.getLocation())
? properties.getLocation() + "/" : null)
.prefix(StringUtils.isNotEmpty(finalLocation)
? finalLocation + "/" : null)
.delimiter("/")
.maxKeys(pageSize)
.continuationToken(StringUtils.isNotEmpty(continuationToken)
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/run/halo/s3os/S3OsProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import lombok.Data;
import org.springframework.util.StringUtils;

import java.time.LocalDate;

@Data
class S3OsProperties {

Expand Down Expand Up @@ -39,8 +41,9 @@ class S3OsProperties {

public String getObjectName(String filename) {
var objectName = filename;
if (StringUtils.hasText(getLocation())) {
objectName = getLocation() + "/" + objectName;
var finalName = FilePathUtils.getFilePathByPlaceholder(getLocation());
if (StringUtils.hasText(finalName)) {
objectName = finalName + "/" + objectName;
}
return objectName;
}
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/extensions/policy-template-s3os.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ spec:
name: location
label: 上传目录
placeholder: 如不填写,则默认上传到根目录
help: 支持使用 ${year} ${month} ${day} 占位符
- $formkit: select
name: randomFilenameMode
label: 上传时重命名文件方式
Expand Down

0 comments on commit 217f1db

Please sign in to comment.