mirror of https://github.com/halo-dev/halo
feat: support set file source for tencent cos.
parent
b7ac3f5fed
commit
2701146860
|
@ -72,13 +72,25 @@ public class AliOssFileHandler implements FileHandler {
|
||||||
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());
|
String timestamp = String.valueOf(System.currentTimeMillis());
|
||||||
String upFilePath = StringUtils.join(source, "/", basename, "_", timestamp, ".", extension);
|
StringBuilder upFilePath = new StringBuilder();
|
||||||
String filePath = StringUtils.join(basePath.toString(), upFilePath);
|
|
||||||
|
if (StringUtils.isNotEmpty(source)) {
|
||||||
|
upFilePath.append(source)
|
||||||
|
.append("/");
|
||||||
|
}
|
||||||
|
|
||||||
|
upFilePath.append(basename)
|
||||||
|
.append("_")
|
||||||
|
.append(timestamp)
|
||||||
|
.append(".")
|
||||||
|
.append(extension);
|
||||||
|
|
||||||
|
String filePath = StringUtils.join(basePath.toString(), upFilePath.toString());
|
||||||
|
|
||||||
log.info(basePath.toString());
|
log.info(basePath.toString());
|
||||||
|
|
||||||
// Upload
|
// Upload
|
||||||
PutObjectResult putObjectResult = ossClient.putObject(bucketName, upFilePath, file.getInputStream());
|
PutObjectResult putObjectResult = ossClient.putObject(bucketName, upFilePath.toString(), file.getInputStream());
|
||||||
if (putObjectResult == null) {
|
if (putObjectResult == null) {
|
||||||
throw new FileOperationException("上传附件 " + file.getOriginalFilename() + " 到阿里云失败 ");
|
throw new FileOperationException("上传附件 " + file.getOriginalFilename() + " 到阿里云失败 ");
|
||||||
}
|
}
|
||||||
|
@ -87,7 +99,7 @@ public class AliOssFileHandler implements FileHandler {
|
||||||
UploadResult uploadResult = new UploadResult();
|
UploadResult uploadResult = new UploadResult();
|
||||||
uploadResult.setFilename(basename);
|
uploadResult.setFilename(basename);
|
||||||
uploadResult.setFilePath(StringUtils.isBlank(styleRule) ? filePath : filePath + styleRule);
|
uploadResult.setFilePath(StringUtils.isBlank(styleRule) ? filePath : filePath + styleRule);
|
||||||
uploadResult.setKey(upFilePath);
|
uploadResult.setKey(upFilePath.toString());
|
||||||
uploadResult.setMediaType(MediaType.valueOf(Objects.requireNonNull(file.getContentType())));
|
uploadResult.setMediaType(MediaType.valueOf(Objects.requireNonNull(file.getContentType())));
|
||||||
uploadResult.setSuffix(extension);
|
uploadResult.setSuffix(extension);
|
||||||
uploadResult.setSize(file.getSize());
|
uploadResult.setSize(file.getSize());
|
||||||
|
|
|
@ -53,38 +53,56 @@ public class TencentCosFileHandler implements FileHandler {
|
||||||
String secretId = optionService.getByPropertyOfNonNull(TencentCosProperties.COS_SECRET_ID).toString();
|
String secretId = optionService.getByPropertyOfNonNull(TencentCosProperties.COS_SECRET_ID).toString();
|
||||||
String secretKey = optionService.getByPropertyOfNonNull(TencentCosProperties.COS_SECRET_KEY).toString();
|
String secretKey = optionService.getByPropertyOfNonNull(TencentCosProperties.COS_SECRET_KEY).toString();
|
||||||
String bucketName = optionService.getByPropertyOfNonNull(TencentCosProperties.COS_BUCKET_NAME).toString();
|
String bucketName = optionService.getByPropertyOfNonNull(TencentCosProperties.COS_BUCKET_NAME).toString();
|
||||||
String source = StringUtils.join(protocol, bucketName, ".cos." + region + ".myqcloud.com");
|
String source = optionService.getByPropertyOrDefault(TencentCosProperties.COS_SOURCE, String.class, "");
|
||||||
String styleRule = optionService.getByPropertyOrDefault(TencentCosProperties.COS_STYLE_RULE, String.class, "");
|
String styleRule = optionService.getByPropertyOrDefault(TencentCosProperties.COS_STYLE_RULE, String.class, "");
|
||||||
String thumbnailStyleRule = optionService.getByPropertyOrDefault(TencentCosProperties.COS_THUMBNAIL_STYLE_RULE, String.class, "");
|
String thumbnailStyleRule = optionService.getByPropertyOrDefault(TencentCosProperties.COS_THUMBNAIL_STYLE_RULE, String.class, "");
|
||||||
|
|
||||||
//get file attribute
|
|
||||||
long size = file.getSize();
|
|
||||||
String contentType = file.getContentType();
|
|
||||||
|
|
||||||
COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
|
COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
|
||||||
Region regionConfig = new Region(region);
|
Region regionConfig = new Region(region);
|
||||||
ClientConfig clientConfig = new ClientConfig(regionConfig);
|
ClientConfig clientConfig = new ClientConfig(regionConfig);
|
||||||
|
|
||||||
|
|
||||||
// Init OSS client
|
// Init OSS client
|
||||||
COSClient cosClient = new COSClient(cred, clientConfig);
|
COSClient cosClient = new COSClient(cred, clientConfig);
|
||||||
|
|
||||||
domain = protocol + domain;
|
StringBuilder basePath = new StringBuilder(protocol);
|
||||||
|
|
||||||
|
if (StringUtils.isNotEmpty(domain)) {
|
||||||
|
basePath.append(domain)
|
||||||
|
.append("/");
|
||||||
|
} else {
|
||||||
|
basePath.append(bucketName)
|
||||||
|
.append(".cos.")
|
||||||
|
.append(region)
|
||||||
|
.append(".myqcloud.com")
|
||||||
|
.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());
|
String timestamp = String.valueOf(System.currentTimeMillis());
|
||||||
String upFilePath = StringUtils.join(basename, "_", timestamp, ".", extension);
|
StringBuilder upFilePath = new StringBuilder();
|
||||||
String filePath = StringUtils.join(StringUtils.appendIfMissing(StringUtils.isNotBlank(domain) ? domain : source, "/"), upFilePath);
|
|
||||||
|
if (StringUtils.isNotEmpty(source)) {
|
||||||
|
upFilePath.append(source)
|
||||||
|
.append("/");
|
||||||
|
}
|
||||||
|
|
||||||
|
upFilePath.append(basename)
|
||||||
|
.append("_")
|
||||||
|
.append(timestamp)
|
||||||
|
.append(".")
|
||||||
|
.append(extension);
|
||||||
|
|
||||||
|
String filePath = StringUtils.join(basePath.toString(), upFilePath.toString());
|
||||||
|
|
||||||
// Upload
|
// Upload
|
||||||
ObjectMetadata objectMetadata = new ObjectMetadata();
|
ObjectMetadata objectMetadata = new ObjectMetadata();
|
||||||
//提前告知输入流的长度, 否则可能导致 oom
|
//提前告知输入流的长度, 否则可能导致 oom
|
||||||
objectMetadata.setContentLength(size);
|
objectMetadata.setContentLength(file.getSize());
|
||||||
// 设置 Content type, 默认是 application/octet-stream
|
// 设置 Content type, 默认是 application/octet-stream
|
||||||
objectMetadata.setContentType(contentType);
|
objectMetadata.setContentType(file.getContentType());
|
||||||
PutObjectResult putObjectResponseFromInputStream = cosClient.putObject(bucketName, upFilePath, file.getInputStream(), objectMetadata);
|
PutObjectResult putObjectResponseFromInputStream = cosClient.putObject(bucketName, upFilePath.toString(), file.getInputStream(), objectMetadata);
|
||||||
if (putObjectResponseFromInputStream == null) {
|
if (putObjectResponseFromInputStream == null) {
|
||||||
throw new FileOperationException("上传附件 " + file.getOriginalFilename() + " 到腾讯云失败 ");
|
throw new FileOperationException("上传附件 " + file.getOriginalFilename() + " 到腾讯云失败 ");
|
||||||
}
|
}
|
||||||
|
@ -93,7 +111,7 @@ public class TencentCosFileHandler implements FileHandler {
|
||||||
UploadResult uploadResult = new UploadResult();
|
UploadResult uploadResult = new UploadResult();
|
||||||
uploadResult.setFilename(basename);
|
uploadResult.setFilename(basename);
|
||||||
uploadResult.setFilePath(StringUtils.isBlank(styleRule) ? filePath : filePath + styleRule);
|
uploadResult.setFilePath(StringUtils.isBlank(styleRule) ? filePath : filePath + styleRule);
|
||||||
uploadResult.setKey(upFilePath);
|
uploadResult.setKey(upFilePath.toString());
|
||||||
uploadResult.setMediaType(MediaType.valueOf(Objects.requireNonNull(file.getContentType())));
|
uploadResult.setMediaType(MediaType.valueOf(Objects.requireNonNull(file.getContentType())));
|
||||||
uploadResult.setSuffix(extension);
|
uploadResult.setSuffix(extension);
|
||||||
uploadResult.setSize(file.getSize());
|
uploadResult.setSize(file.getSize());
|
||||||
|
|
|
@ -40,7 +40,7 @@ public enum AliOssProperties implements PropertyEnum {
|
||||||
OSS_ACCESS_SECRET("oss_ali_access_secret", String.class, ""),
|
OSS_ACCESS_SECRET("oss_ali_access_secret", String.class, ""),
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* upyun oss source
|
* Aliyun oss source
|
||||||
*/
|
*/
|
||||||
OSS_SOURCE("oss_ali_source", String.class, ""),
|
OSS_SOURCE("oss_ali_source", String.class, ""),
|
||||||
|
|
||||||
|
|
|
@ -39,6 +39,11 @@ public enum TencentCosProperties implements PropertyEnum {
|
||||||
*/
|
*/
|
||||||
COS_SECRET_KEY("cos_tencent_secret_key", String.class, ""),
|
COS_SECRET_KEY("cos_tencent_secret_key", String.class, ""),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tencent cos source
|
||||||
|
*/
|
||||||
|
COS_SOURCE("cos_tencent_source", String.class, ""),
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tencent cos style rule.
|
* Tencent cos style rule.
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in New Issue