Fixed file upload handler.

pull/296/head
ruibaby 2019-08-28 10:46:28 +08:00
parent f62f3f5c4a
commit 7f3177eddc
9 changed files with 85 additions and 78 deletions

View File

@ -25,6 +25,7 @@ import java.util.Objects;
* AliYun file handler.
*
* @author MyFaith
* @author ryanwang
* @date 2019-04-04 00:06:13
*/
@Slf4j
@ -46,8 +47,9 @@ public class AliYunFileHandler implements FileHandler {
String ossAccessKey = optionService.getByPropertyOfNonNull(AliYunProperties.OSS_ACCESS_KEY).toString();
String ossAccessSecret = optionService.getByPropertyOfNonNull(AliYunProperties.OSS_ACCESS_SECRET).toString();
String ossBucketName = optionService.getByPropertyOfNonNull(AliYunProperties.OSS_BUCKET_NAME).toString();
String ossStyleRule = optionService.getByPropertyOfNonNull(AliYunProperties.OSS_STYLE_RULE).toString();
String ossSource = StringUtils.join("https://", ossBucketName, "." + ossEndPoint);
String ossStyleRule = optionService.getByPropertyOrDefault(AliYunProperties.OSS_STYLE_RULE,String.class,"");
String ossThumbnailStyleRule = optionService.getByPropertyOrDefault(AliYunProperties.OSS_THUMBNAIL_STYLE_RULE,String.class,"");
// Init OSS client
OSS ossClient = new OSSClientBuilder().build(ossEndPoint, ossAccessKey, ossAccessSecret);
@ -68,7 +70,7 @@ public class AliYunFileHandler implements FileHandler {
// Response result
UploadResult uploadResult = new UploadResult();
uploadResult.setFilename(basename);
uploadResult.setFilePath(filePath);
uploadResult.setFilePath(StringUtils.isBlank(ossStyleRule) ? filePath : filePath + ossStyleRule);
uploadResult.setKey(upFilePath);
uploadResult.setMediaType(MediaType.valueOf(Objects.requireNonNull(file.getContentType())));
uploadResult.setSuffix(extension);
@ -79,7 +81,7 @@ public class AliYunFileHandler implements FileHandler {
BufferedImage image = ImageIO.read(file.getInputStream());
uploadResult.setWidth(image.getWidth());
uploadResult.setHeight(image.getHeight());
uploadResult.setThumbPath(StringUtils.isBlank(ossStyleRule) ? filePath : filePath + ossStyleRule);
uploadResult.setThumbPath(StringUtils.isBlank(ossThumbnailStyleRule) ? filePath : filePath + ossThumbnailStyleRule);
}
return uploadResult;
@ -106,7 +108,6 @@ public class AliYunFileHandler implements FileHandler {
String ossAccessKey = optionService.getByPropertyOfNonNull(AliYunProperties.OSS_ACCESS_KEY).toString();
String ossAccessSecret = optionService.getByPropertyOfNonNull(AliYunProperties.OSS_ACCESS_SECRET).toString();
String ossBucketName = optionService.getByPropertyOfNonNull(AliYunProperties.OSS_BUCKET_NAME).toString();
String ossSource = StringUtils.join("https://", ossBucketName, "." + ossEndPoint);
// Init OSS client
OSS ossClient = new OSSClientBuilder().build(ossEndPoint, ossAccessKey, ossAccessSecret);

View File

@ -43,17 +43,18 @@ public class BaiDuYunFileHandler implements FileHandler {
Assert.notNull(file, "Multipart file must not be null");
// Get config
String ossEndPoint = optionService.getByPropertyOfNonNull(BaiDuYunProperties.OSS_ENDPOINT).toString();
String ossAccessKey = optionService.getByPropertyOfNonNull(BaiDuYunProperties.OSS_ACCESS_KEY).toString();
String ossAccessSecret = optionService.getByPropertyOfNonNull(BaiDuYunProperties.OSS_ACCESS_SECRET).toString();
String ossBucketName = optionService.getByPropertyOfNonNull(BaiDuYunProperties.OSS_BUCKET_NAME).toString();
String ossStyleRule = optionService.getByPropertyOfNonNull(BaiDuYunProperties.OSS_STYLE_RULE).toString();
String ossSource = StringUtils.join("https://", ossBucketName, "." + ossEndPoint);
String bosEndPoint = optionService.getByPropertyOfNonNull(BaiDuYunProperties.BOS_ENDPOINT).toString();
String bosAccessKey = optionService.getByPropertyOfNonNull(BaiDuYunProperties.BOS_ACCESS_KEY).toString();
String bosSecretKey = optionService.getByPropertyOfNonNull(BaiDuYunProperties.BOS_SECRET_KEY).toString();
String bosBucketName = optionService.getByPropertyOfNonNull(BaiDuYunProperties.BOS_BUCKET_NAME).toString();
String bosStyleRule = optionService.getByPropertyOrDefault(BaiDuYunProperties.BOS_STYLE_RULE, String.class, "");
String bosThumbnailStyleRule = optionService.getByPropertyOrDefault(BaiDuYunProperties.BOS_STYLE_RULE, String.class, "");
String bosSource = StringUtils.join("https://", bosBucketName, "." + bosEndPoint);
BosClientConfiguration config = new BosClientConfiguration();
config.setCredentials(new DefaultBceCredentials(ossAccessKey, ossAccessSecret));
config.setEndpoint(ossEndPoint);
config.setCredentials(new DefaultBceCredentials(bosAccessKey, bosSecretKey));
config.setEndpoint(bosEndPoint);
// Init OSS client
BosClient client = new BosClient(config);
@ -63,10 +64,10 @@ public class BaiDuYunFileHandler implements FileHandler {
String extension = FilenameUtils.getExtension(file.getOriginalFilename());
String timestamp = String.valueOf(System.currentTimeMillis());
String upFilePath = StringUtils.join(basename, "_", timestamp, ".", extension);
String filePath = StringUtils.join(StringUtils.appendIfMissing(ossSource, "/"), upFilePath);
String filePath = StringUtils.join(StringUtils.appendIfMissing(bosSource, "/"), upFilePath);
// Upload
PutObjectResponse putObjectResponseFromInputStream = client.putObject(ossBucketName, upFilePath, file.getInputStream());
PutObjectResponse putObjectResponseFromInputStream = client.putObject(bosBucketName, upFilePath, file.getInputStream());
if (putObjectResponseFromInputStream == null) {
throw new FileOperationException("上传附件 " + file.getOriginalFilename() + " 到百度云失败 ");
}
@ -74,7 +75,7 @@ public class BaiDuYunFileHandler implements FileHandler {
// Response result
UploadResult uploadResult = new UploadResult();
uploadResult.setFilename(basename);
uploadResult.setFilePath(filePath);
uploadResult.setFilePath(StringUtils.isBlank(bosStyleRule) ? filePath : filePath + bosStyleRule);
uploadResult.setKey(upFilePath);
uploadResult.setMediaType(MediaType.valueOf(Objects.requireNonNull(file.getContentType())));
uploadResult.setSuffix(extension);
@ -85,7 +86,7 @@ public class BaiDuYunFileHandler implements FileHandler {
BufferedImage image = ImageIO.read(file.getInputStream());
uploadResult.setWidth(image.getWidth());
uploadResult.setHeight(image.getHeight());
uploadResult.setThumbPath(StringUtils.isBlank(ossStyleRule) ? filePath : filePath + ossStyleRule);
uploadResult.setThumbPath(StringUtils.isBlank(bosThumbnailStyleRule) ? filePath : filePath + bosThumbnailStyleRule);
}
return uploadResult;
@ -101,21 +102,20 @@ public class BaiDuYunFileHandler implements FileHandler {
Assert.notNull(key, "File key must not be blank");
// Get config
String ossEndPoint = optionService.getByPropertyOfNonNull(BaiDuYunProperties.OSS_ENDPOINT).toString();
String ossAccessKey = optionService.getByPropertyOfNonNull(BaiDuYunProperties.OSS_ACCESS_KEY).toString();
String ossAccessSecret = optionService.getByPropertyOfNonNull(BaiDuYunProperties.OSS_ACCESS_SECRET).toString();
String ossBucketName = optionService.getByPropertyOfNonNull(BaiDuYunProperties.OSS_BUCKET_NAME).toString();
String ossSource = StringUtils.join("https://", ossBucketName, "." + ossEndPoint);
String bosEndPoint = optionService.getByPropertyOfNonNull(BaiDuYunProperties.BOS_ENDPOINT).toString();
String bosAccessKey = optionService.getByPropertyOfNonNull(BaiDuYunProperties.BOS_ACCESS_KEY).toString();
String bosSecretKey = optionService.getByPropertyOfNonNull(BaiDuYunProperties.BOS_SECRET_KEY).toString();
String bosBucketName = optionService.getByPropertyOfNonNull(BaiDuYunProperties.BOS_BUCKET_NAME).toString();
BosClientConfiguration config = new BosClientConfiguration();
config.setCredentials(new DefaultBceCredentials(ossAccessKey, ossAccessSecret));
config.setEndpoint(ossEndPoint);
config.setCredentials(new DefaultBceCredentials(bosAccessKey, bosSecretKey));
config.setEndpoint(bosEndPoint);
// Init OSS client
BosClient client = new BosClient(config);
try {
client.deleteObject(ossBucketName, key);
client.deleteObject(bosBucketName, key);
} catch (Exception e) {
throw new FileOperationException("附件 " + key + " 从百度云删除失败", e);
} finally {

View File

@ -35,6 +35,7 @@ import static run.halo.app.handler.file.FileHandler.isImageType;
* Qi niu yun file handler.
*
* @author johnniang
* @author ryanwang
* @date 3/27/19
*/
@Slf4j
@ -58,6 +59,7 @@ public class QnYunFileHandler implements FileHandler {
String bucket = optionService.getByPropertyOfNonNull(QnYunProperties.OSS_BUCKET).toString();
String domain = optionService.getByPropertyOfNonNull(QnYunProperties.OSS_DOMAIN).toString();
String styleRule = optionService.getByPropertyOrDefault(QnYunProperties.OSS_STYLE_RULE, String.class, "");
String thumbnailStyleRule = optionService.getByPropertyOrDefault(QnYunProperties.OSS_THUMBNAIL_STYLE_RULE, String.class, "");
// Create configuration
Configuration configuration = new Configuration(zone);
@ -103,7 +105,7 @@ public class QnYunFileHandler implements FileHandler {
// Build upload result
UploadResult result = new UploadResult();
result.setFilename(basename);
result.setFilePath(filePath);
result.setFilePath(StringUtils.isBlank(styleRule) ? filePath : filePath + styleRule);
result.setKey(putSet.getKey());
result.setSuffix(extension);
result.setWidth(putSet.getWidth());
@ -112,7 +114,7 @@ public class QnYunFileHandler implements FileHandler {
result.setSize(file.getSize());
if (isImageType(result.getMediaType())) {
result.setThumbPath(StringUtils.isBlank(styleRule) ? filePath : filePath + styleRule);
result.setThumbPath(StringUtils.isBlank(thumbnailStyleRule) ? filePath : filePath + thumbnailStyleRule);
}
return result;

View File

@ -1,7 +1,6 @@
package run.halo.app.handler.file;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
@ -30,6 +29,7 @@ import java.util.Objects;
* TencentYun file handler.
*
* @author wangya
* @author ryanwang
* @date 2019-07-25
*/
@Slf4j
@ -47,21 +47,18 @@ public class TencentYunFileHandler implements FileHandler {
Assert.notNull(file, "Multipart file must not be null");
// Get config
String ossRegion = optionService.getByPropertyOfNonNull(TencentYunProperties.OSS_REGION).toString();
String ossAccessKey = optionService.getByPropertyOfNonNull(TencentYunProperties.OSS_ACCESS_KEY).toString();
String ossAccessSecret = optionService.getByPropertyOfNonNull(TencentYunProperties.OSS_ACCESS_SECRET).toString();
String ossBucketName = optionService.getByPropertyOfNonNull(TencentYunProperties.OSS_BUCKET_NAME).toString();
String ossStyleRule = optionService.getByPropertyOfNonNull(TencentYunProperties.OSS_STYLE_RULE).toString();
String ossSource = StringUtils.join("https://", ossBucketName, ".cos." + ossRegion+".myqcloud.com");
String cosRegion = optionService.getByPropertyOfNonNull(TencentYunProperties.COS_REGION).toString();
String cosSecretId = optionService.getByPropertyOfNonNull(TencentYunProperties.COS_SECRET_ID).toString();
String cosSecretKey = optionService.getByPropertyOfNonNull(TencentYunProperties.COS_SECRET_KEY).toString();
String cosBucketName = optionService.getByPropertyOfNonNull(TencentYunProperties.COS_BUCKET_NAME).toString();
String cosSource = StringUtils.join("https://", cosBucketName, ".cos." + cosRegion + ".myqcloud.com");
//get file attribute
long size = file.getSize();
String contentType = file.getContentType();
COSCredentials cred = new BasicCOSCredentials(ossAccessKey, ossAccessSecret);
Region region = new Region(ossRegion);
COSCredentials cred = new BasicCOSCredentials(cosSecretId, cosSecretKey);
Region region = new Region(cosRegion);
ClientConfig clientConfig = new ClientConfig(region);
@ -69,13 +66,12 @@ public class TencentYunFileHandler implements FileHandler {
COSClient cosClient = new COSClient(cred, clientConfig);
try {
String basename = FilenameUtils.getBasename(file.getOriginalFilename());
String extension = FilenameUtils.getExtension(file.getOriginalFilename());
String timestamp = String.valueOf(System.currentTimeMillis());
String upFilePath = StringUtils.join(basename, "_", timestamp, ".", extension);
String filePath = StringUtils.join(StringUtils.appendIfMissing(ossSource, "/"), upFilePath);
String filePath = StringUtils.join(StringUtils.appendIfMissing(cosSource, "/"), upFilePath);
// Upload
ObjectMetadata objectMetadata = new ObjectMetadata();
@ -83,7 +79,7 @@ public class TencentYunFileHandler implements FileHandler {
objectMetadata.setContentLength(size);
// 设置 Content type, 默认是 application/octet-stream
objectMetadata.setContentType(contentType);
PutObjectResult putObjectResponseFromInputStream = cosClient.putObject(ossBucketName, upFilePath, file.getInputStream(),objectMetadata);
PutObjectResult putObjectResponseFromInputStream = cosClient.putObject(cosBucketName, upFilePath, file.getInputStream(), objectMetadata);
if (putObjectResponseFromInputStream == null) {
throw new FileOperationException("上传附件 " + file.getOriginalFilename() + " 到腾讯云失败 ");
}
@ -102,7 +98,7 @@ public class TencentYunFileHandler implements FileHandler {
BufferedImage image = ImageIO.read(file.getInputStream());
uploadResult.setWidth(image.getWidth());
uploadResult.setHeight(image.getHeight());
uploadResult.setThumbPath(StringUtils.isBlank(ossStyleRule) ? filePath : filePath + ossStyleRule);
uploadResult.setThumbPath(filePath);
}
return uploadResult;
@ -118,22 +114,20 @@ public class TencentYunFileHandler implements FileHandler {
Assert.notNull(key, "File key must not be blank");
// Get config
String ossRegion = optionService.getByPropertyOfNonNull(TencentYunProperties.OSS_REGION).toString();
String ossAccessKey = optionService.getByPropertyOfNonNull(TencentYunProperties.OSS_ACCESS_KEY).toString();
String ossAccessSecret = optionService.getByPropertyOfNonNull(TencentYunProperties.OSS_ACCESS_SECRET).toString();
String ossBucketName = optionService.getByPropertyOfNonNull(TencentYunProperties.OSS_BUCKET_NAME).toString();
String ossSource = StringUtils.join("https://", ossBucketName, ".cos." + ossRegion+".myqcloud.com");
String cosRegion = optionService.getByPropertyOfNonNull(TencentYunProperties.COS_REGION).toString();
String cosSecretId = optionService.getByPropertyOfNonNull(TencentYunProperties.COS_SECRET_ID).toString();
String cosSecretKey = optionService.getByPropertyOfNonNull(TencentYunProperties.COS_SECRET_KEY).toString();
String cosBucketName = optionService.getByPropertyOfNonNull(TencentYunProperties.COS_BUCKET_NAME).toString();
COSCredentials cred = new BasicCOSCredentials(ossAccessKey, ossAccessSecret);
Region region = new Region(ossRegion);
COSCredentials cred = new BasicCOSCredentials(cosSecretId, cosSecretKey);
Region region = new Region(cosRegion);
ClientConfig clientConfig = new ClientConfig(region);
// Init OSS client
COSClient cosClient = new COSClient(cred, clientConfig);
try {
cosClient.deleteObject(ossBucketName, key);
cosClient.deleteObject(cosBucketName, key);
} catch (Exception e) {
throw new FileOperationException("附件 " + key + " 从腾讯云删除失败", e);
} finally {

View File

@ -77,7 +77,7 @@ public class UpYunFileHandler implements FileHandler {
// Build upload result
UploadResult uploadResult = new UploadResult();
uploadResult.setFilename(basename);
uploadResult.setFilePath(StringUtils.isBlank(filePath) ? filePath : filePath + ossStyleRule);
uploadResult.setFilePath(StringUtils.isBlank(ossStyleRule) ? filePath : filePath + ossStyleRule);
uploadResult.setKey(upFilePath);
uploadResult.setMediaType(MediaType.valueOf(Objects.requireNonNull(file.getContentType())));
uploadResult.setSuffix(extension);
@ -88,7 +88,7 @@ public class UpYunFileHandler implements FileHandler {
BufferedImage image = ImageIO.read(file.getInputStream());
uploadResult.setWidth(image.getWidth());
uploadResult.setHeight(image.getHeight());
uploadResult.setThumbPath(StringUtils.isBlank(ossStyleRule) ? filePath : filePath + ossThumbnailStyleRule);
uploadResult.setThumbPath(StringUtils.isBlank(ossThumbnailStyleRule) ? filePath : filePath + ossThumbnailStyleRule);
}
return uploadResult;
@ -102,7 +102,6 @@ public class UpYunFileHandler implements FileHandler {
Assert.notNull(key, "File key must not be blank");
// Get config
// String ossSource = optionService.getByPropertyOfNonNull(UpYunProperties.OSS_SOURCE).toString();
String ossPassword = optionService.getByPropertyOfNonNull(UpYunProperties.OSS_PASSWORD).toString();
String ossBucket = optionService.getByPropertyOfNonNull(UpYunProperties.OSS_BUCKET).toString();
String ossOperator = optionService.getByPropertyOfNonNull(UpYunProperties.OSS_OPERATOR).toString();

View File

@ -4,6 +4,7 @@ package run.halo.app.model.properties;
* AliYun properties.
*
* @author MyFaith
* @author ryanwang
* @date 2019-04-04 00:00:56
*/
public enum AliYunProperties implements PropertyEnum {
@ -31,7 +32,12 @@ public enum AliYunProperties implements PropertyEnum {
/**
* Aliyun oss style rule
*/
OSS_STYLE_RULE("oss_aliyun_style_rule", String.class, "");
OSS_STYLE_RULE("oss_aliyun_style_rule", String.class, ""),
/**
* Aliyun oss thumbnail style rule
*/
OSS_THUMBNAIL_STYLE_RULE("oss_aliyun_thumbnail_style_rule", String.class, "");
private final String value;

View File

@ -4,34 +4,40 @@ package run.halo.app.model.properties;
* BaiDuYun properties.
*
* @author wangya
* @author ryanwang
* @date 2019-07-19
*/
public enum BaiDuYunProperties implements PropertyEnum {
/**
* BaiDuyun oss endpoint.
* Baidu yun bos endpoint.
*/
OSS_ENDPOINT("oss_baiduyun_endpoint", String.class, ""),
BOS_ENDPOINT("bos_baiduyun_endpoint", String.class, ""),
/**
* BaiDuyun oss bucket name.
* Baidu yun bos bucket name.
*/
OSS_BUCKET_NAME("oss_baiduyun_bucket_name", String.class, ""),
BOS_BUCKET_NAME("bos_baiduyun_bucket_name", String.class, ""),
/**
* BaiDuyun oss access key.
* Baidu yun bos access key.
*/
OSS_ACCESS_KEY("oss_baiduyun_access_key", String.class, ""),
BOS_ACCESS_KEY("bos_baiduyun_access_key", String.class, ""),
/**
* BaiDuyun oss access secret.
* Baidu yun bos secret key.
*/
OSS_ACCESS_SECRET("oss_baiduyun_access_secret", String.class, ""),
BOS_SECRET_KEY("bos_baiduyun_secret_key", String.class, ""),
/**
* BaiDuyun oss style rule
* Baidu yun bos style rule.
*/
OSS_STYLE_RULE("oss_baiduyun_style_rule", String.class, "");
BOS_STYLE_RULE("bos_baiduyun_style_rule", String.class, ""),
/**
* Baidu yun bos thumbnail style rule.
*/
BOS_THUMBNAIL_STYLE_RULE("bos_baiduyun_thumbnail_style_rule", String.class, "");
private final String value;

View File

@ -4,6 +4,7 @@ package run.halo.app.model.properties;
* Qi niu yun properties.
*
* @author johnniang
* @author ryanwang
* @date 3/26/19
*/
public enum QnYunProperties implements PropertyEnum {
@ -18,7 +19,9 @@ public enum QnYunProperties implements PropertyEnum {
OSS_BUCKET("oss_qiniu_bucket", String.class, ""),
OSS_STYLE_RULE("oss_qiniu_style_rule", String.class, "");
OSS_STYLE_RULE("oss_qiniu_style_rule", String.class, ""),
OSS_THUMBNAIL_STYLE_RULE("oss_qiniu_thumbnail_style_rule", String.class, "");
private final String value;

View File

@ -4,34 +4,30 @@ package run.halo.app.model.properties;
* TencentYun properties.
*
* @author wangya
* @author ryanwang
* @date 2019-07-25
*/
public enum TencentYunProperties implements PropertyEnum {
/**
* Tencentyun oss endpoint.
* Tencentyun cos endpoint.
*/
OSS_REGION("oss_tencentyun_region", String.class, ""),
COS_REGION("cos_tencentyun_region", String.class, ""),
/**
* Tencentyun oss bucket name.
* Tencentyun cos bucket name.
*/
OSS_BUCKET_NAME("oss_tencentyun_bucket_name", String.class, ""),
COS_BUCKET_NAME("cos_tencentyun_bucket_name", String.class, ""),
/**
* Tencentyun oss access key.
* Tencentyun cos secret id.
*/
OSS_ACCESS_KEY("oss_tencentyun_access_key", String.class, ""),
COS_SECRET_ID("cos_tencentyun_secret_id", String.class, ""),
/**
* Tencentyun oss access secret.
* Tencentyun cos secret key.
*/
OSS_ACCESS_SECRET("oss_tencentyun_access_secret", String.class, ""),
/**
* Tencentyun oss style rule
*/
OSS_STYLE_RULE("oss_tencentyun_style_rule", String.class, "");
COS_SECRET_KEY("cos_tencentyun_secret_key", String.class, "");
private final String value;