-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ability to configure and use image thumbnails (#167)
### What this PR does? 适配 Halo 2.19(WIP) 的缩略图机制支持 S3 配置并使用缩略图 缩略图规则参考: - [腾讯云 OSS 图片缩放](https://cloud.tencent.com/document/product/1246/109210) - [百度 OSS 图片缩放](https://cloud.baidu.com/doc/BOS/s/gkbisf3l4) - [阿里云 OSS 图片缩放](https://help.aliyun.com/zh/oss/user-guide/resize-images-4) - [七牛云 OSS 图片缩放](https://developer.qiniu.com/dora/api/basic-processing-images-imageview2) - [青云 OSS 图片缩放](https://docsv3.qingcloud.com/storage/object-storage/api/object/image_process/resize/) - [京东云 OSS](https://docs.jdcloud.com/cn/object-storage-service/resize-images) - [又拍云图片缩放](https://docs.upyun.com/cloud/image/#_11) ```release-note 支持配置并使用图片缩略图机制 ```
- Loading branch information
Showing
8 changed files
with
183 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package run.halo.s3os; | ||
|
||
import java.net.URI; | ||
import java.net.URL; | ||
import java.util.Map; | ||
import com.google.common.cache.Cache; | ||
import com.google.common.cache.CacheBuilder; | ||
import lombok.Builder; | ||
import lombok.RequiredArgsConstructor; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.util.UriComponentsBuilder; | ||
import reactor.core.publisher.Flux; | ||
import reactor.core.publisher.Mono; | ||
import run.halo.app.core.attachment.ThumbnailProvider; | ||
import run.halo.app.core.attachment.ThumbnailSize; | ||
import run.halo.app.extension.ConfigMap; | ||
import run.halo.app.extension.ReactiveExtensionClient; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class S3ThumbnailProvider implements ThumbnailProvider { | ||
static final String WIDTH_PLACEHOLDER = "{width}"; | ||
private final Cache<String, S3PropsCacheValue> s3PropsCache = CacheBuilder.newBuilder() | ||
.maximumSize(50) | ||
.build(); | ||
|
||
private final ReactiveExtensionClient client; | ||
private final S3LinkService s3LinkService; | ||
|
||
@Override | ||
public Mono<URI> generate(ThumbnailContext thumbnailContext) { | ||
var url = thumbnailContext.getImageUrl().toString(); | ||
var size = thumbnailContext.getSize(); | ||
return getCacheValue(url) | ||
.mapNotNull(cacheValue -> placedPattern(cacheValue.pattern(), size)) | ||
.map(param -> { | ||
if (param.startsWith("?")) { | ||
return UriComponentsBuilder.fromHttpUrl(url) | ||
.queryParam(param.substring(1)) | ||
.build() | ||
.toString(); | ||
} | ||
return url + param; | ||
}) | ||
.map(URI::create); | ||
} | ||
|
||
private static String placedPattern(String pattern, ThumbnailSize size) { | ||
return StringUtils.replace(pattern, WIDTH_PLACEHOLDER, String.valueOf(size.getWidth())); | ||
} | ||
|
||
@Override | ||
public Mono<Void> delete(URL url) { | ||
// do nothing for s3 | ||
return Mono.empty(); | ||
} | ||
|
||
@Override | ||
public Mono<Boolean> supports(ThumbnailContext thumbnailContext) { | ||
var url = thumbnailContext.getImageUrl().toString(); | ||
return getCacheValue(url).hasElement(); | ||
} | ||
|
||
private Mono<S3PropsCacheValue> getCacheValue(String imageUrl) { | ||
return Flux.fromIterable(s3PropsCache.asMap().entrySet()) | ||
.filter(entry -> imageUrl.startsWith(entry.getKey())) | ||
.next() | ||
.map(Map.Entry::getValue) | ||
.switchIfEmpty(Mono.defer(() -> listAllS3ObjectDomain() | ||
.filter(entry -> imageUrl.startsWith(entry.getKey())) | ||
.map(Map.Entry::getValue) | ||
.next() | ||
)); | ||
} | ||
|
||
@Builder | ||
record S3PropsCacheValue(String pattern, String configMapName) { | ||
} | ||
|
||
private Flux<Map.Entry<String, S3PropsCacheValue>> listAllS3ObjectDomain() { | ||
return s3LinkService.listS3Policies() | ||
.flatMap(s3Policy -> { | ||
var s3ConfigMapName = s3Policy.getSpec().getConfigMapName(); | ||
return fetchS3PropsByConfigMapName(s3ConfigMapName) | ||
.mapNotNull(properties -> { | ||
var thumbnailParam = properties.getThumbnailParamPattern(); | ||
if (StringUtils.isBlank(thumbnailParam)) { | ||
return null; | ||
} | ||
var objectDomain = properties.toObjectURL(""); | ||
var cacheValue = S3PropsCacheValue.builder() | ||
.pattern(thumbnailParam) | ||
.configMapName(s3ConfigMapName) | ||
.build(); | ||
return Map.entry(objectDomain, cacheValue); | ||
}); | ||
}) | ||
.doOnNext(cache -> s3PropsCache.put(cache.getKey(), cache.getValue())); | ||
} | ||
|
||
private Mono<S3OsProperties> fetchS3PropsByConfigMapName(String name) { | ||
return client.fetch(ConfigMap.class, name) | ||
.map(S3OsProperties::convertFrom); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
apiVersion: plugin.halo.run/v1alpha1 | ||
kind: ExtensionDefinition | ||
metadata: | ||
name: s3os-thumbnail-provider | ||
spec: | ||
className: run.halo.s3os.S3ThumbnailProvider | ||
extensionPointName: thumbnail-provider | ||
displayName: "S3 协议 OSS 缩略图生成" | ||
description: "为上传到支持 S3 协议的 OSS 的图片生成缩略图" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters