mirror of https://github.com/halo-dev/halo
feat: support set protocol for cloud storage.
parent
dbf12f711c
commit
4b59b9eb57
|
@ -26,7 +26,7 @@ import java.util.Objects;
|
||||||
*
|
*
|
||||||
* @author MyFaith
|
* @author MyFaith
|
||||||
* @author ryanwang
|
* @author ryanwang
|
||||||
* @date 2019-04-04 00:06:13
|
* @date 2019-04-04
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Component
|
@Component
|
||||||
|
@ -43,27 +43,30 @@ public class AliYunFileHandler implements FileHandler {
|
||||||
Assert.notNull(file, "Multipart file must not be null");
|
Assert.notNull(file, "Multipart file must not be null");
|
||||||
|
|
||||||
// Get config
|
// Get config
|
||||||
String ossDomain = optionService.getByPropertyOrDefault(AliYunProperties.OSS_DOMAIN, String.class, "");
|
String protocol = optionService.getByPropertyOfNonNull(AliYunProperties.OSS_PROTOCOL).toString();
|
||||||
String ossEndPoint = optionService.getByPropertyOfNonNull(AliYunProperties.OSS_ENDPOINT).toString();
|
String domain = optionService.getByPropertyOrDefault(AliYunProperties.OSS_DOMAIN, String.class, "");
|
||||||
String ossAccessKey = optionService.getByPropertyOfNonNull(AliYunProperties.OSS_ACCESS_KEY).toString();
|
String endPoint = optionService.getByPropertyOfNonNull(AliYunProperties.OSS_ENDPOINT).toString();
|
||||||
String ossAccessSecret = optionService.getByPropertyOfNonNull(AliYunProperties.OSS_ACCESS_SECRET).toString();
|
String accessKey = optionService.getByPropertyOfNonNull(AliYunProperties.OSS_ACCESS_KEY).toString();
|
||||||
String ossBucketName = optionService.getByPropertyOfNonNull(AliYunProperties.OSS_BUCKET_NAME).toString();
|
String accessSecret = optionService.getByPropertyOfNonNull(AliYunProperties.OSS_ACCESS_SECRET).toString();
|
||||||
String ossSource = StringUtils.join("https://", ossBucketName, "." + ossEndPoint);
|
String bucketName = optionService.getByPropertyOfNonNull(AliYunProperties.OSS_BUCKET_NAME).toString();
|
||||||
String ossStyleRule = optionService.getByPropertyOrDefault(AliYunProperties.OSS_STYLE_RULE, String.class, "");
|
String source = StringUtils.join(protocol, bucketName, "." + endPoint);
|
||||||
String ossThumbnailStyleRule = optionService.getByPropertyOrDefault(AliYunProperties.OSS_THUMBNAIL_STYLE_RULE, String.class, "");
|
String styleRule = optionService.getByPropertyOrDefault(AliYunProperties.OSS_STYLE_RULE, String.class, "");
|
||||||
|
String thumbnailStyleRule = optionService.getByPropertyOrDefault(AliYunProperties.OSS_THUMBNAIL_STYLE_RULE, String.class, "");
|
||||||
|
|
||||||
// Init OSS client
|
// Init OSS client
|
||||||
OSS ossClient = new OSSClientBuilder().build(ossEndPoint, ossAccessKey, ossAccessSecret);
|
OSS ossClient = new OSSClientBuilder().build(endPoint, accessKey, accessSecret);
|
||||||
|
|
||||||
|
domain = protocol + domain;
|
||||||
|
|
||||||
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);
|
String upFilePath = StringUtils.join(basename, "_", timestamp, ".", extension);
|
||||||
String filePath = StringUtils.join(StringUtils.appendIfMissing(StringUtils.isNotBlank(ossDomain) ? ossDomain : ossSource, "/"), upFilePath);
|
String filePath = StringUtils.join(StringUtils.appendIfMissing(StringUtils.isNotBlank(domain) ? domain : source, "/"), upFilePath);
|
||||||
|
|
||||||
// Upload
|
// Upload
|
||||||
PutObjectResult putObjectResult = ossClient.putObject(ossBucketName, upFilePath, file.getInputStream());
|
PutObjectResult putObjectResult = ossClient.putObject(bucketName, upFilePath, file.getInputStream());
|
||||||
if (putObjectResult == null) {
|
if (putObjectResult == null) {
|
||||||
throw new FileOperationException("上传附件 " + file.getOriginalFilename() + " 到阿里云失败 ");
|
throw new FileOperationException("上传附件 " + file.getOriginalFilename() + " 到阿里云失败 ");
|
||||||
}
|
}
|
||||||
|
@ -71,7 +74,7 @@ public class AliYunFileHandler implements FileHandler {
|
||||||
// Response result
|
// Response result
|
||||||
UploadResult uploadResult = new UploadResult();
|
UploadResult uploadResult = new UploadResult();
|
||||||
uploadResult.setFilename(basename);
|
uploadResult.setFilename(basename);
|
||||||
uploadResult.setFilePath(StringUtils.isBlank(ossStyleRule) ? filePath : filePath + ossStyleRule);
|
uploadResult.setFilePath(StringUtils.isBlank(styleRule) ? filePath : filePath + styleRule);
|
||||||
uploadResult.setKey(upFilePath);
|
uploadResult.setKey(upFilePath);
|
||||||
uploadResult.setMediaType(MediaType.valueOf(Objects.requireNonNull(file.getContentType())));
|
uploadResult.setMediaType(MediaType.valueOf(Objects.requireNonNull(file.getContentType())));
|
||||||
uploadResult.setSuffix(extension);
|
uploadResult.setSuffix(extension);
|
||||||
|
@ -82,7 +85,7 @@ public class AliYunFileHandler implements FileHandler {
|
||||||
BufferedImage image = ImageIO.read(file.getInputStream());
|
BufferedImage image = ImageIO.read(file.getInputStream());
|
||||||
uploadResult.setWidth(image.getWidth());
|
uploadResult.setWidth(image.getWidth());
|
||||||
uploadResult.setHeight(image.getHeight());
|
uploadResult.setHeight(image.getHeight());
|
||||||
uploadResult.setThumbPath(StringUtils.isBlank(ossThumbnailStyleRule) ? filePath : filePath + ossThumbnailStyleRule);
|
uploadResult.setThumbPath(StringUtils.isBlank(thumbnailStyleRule) ? filePath : filePath + thumbnailStyleRule);
|
||||||
}
|
}
|
||||||
|
|
||||||
return uploadResult;
|
return uploadResult;
|
||||||
|
@ -105,16 +108,16 @@ public class AliYunFileHandler implements FileHandler {
|
||||||
Assert.notNull(key, "File key must not be blank");
|
Assert.notNull(key, "File key must not be blank");
|
||||||
|
|
||||||
// Get config
|
// Get config
|
||||||
String ossEndPoint = optionService.getByPropertyOfNonNull(AliYunProperties.OSS_ENDPOINT).toString();
|
String endPoint = optionService.getByPropertyOfNonNull(AliYunProperties.OSS_ENDPOINT).toString();
|
||||||
String ossAccessKey = optionService.getByPropertyOfNonNull(AliYunProperties.OSS_ACCESS_KEY).toString();
|
String accessKey = optionService.getByPropertyOfNonNull(AliYunProperties.OSS_ACCESS_KEY).toString();
|
||||||
String ossAccessSecret = optionService.getByPropertyOfNonNull(AliYunProperties.OSS_ACCESS_SECRET).toString();
|
String accessSecret = optionService.getByPropertyOfNonNull(AliYunProperties.OSS_ACCESS_SECRET).toString();
|
||||||
String ossBucketName = optionService.getByPropertyOfNonNull(AliYunProperties.OSS_BUCKET_NAME).toString();
|
String bucketName = optionService.getByPropertyOfNonNull(AliYunProperties.OSS_BUCKET_NAME).toString();
|
||||||
|
|
||||||
// Init OSS client
|
// Init OSS client
|
||||||
OSS ossClient = new OSSClientBuilder().build(ossEndPoint, ossAccessKey, ossAccessSecret);
|
OSS ossClient = new OSSClientBuilder().build(endPoint, accessKey, accessSecret);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
ossClient.deleteObject(new DeleteObjectsRequest(ossBucketName).withKey(key));
|
ossClient.deleteObject(new DeleteObjectsRequest(bucketName).withKey(key));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new FileOperationException("附件 " + key + " 从阿里云删除失败", e);
|
throw new FileOperationException("附件 " + key + " 从阿里云删除失败", e);
|
||||||
} finally {
|
} finally {
|
||||||
|
|
|
@ -26,6 +26,7 @@ import java.util.Objects;
|
||||||
* BaiDuYun file handler.
|
* BaiDuYun file handler.
|
||||||
*
|
*
|
||||||
* @author wangya
|
* @author wangya
|
||||||
|
* @author ryanwang
|
||||||
* @date 2019-07-20
|
* @date 2019-07-20
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
|
@ -43,31 +44,34 @@ public class BaiDuYunFileHandler implements FileHandler {
|
||||||
Assert.notNull(file, "Multipart file must not be null");
|
Assert.notNull(file, "Multipart file must not be null");
|
||||||
|
|
||||||
// Get config
|
// Get config
|
||||||
String bosDomain = optionService.getByPropertyOrDefault(BaiDuYunProperties.BOS_DOMAIN, String.class, "");
|
Object protocol = optionService.getByPropertyOfNonNull(BaiDuYunProperties.BOS_PROTOCOL);
|
||||||
String bosEndPoint = optionService.getByPropertyOfNonNull(BaiDuYunProperties.BOS_ENDPOINT).toString();
|
String domain = optionService.getByPropertyOrDefault(BaiDuYunProperties.BOS_DOMAIN, String.class, "");
|
||||||
String bosAccessKey = optionService.getByPropertyOfNonNull(BaiDuYunProperties.BOS_ACCESS_KEY).toString();
|
String endPoint = optionService.getByPropertyOfNonNull(BaiDuYunProperties.BOS_ENDPOINT).toString();
|
||||||
String bosSecretKey = optionService.getByPropertyOfNonNull(BaiDuYunProperties.BOS_SECRET_KEY).toString();
|
String accessKey = optionService.getByPropertyOfNonNull(BaiDuYunProperties.BOS_ACCESS_KEY).toString();
|
||||||
String bosBucketName = optionService.getByPropertyOfNonNull(BaiDuYunProperties.BOS_BUCKET_NAME).toString();
|
String secretKey = optionService.getByPropertyOfNonNull(BaiDuYunProperties.BOS_SECRET_KEY).toString();
|
||||||
String bosStyleRule = optionService.getByPropertyOrDefault(BaiDuYunProperties.BOS_STYLE_RULE, String.class, "");
|
String bucketName = optionService.getByPropertyOfNonNull(BaiDuYunProperties.BOS_BUCKET_NAME).toString();
|
||||||
String bosThumbnailStyleRule = optionService.getByPropertyOrDefault(BaiDuYunProperties.BOS_THUMBNAIL_STYLE_RULE, String.class, "");
|
String styleRule = optionService.getByPropertyOrDefault(BaiDuYunProperties.BOS_STYLE_RULE, String.class, "");
|
||||||
String bosSource = StringUtils.join("https://", bosBucketName, "." + bosEndPoint);
|
String thumbnailStyleRule = optionService.getByPropertyOrDefault(BaiDuYunProperties.BOS_THUMBNAIL_STYLE_RULE, String.class, "");
|
||||||
|
String source = StringUtils.join(protocol, bucketName, "." + endPoint);
|
||||||
|
|
||||||
BosClientConfiguration config = new BosClientConfiguration();
|
BosClientConfiguration config = new BosClientConfiguration();
|
||||||
config.setCredentials(new DefaultBceCredentials(bosAccessKey, bosSecretKey));
|
config.setCredentials(new DefaultBceCredentials(accessKey, secretKey));
|
||||||
config.setEndpoint(bosEndPoint);
|
config.setEndpoint(endPoint);
|
||||||
|
|
||||||
// Init OSS client
|
// Init OSS client
|
||||||
BosClient client = new BosClient(config);
|
BosClient client = new BosClient(config);
|
||||||
|
|
||||||
|
domain = protocol + domain;
|
||||||
|
|
||||||
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);
|
String upFilePath = StringUtils.join(basename, "_", timestamp, ".", extension);
|
||||||
String filePath = StringUtils.join(StringUtils.appendIfMissing(StringUtils.isNotBlank(bosDomain) ? bosDomain : bosSource, "/"), upFilePath);
|
String filePath = StringUtils.join(StringUtils.appendIfMissing(StringUtils.isNotBlank(domain) ? domain : source, "/"), upFilePath);
|
||||||
|
|
||||||
// Upload
|
// Upload
|
||||||
PutObjectResponse putObjectResponseFromInputStream = client.putObject(bosBucketName, upFilePath, file.getInputStream());
|
PutObjectResponse putObjectResponseFromInputStream = client.putObject(bucketName, upFilePath, file.getInputStream());
|
||||||
if (putObjectResponseFromInputStream == null) {
|
if (putObjectResponseFromInputStream == null) {
|
||||||
throw new FileOperationException("上传附件 " + file.getOriginalFilename() + " 到百度云失败 ");
|
throw new FileOperationException("上传附件 " + file.getOriginalFilename() + " 到百度云失败 ");
|
||||||
}
|
}
|
||||||
|
@ -75,7 +79,7 @@ public class BaiDuYunFileHandler implements FileHandler {
|
||||||
// Response result
|
// Response result
|
||||||
UploadResult uploadResult = new UploadResult();
|
UploadResult uploadResult = new UploadResult();
|
||||||
uploadResult.setFilename(basename);
|
uploadResult.setFilename(basename);
|
||||||
uploadResult.setFilePath(StringUtils.isBlank(bosStyleRule) ? filePath : filePath + bosStyleRule);
|
uploadResult.setFilePath(StringUtils.isBlank(styleRule) ? filePath : filePath + styleRule);
|
||||||
uploadResult.setKey(upFilePath);
|
uploadResult.setKey(upFilePath);
|
||||||
uploadResult.setMediaType(MediaType.valueOf(Objects.requireNonNull(file.getContentType())));
|
uploadResult.setMediaType(MediaType.valueOf(Objects.requireNonNull(file.getContentType())));
|
||||||
uploadResult.setSuffix(extension);
|
uploadResult.setSuffix(extension);
|
||||||
|
@ -86,7 +90,7 @@ public class BaiDuYunFileHandler implements FileHandler {
|
||||||
BufferedImage image = ImageIO.read(file.getInputStream());
|
BufferedImage image = ImageIO.read(file.getInputStream());
|
||||||
uploadResult.setWidth(image.getWidth());
|
uploadResult.setWidth(image.getWidth());
|
||||||
uploadResult.setHeight(image.getHeight());
|
uploadResult.setHeight(image.getHeight());
|
||||||
uploadResult.setThumbPath(StringUtils.isBlank(bosThumbnailStyleRule) ? filePath : filePath + bosThumbnailStyleRule);
|
uploadResult.setThumbPath(StringUtils.isBlank(thumbnailStyleRule) ? filePath : filePath + thumbnailStyleRule);
|
||||||
}
|
}
|
||||||
|
|
||||||
return uploadResult;
|
return uploadResult;
|
||||||
|
@ -102,20 +106,20 @@ public class BaiDuYunFileHandler implements FileHandler {
|
||||||
Assert.notNull(key, "File key must not be blank");
|
Assert.notNull(key, "File key must not be blank");
|
||||||
|
|
||||||
// Get config
|
// Get config
|
||||||
String bosEndPoint = optionService.getByPropertyOfNonNull(BaiDuYunProperties.BOS_ENDPOINT).toString();
|
String endPoint = optionService.getByPropertyOfNonNull(BaiDuYunProperties.BOS_ENDPOINT).toString();
|
||||||
String bosAccessKey = optionService.getByPropertyOfNonNull(BaiDuYunProperties.BOS_ACCESS_KEY).toString();
|
String accessKey = optionService.getByPropertyOfNonNull(BaiDuYunProperties.BOS_ACCESS_KEY).toString();
|
||||||
String bosSecretKey = optionService.getByPropertyOfNonNull(BaiDuYunProperties.BOS_SECRET_KEY).toString();
|
String secretKey = optionService.getByPropertyOfNonNull(BaiDuYunProperties.BOS_SECRET_KEY).toString();
|
||||||
String bosBucketName = optionService.getByPropertyOfNonNull(BaiDuYunProperties.BOS_BUCKET_NAME).toString();
|
String bucketName = optionService.getByPropertyOfNonNull(BaiDuYunProperties.BOS_BUCKET_NAME).toString();
|
||||||
|
|
||||||
BosClientConfiguration config = new BosClientConfiguration();
|
BosClientConfiguration config = new BosClientConfiguration();
|
||||||
config.setCredentials(new DefaultBceCredentials(bosAccessKey, bosSecretKey));
|
config.setCredentials(new DefaultBceCredentials(accessKey, secretKey));
|
||||||
config.setEndpoint(bosEndPoint);
|
config.setEndpoint(endPoint);
|
||||||
|
|
||||||
// Init OSS client
|
// Init OSS client
|
||||||
BosClient client = new BosClient(config);
|
BosClient client = new BosClient(config);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
client.deleteObject(bosBucketName, key);
|
client.deleteObject(bucketName, key);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new FileOperationException("附件 " + key + " 从百度云删除失败", e);
|
throw new FileOperationException("附件 " + key + " 从百度云删除失败", e);
|
||||||
} finally {
|
} finally {
|
||||||
|
|
|
@ -36,7 +36,7 @@ import static run.halo.app.handler.file.FileHandler.isImageType;
|
||||||
*
|
*
|
||||||
* @author johnniang
|
* @author johnniang
|
||||||
* @author ryanwang
|
* @author ryanwang
|
||||||
* @date 3/27/19
|
* @date 2019-03-27
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Component
|
@Component
|
||||||
|
@ -57,6 +57,7 @@ public class QnYunFileHandler implements FileHandler {
|
||||||
String accessKey = optionService.getByPropertyOfNonNull(QnYunProperties.OSS_ACCESS_KEY).toString();
|
String accessKey = optionService.getByPropertyOfNonNull(QnYunProperties.OSS_ACCESS_KEY).toString();
|
||||||
String secretKey = optionService.getByPropertyOfNonNull(QnYunProperties.OSS_SECRET_KEY).toString();
|
String secretKey = optionService.getByPropertyOfNonNull(QnYunProperties.OSS_SECRET_KEY).toString();
|
||||||
String bucket = optionService.getByPropertyOfNonNull(QnYunProperties.OSS_BUCKET).toString();
|
String bucket = optionService.getByPropertyOfNonNull(QnYunProperties.OSS_BUCKET).toString();
|
||||||
|
String protocol = optionService.getByPropertyOfNonNull(QnYunProperties.OSS_PROTOCOL).toString();
|
||||||
String domain = optionService.getByPropertyOfNonNull(QnYunProperties.OSS_DOMAIN).toString();
|
String domain = optionService.getByPropertyOfNonNull(QnYunProperties.OSS_DOMAIN).toString();
|
||||||
String styleRule = optionService.getByPropertyOrDefault(QnYunProperties.OSS_STYLE_RULE, String.class, "");
|
String styleRule = optionService.getByPropertyOrDefault(QnYunProperties.OSS_STYLE_RULE, String.class, "");
|
||||||
String thumbnailStyleRule = optionService.getByPropertyOrDefault(QnYunProperties.OSS_THUMBNAIL_STYLE_RULE, String.class, "");
|
String thumbnailStyleRule = optionService.getByPropertyOrDefault(QnYunProperties.OSS_THUMBNAIL_STYLE_RULE, String.class, "");
|
||||||
|
@ -99,7 +100,7 @@ public class QnYunFileHandler 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 = StringUtils.appendIfMissing(domain, "/") + putSet.getHash();
|
String filePath = protocol + StringUtils.appendIfMissing(domain, "/") + putSet.getHash();
|
||||||
|
|
||||||
// Build upload result
|
// Build upload result
|
||||||
UploadResult result = new UploadResult();
|
UploadResult result = new UploadResult();
|
||||||
|
|
|
@ -47,32 +47,34 @@ public class TencentYunFileHandler implements FileHandler {
|
||||||
Assert.notNull(file, "Multipart file must not be null");
|
Assert.notNull(file, "Multipart file must not be null");
|
||||||
|
|
||||||
// Get config
|
// Get config
|
||||||
String cosDomain = optionService.getByPropertyOrDefault(TencentYunProperties.COS_DOMAIN, String.class, "");
|
String protocol = optionService.getByPropertyOfNonNull(TencentYunProperties.COS_PROTOCOL).toString();
|
||||||
String cosRegion = optionService.getByPropertyOfNonNull(TencentYunProperties.COS_REGION).toString();
|
String domain = optionService.getByPropertyOrDefault(TencentYunProperties.COS_DOMAIN, String.class, "");
|
||||||
String cosSecretId = optionService.getByPropertyOfNonNull(TencentYunProperties.COS_SECRET_ID).toString();
|
String region = optionService.getByPropertyOfNonNull(TencentYunProperties.COS_REGION).toString();
|
||||||
String cosSecretKey = optionService.getByPropertyOfNonNull(TencentYunProperties.COS_SECRET_KEY).toString();
|
String secretId = optionService.getByPropertyOfNonNull(TencentYunProperties.COS_SECRET_ID).toString();
|
||||||
String cosBucketName = optionService.getByPropertyOfNonNull(TencentYunProperties.COS_BUCKET_NAME).toString();
|
String secretKey = optionService.getByPropertyOfNonNull(TencentYunProperties.COS_SECRET_KEY).toString();
|
||||||
String cosSource = StringUtils.join("https://", cosBucketName, ".cos." + cosRegion + ".myqcloud.com");
|
String bucketName = optionService.getByPropertyOfNonNull(TencentYunProperties.COS_BUCKET_NAME).toString();
|
||||||
|
String source = StringUtils.join(protocol, bucketName, ".cos." + region + ".myqcloud.com");
|
||||||
|
|
||||||
//get file attribute
|
//get file attribute
|
||||||
long size = file.getSize();
|
long size = file.getSize();
|
||||||
String contentType = file.getContentType();
|
String contentType = file.getContentType();
|
||||||
|
|
||||||
COSCredentials cred = new BasicCOSCredentials(cosSecretId, cosSecretKey);
|
COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
|
||||||
Region region = new Region(cosRegion);
|
Region regionConfig = new Region(region);
|
||||||
ClientConfig clientConfig = new ClientConfig(region);
|
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;
|
||||||
|
|
||||||
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);
|
String upFilePath = StringUtils.join(basename, "_", timestamp, ".", extension);
|
||||||
String filePath = StringUtils.join(StringUtils.appendIfMissing(StringUtils.isNotBlank(cosDomain) ? cosDomain : cosSource, "/"), upFilePath);
|
String filePath = StringUtils.join(StringUtils.appendIfMissing(StringUtils.isNotBlank(domain) ? domain : source, "/"), upFilePath);
|
||||||
|
|
||||||
// Upload
|
// Upload
|
||||||
ObjectMetadata objectMetadata = new ObjectMetadata();
|
ObjectMetadata objectMetadata = new ObjectMetadata();
|
||||||
|
@ -80,7 +82,7 @@ public class TencentYunFileHandler implements FileHandler {
|
||||||
objectMetadata.setContentLength(size);
|
objectMetadata.setContentLength(size);
|
||||||
// 设置 Content type, 默认是 application/octet-stream
|
// 设置 Content type, 默认是 application/octet-stream
|
||||||
objectMetadata.setContentType(contentType);
|
objectMetadata.setContentType(contentType);
|
||||||
PutObjectResult putObjectResponseFromInputStream = cosClient.putObject(cosBucketName, upFilePath, file.getInputStream(), objectMetadata);
|
PutObjectResult putObjectResponseFromInputStream = cosClient.putObject(bucketName, upFilePath, file.getInputStream(), objectMetadata);
|
||||||
if (putObjectResponseFromInputStream == null) {
|
if (putObjectResponseFromInputStream == null) {
|
||||||
throw new FileOperationException("上传附件 " + file.getOriginalFilename() + " 到腾讯云失败 ");
|
throw new FileOperationException("上传附件 " + file.getOriginalFilename() + " 到腾讯云失败 ");
|
||||||
}
|
}
|
||||||
|
@ -115,20 +117,20 @@ public class TencentYunFileHandler implements FileHandler {
|
||||||
Assert.notNull(key, "File key must not be blank");
|
Assert.notNull(key, "File key must not be blank");
|
||||||
|
|
||||||
// Get config
|
// Get config
|
||||||
String cosRegion = optionService.getByPropertyOfNonNull(TencentYunProperties.COS_REGION).toString();
|
String region = optionService.getByPropertyOfNonNull(TencentYunProperties.COS_REGION).toString();
|
||||||
String cosSecretId = optionService.getByPropertyOfNonNull(TencentYunProperties.COS_SECRET_ID).toString();
|
String secretId = optionService.getByPropertyOfNonNull(TencentYunProperties.COS_SECRET_ID).toString();
|
||||||
String cosSecretKey = optionService.getByPropertyOfNonNull(TencentYunProperties.COS_SECRET_KEY).toString();
|
String secretKey = optionService.getByPropertyOfNonNull(TencentYunProperties.COS_SECRET_KEY).toString();
|
||||||
String cosBucketName = optionService.getByPropertyOfNonNull(TencentYunProperties.COS_BUCKET_NAME).toString();
|
String bucketName = optionService.getByPropertyOfNonNull(TencentYunProperties.COS_BUCKET_NAME).toString();
|
||||||
|
|
||||||
COSCredentials cred = new BasicCOSCredentials(cosSecretId, cosSecretKey);
|
COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
|
||||||
Region region = new Region(cosRegion);
|
Region regionConfig = new Region(region);
|
||||||
ClientConfig clientConfig = new ClientConfig(region);
|
ClientConfig clientConfig = new ClientConfig(regionConfig);
|
||||||
|
|
||||||
// Init OSS client
|
// Init OSS client
|
||||||
COSClient cosClient = new COSClient(cred, clientConfig);
|
COSClient cosClient = new COSClient(cred, clientConfig);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
cosClient.deleteObject(cosBucketName, key);
|
cosClient.deleteObject(bucketName, key);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new FileOperationException("附件 " + key + " 从腾讯云删除失败", e);
|
throw new FileOperationException("附件 " + key + " 从腾讯云删除失败", e);
|
||||||
} finally {
|
} finally {
|
||||||
|
|
|
@ -24,7 +24,7 @@ import java.util.Objects;
|
||||||
*
|
*
|
||||||
* @author johnniang
|
* @author johnniang
|
||||||
* @author ryanwang
|
* @author ryanwang
|
||||||
* @date 3/27/19
|
* @date 2019-03-27
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Component
|
@Component
|
||||||
|
@ -40,17 +40,18 @@ public class UpYunFileHandler implements FileHandler {
|
||||||
public UploadResult upload(MultipartFile file) {
|
public UploadResult upload(MultipartFile file) {
|
||||||
Assert.notNull(file, "Multipart file must not be null");
|
Assert.notNull(file, "Multipart file must not be null");
|
||||||
|
|
||||||
String ossSource = optionService.getByPropertyOfNonNull(UpYunProperties.OSS_SOURCE).toString();
|
String source = optionService.getByPropertyOfNonNull(UpYunProperties.OSS_SOURCE).toString();
|
||||||
String ossPassword = optionService.getByPropertyOfNonNull(UpYunProperties.OSS_PASSWORD).toString();
|
String password = optionService.getByPropertyOfNonNull(UpYunProperties.OSS_PASSWORD).toString();
|
||||||
String ossBucket = optionService.getByPropertyOfNonNull(UpYunProperties.OSS_BUCKET).toString();
|
String bucket = optionService.getByPropertyOfNonNull(UpYunProperties.OSS_BUCKET).toString();
|
||||||
String ossDomain = optionService.getByPropertyOfNonNull(UpYunProperties.OSS_DOMAIN).toString();
|
String protocol = optionService.getByPropertyOfNonNull(UpYunProperties.OSS_PROTOCOL).toString();
|
||||||
String ossOperator = optionService.getByPropertyOfNonNull(UpYunProperties.OSS_OPERATOR).toString();
|
String domain = optionService.getByPropertyOfNonNull(UpYunProperties.OSS_DOMAIN).toString();
|
||||||
|
String operator = optionService.getByPropertyOfNonNull(UpYunProperties.OSS_OPERATOR).toString();
|
||||||
// style rule can be null
|
// style rule can be null
|
||||||
String ossStyleRule = optionService.getByPropertyOrDefault(UpYunProperties.OSS_STYLE_RULE, String.class, "");
|
String styleRule = optionService.getByPropertyOrDefault(UpYunProperties.OSS_STYLE_RULE, String.class, "");
|
||||||
String ossThumbnailStyleRule = optionService.getByPropertyOrDefault(UpYunProperties.OSS_THUMBNAIL_STYLE_RULE, String.class, "");
|
String thumbnailStyleRule = optionService.getByPropertyOrDefault(UpYunProperties.OSS_THUMBNAIL_STYLE_RULE, String.class, "");
|
||||||
|
|
||||||
// Create up yun
|
// Create up yun
|
||||||
UpYun upYun = new UpYun(ossBucket, ossOperator, ossPassword);
|
UpYun upYun = new UpYun(bucket, operator, password);
|
||||||
upYun.setDebug(log.isDebugEnabled());
|
upYun.setDebug(log.isDebugEnabled());
|
||||||
upYun.setTimeout(60);
|
upYun.setTimeout(60);
|
||||||
upYun.setApiDomain(UpYun.ED_AUTO);
|
upYun.setApiDomain(UpYun.ED_AUTO);
|
||||||
|
@ -63,7 +64,7 @@ public class UpYunFileHandler implements FileHandler {
|
||||||
// Get md5 value of the file
|
// Get md5 value of the file
|
||||||
String md5OfFile = DigestUtils.md5DigestAsHex(file.getInputStream());
|
String md5OfFile = DigestUtils.md5DigestAsHex(file.getInputStream());
|
||||||
// Build file path
|
// Build file path
|
||||||
String upFilePath = StringUtils.appendIfMissing(ossSource, "/") + md5OfFile + '.' + extension;
|
String upFilePath = StringUtils.appendIfMissing(source, "/") + md5OfFile + '.' + extension;
|
||||||
// Set md5Content
|
// Set md5Content
|
||||||
upYun.setContentMD5(md5OfFile);
|
upYun.setContentMD5(md5OfFile);
|
||||||
// Write file
|
// Write file
|
||||||
|
@ -72,12 +73,12 @@ public class UpYunFileHandler implements FileHandler {
|
||||||
throw new FileOperationException("上传附件 " + file.getOriginalFilename() + " 到又拍云失败" + upFilePath);
|
throw new FileOperationException("上传附件 " + file.getOriginalFilename() + " 到又拍云失败" + upFilePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
String filePath = StringUtils.removeEnd(ossDomain, "/") + upFilePath;
|
String filePath = protocol + StringUtils.removeEnd(domain, "/") + upFilePath;
|
||||||
|
|
||||||
// Build upload result
|
// Build upload result
|
||||||
UploadResult uploadResult = new UploadResult();
|
UploadResult uploadResult = new UploadResult();
|
||||||
uploadResult.setFilename(basename);
|
uploadResult.setFilename(basename);
|
||||||
uploadResult.setFilePath(StringUtils.isBlank(ossStyleRule) ? filePath : filePath + ossStyleRule);
|
uploadResult.setFilePath(StringUtils.isBlank(styleRule) ? filePath : filePath + styleRule);
|
||||||
uploadResult.setKey(upFilePath);
|
uploadResult.setKey(upFilePath);
|
||||||
uploadResult.setMediaType(MediaType.valueOf(Objects.requireNonNull(file.getContentType())));
|
uploadResult.setMediaType(MediaType.valueOf(Objects.requireNonNull(file.getContentType())));
|
||||||
uploadResult.setSuffix(extension);
|
uploadResult.setSuffix(extension);
|
||||||
|
@ -88,7 +89,7 @@ public class UpYunFileHandler implements FileHandler {
|
||||||
BufferedImage image = ImageIO.read(file.getInputStream());
|
BufferedImage image = ImageIO.read(file.getInputStream());
|
||||||
uploadResult.setWidth(image.getWidth());
|
uploadResult.setWidth(image.getWidth());
|
||||||
uploadResult.setHeight(image.getHeight());
|
uploadResult.setHeight(image.getHeight());
|
||||||
uploadResult.setThumbPath(StringUtils.isBlank(ossThumbnailStyleRule) ? filePath : filePath + ossThumbnailStyleRule);
|
uploadResult.setThumbPath(StringUtils.isBlank(thumbnailStyleRule) ? filePath : filePath + thumbnailStyleRule);
|
||||||
}
|
}
|
||||||
|
|
||||||
return uploadResult;
|
return uploadResult;
|
||||||
|
@ -102,12 +103,12 @@ public class UpYunFileHandler implements FileHandler {
|
||||||
Assert.notNull(key, "File key must not be blank");
|
Assert.notNull(key, "File key must not be blank");
|
||||||
|
|
||||||
// Get config
|
// Get config
|
||||||
String ossPassword = optionService.getByPropertyOfNonNull(UpYunProperties.OSS_PASSWORD).toString();
|
String password = optionService.getByPropertyOfNonNull(UpYunProperties.OSS_PASSWORD).toString();
|
||||||
String ossBucket = optionService.getByPropertyOfNonNull(UpYunProperties.OSS_BUCKET).toString();
|
String bucket = optionService.getByPropertyOfNonNull(UpYunProperties.OSS_BUCKET).toString();
|
||||||
String ossOperator = optionService.getByPropertyOfNonNull(UpYunProperties.OSS_OPERATOR).toString();
|
String operator = optionService.getByPropertyOfNonNull(UpYunProperties.OSS_OPERATOR).toString();
|
||||||
|
|
||||||
// Create up yun
|
// Create up yun
|
||||||
UpYun upYun = new UpYun(ossBucket, ossOperator, ossPassword);
|
UpYun upYun = new UpYun(bucket, operator, password);
|
||||||
// Set api domain with ED_AUTO
|
// Set api domain with ED_AUTO
|
||||||
upYun.setApiDomain(UpYun.ED_AUTO);
|
upYun.setApiDomain(UpYun.ED_AUTO);
|
||||||
|
|
||||||
|
|
|
@ -1,18 +1,23 @@
|
||||||
package run.halo.app.model.properties;
|
package run.halo.app.model.properties;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* AliYun properties.
|
* Ali yun oss properties.
|
||||||
*
|
*
|
||||||
* @author MyFaith
|
* @author MyFaith
|
||||||
* @author ryanwang
|
* @author ryanwang
|
||||||
* @date 2019-04-04 00:00:56
|
* @date 2019-04-04
|
||||||
*/
|
*/
|
||||||
public enum AliYunProperties implements PropertyEnum {
|
public enum AliYunProperties implements PropertyEnum {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Aliyun oss domain protocol
|
||||||
|
*/
|
||||||
|
OSS_PROTOCOL("oss_aliyun_domain_protocol", String.class, "https://"),
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Aliyun oss domain
|
* Aliyun oss domain
|
||||||
*/
|
*/
|
||||||
OSS_DOMAIN("oss_aliyun_domain",String.class,""),
|
OSS_DOMAIN("oss_aliyun_domain", String.class, ""),
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Aliyun oss endpoint.
|
* Aliyun oss endpoint.
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
package run.halo.app.model.properties;
|
package run.halo.app.model.properties;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* BaiDuYun properties.
|
* Baidu yun bos properties.
|
||||||
*
|
*
|
||||||
* @author wangya
|
* @author wangya
|
||||||
* @author ryanwang
|
* @author ryanwang
|
||||||
|
@ -9,10 +9,15 @@ package run.halo.app.model.properties;
|
||||||
*/
|
*/
|
||||||
public enum BaiDuYunProperties implements PropertyEnum {
|
public enum BaiDuYunProperties implements PropertyEnum {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Baidu yun bos domain protocol.
|
||||||
|
*/
|
||||||
|
BOS_PROTOCOL("bos_baiduyun_domain_protocol", String.class, "https://"),
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Baidu yun bos domain.
|
* Baidu yun bos domain.
|
||||||
*/
|
*/
|
||||||
BOS_DOMAIN("bos_baiduyun_domain",String.class,""),
|
BOS_DOMAIN("bos_baiduyun_domain", String.class, ""),
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Baidu yun bos endpoint.
|
* Baidu yun bos endpoint.
|
||||||
|
|
|
@ -1,26 +1,52 @@
|
||||||
package run.halo.app.model.properties;
|
package run.halo.app.model.properties;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Qi niu yun properties.
|
* Qi niu yun oss properties.
|
||||||
*
|
*
|
||||||
* @author johnniang
|
* @author johnniang
|
||||||
* @author ryanwang
|
* @author ryanwang
|
||||||
* @date 3/26/19
|
* @date 2019-03-26
|
||||||
*/
|
*/
|
||||||
public enum QnYunProperties implements PropertyEnum {
|
public enum QnYunProperties implements PropertyEnum {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Qiniu yun oss zone.
|
||||||
|
*/
|
||||||
OSS_ZONE("oss_qiniu_zone", String.class, "auto"),
|
OSS_ZONE("oss_qiniu_zone", String.class, "auto"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Qiniu yun oss access key.
|
||||||
|
*/
|
||||||
OSS_ACCESS_KEY("oss_qiniu_access_key", String.class, ""),
|
OSS_ACCESS_KEY("oss_qiniu_access_key", String.class, ""),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Qiniu yun oss secret key.
|
||||||
|
*/
|
||||||
OSS_SECRET_KEY("oss_qiniu_secret_key", String.class, ""),
|
OSS_SECRET_KEY("oss_qiniu_secret_key", String.class, ""),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Qiniu yun oss domain protocol.
|
||||||
|
*/
|
||||||
|
OSS_PROTOCOL("oss_qiniu_domain_protocol", String.class, "https://"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Qiniu yun oss domain.
|
||||||
|
*/
|
||||||
OSS_DOMAIN("oss_qiniu_domain", String.class, ""),
|
OSS_DOMAIN("oss_qiniu_domain", String.class, ""),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Qiniu yun oss bucket.
|
||||||
|
*/
|
||||||
OSS_BUCKET("oss_qiniu_bucket", String.class, ""),
|
OSS_BUCKET("oss_qiniu_bucket", String.class, ""),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Qiniu yun oss style rule.
|
||||||
|
*/
|
||||||
OSS_STYLE_RULE("oss_qiniu_style_rule", String.class, ""),
|
OSS_STYLE_RULE("oss_qiniu_style_rule", String.class, ""),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Qiniu yun oss thumbnail style rule.
|
||||||
|
*/
|
||||||
OSS_THUMBNAIL_STYLE_RULE("oss_qiniu_thumbnail_style_rule", String.class, "");
|
OSS_THUMBNAIL_STYLE_RULE("oss_qiniu_thumbnail_style_rule", String.class, "");
|
||||||
|
|
||||||
private final String value;
|
private final String value;
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
package run.halo.app.model.properties;
|
package run.halo.app.model.properties;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TencentYun properties.
|
* Tencent yun cos properties.
|
||||||
*
|
*
|
||||||
* @author wangya
|
* @author wangya
|
||||||
* @author ryanwang
|
* @author ryanwang
|
||||||
|
@ -9,10 +9,15 @@ package run.halo.app.model.properties;
|
||||||
*/
|
*/
|
||||||
public enum TencentYunProperties implements PropertyEnum {
|
public enum TencentYunProperties implements PropertyEnum {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tencentyun cos domain protocol.
|
||||||
|
*/
|
||||||
|
COS_PROTOCOL("cos_tencentyun_domain_protocol", String.class, "https://"),
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tencentyun cos domain.
|
* Tencentyun cos domain.
|
||||||
*/
|
*/
|
||||||
COS_DOMAIN("cos_tencentyun_domain",String.class,""),
|
COS_DOMAIN("cos_tencentyun_domain", String.class, ""),
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tencentyun cos endpoint.
|
* Tencentyun cos endpoint.
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
package run.halo.app.model.properties;
|
package run.halo.app.model.properties;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* You pai yun properties.
|
* You pai yun oss properties.
|
||||||
*
|
*
|
||||||
* @author johnniang
|
* @author johnniang
|
||||||
* @author ryanwang
|
* @author ryanwang
|
||||||
* @date 3/27/19
|
* @date 2019-03-27
|
||||||
*/
|
*/
|
||||||
public enum UpYunProperties implements PropertyEnum {
|
public enum UpYunProperties implements PropertyEnum {
|
||||||
|
|
||||||
|
@ -24,6 +24,11 @@ public enum UpYunProperties implements PropertyEnum {
|
||||||
*/
|
*/
|
||||||
OSS_BUCKET("oss_upyun_bucket", String.class, ""),
|
OSS_BUCKET("oss_upyun_bucket", String.class, ""),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* upyun oss domain protocol
|
||||||
|
*/
|
||||||
|
OSS_PROTOCOL("oss_upyun_domain_protocol",String.class,"https://"),
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* upyun oss domain
|
* upyun oss domain
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in New Issue