mirror of https://github.com/halo-dev/halo
feat: support set file source for qiniu oss.
parent
2701146860
commit
9ac31d0b1c
|
@ -60,6 +60,7 @@ public class QiniuOssFileHandler implements FileHandler {
|
||||||
String bucket = optionService.getByPropertyOfNonNull(QiniuOssProperties.OSS_BUCKET).toString();
|
String bucket = optionService.getByPropertyOfNonNull(QiniuOssProperties.OSS_BUCKET).toString();
|
||||||
String protocol = optionService.getByPropertyOfNonNull(QiniuOssProperties.OSS_PROTOCOL).toString();
|
String protocol = optionService.getByPropertyOfNonNull(QiniuOssProperties.OSS_PROTOCOL).toString();
|
||||||
String domain = optionService.getByPropertyOfNonNull(QiniuOssProperties.OSS_DOMAIN).toString();
|
String domain = optionService.getByPropertyOfNonNull(QiniuOssProperties.OSS_DOMAIN).toString();
|
||||||
|
String source = optionService.getByPropertyOrDefault(QiniuOssProperties.OSS_SOURCE, String.class, "");
|
||||||
String styleRule = optionService.getByPropertyOrDefault(QiniuOssProperties.OSS_STYLE_RULE, String.class, "");
|
String styleRule = optionService.getByPropertyOrDefault(QiniuOssProperties.OSS_STYLE_RULE, String.class, "");
|
||||||
String thumbnailStyleRule = optionService.getByPropertyOrDefault(QiniuOssProperties.OSS_THUMBNAIL_STYLE_RULE, String.class, "");
|
String thumbnailStyleRule = optionService.getByPropertyOrDefault(QiniuOssProperties.OSS_THUMBNAIL_STYLE_RULE, String.class, "");
|
||||||
|
|
||||||
|
@ -81,16 +82,31 @@ public class QiniuOssFileHandler implements FileHandler {
|
||||||
// Create temp path
|
// Create temp path
|
||||||
Path tmpPath = Paths.get(System.getProperty("java.io.tmpdir"), bucket);
|
Path tmpPath = Paths.get(System.getProperty("java.io.tmpdir"), bucket);
|
||||||
|
|
||||||
|
StringBuilder basePath = new StringBuilder(protocol)
|
||||||
|
.append(domain)
|
||||||
|
.append("/");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
String basename = FilenameUtils.getBasename(file.getOriginalFilename());
|
String basename = FilenameUtils.getBasename(file.getOriginalFilename());
|
||||||
String extension = FilenameUtils.getExtension(file.getOriginalFilename());
|
String extension = FilenameUtils.getExtension(file.getOriginalFilename());
|
||||||
|
String timestamp = String.valueOf(System.currentTimeMillis());
|
||||||
|
StringBuilder upFilePath = new StringBuilder();
|
||||||
|
if (StringUtils.isNotEmpty(source)) {
|
||||||
|
upFilePath.append(source)
|
||||||
|
.append("/");
|
||||||
|
}
|
||||||
|
upFilePath.append(basename)
|
||||||
|
.append("_")
|
||||||
|
.append(timestamp)
|
||||||
|
.append(".")
|
||||||
|
.append(extension);
|
||||||
|
|
||||||
// Get file recorder for temp directory
|
// Get file recorder for temp directory
|
||||||
FileRecorder fileRecorder = new FileRecorder(tmpPath.toFile());
|
FileRecorder fileRecorder = new FileRecorder(tmpPath.toFile());
|
||||||
// Get upload manager
|
// Get upload manager
|
||||||
UploadManager uploadManager = new UploadManager(configuration, fileRecorder);
|
UploadManager uploadManager = new UploadManager(configuration, fileRecorder);
|
||||||
// Put the file
|
// Put the file
|
||||||
Response response = uploadManager.put(file.getInputStream(), null, uploadToken, null, null);
|
Response response = uploadManager.put(file.getInputStream(), upFilePath.toString(), uploadToken, null, null);
|
||||||
|
|
||||||
log.debug("QnYun response: [{}]", response.toString());
|
log.debug("QnYun response: [{}]", response.toString());
|
||||||
log.debug("QnYun response body: [{}]", response.bodyString());
|
log.debug("QnYun response body: [{}]", response.bodyString());
|
||||||
|
@ -101,13 +117,13 @@ public class QiniuOssFileHandler implements FileHandler {
|
||||||
QiNiuPutSet putSet = JsonUtils.jsonToObject(response.bodyString(), QiNiuPutSet.class);
|
QiNiuPutSet putSet = JsonUtils.jsonToObject(response.bodyString(), QiNiuPutSet.class);
|
||||||
|
|
||||||
// Get file full path
|
// Get file full path
|
||||||
String filePath = protocol + StringUtils.appendIfMissing(domain, "/") + putSet.getHash();
|
String filePath = StringUtils.join(basePath.toString(), upFilePath.toString());
|
||||||
|
|
||||||
// Build upload result
|
// Build upload result
|
||||||
UploadResult result = new UploadResult();
|
UploadResult result = new UploadResult();
|
||||||
result.setFilename(basename);
|
result.setFilename(basename);
|
||||||
result.setFilePath(StringUtils.isBlank(styleRule) ? filePath : filePath + styleRule);
|
result.setFilePath(StringUtils.isBlank(styleRule) ? filePath : filePath + styleRule);
|
||||||
result.setKey(putSet.getKey());
|
result.setKey(upFilePath.toString());
|
||||||
result.setSuffix(extension);
|
result.setSuffix(extension);
|
||||||
result.setWidth(putSet.getWidth());
|
result.setWidth(putSet.getWidth());
|
||||||
result.setHeight(putSet.getHeight());
|
result.setHeight(putSet.getHeight());
|
||||||
|
|
|
@ -24,6 +24,11 @@ public enum QiniuOssProperties implements PropertyEnum {
|
||||||
*/
|
*/
|
||||||
OSS_SECRET_KEY("oss_qiniu_secret_key", String.class, ""),
|
OSS_SECRET_KEY("oss_qiniu_secret_key", String.class, ""),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Qiniu oss source
|
||||||
|
*/
|
||||||
|
OSS_SOURCE("oss_qiniu_source", String.class, ""),
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Qiniu oss domain protocol.
|
* Qiniu oss domain protocol.
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in New Issue