feat: support set file source for qiniu oss.

pull/462/head
ruibaby 2019-12-31 14:24:56 +08:00
parent 2701146860
commit 9ac31d0b1c
2 changed files with 24 additions and 3 deletions

View File

@ -60,6 +60,7 @@ public class QiniuOssFileHandler implements FileHandler {
String bucket = optionService.getByPropertyOfNonNull(QiniuOssProperties.OSS_BUCKET).toString();
String protocol = optionService.getByPropertyOfNonNull(QiniuOssProperties.OSS_PROTOCOL).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 thumbnailStyleRule = optionService.getByPropertyOrDefault(QiniuOssProperties.OSS_THUMBNAIL_STYLE_RULE, String.class, "");
@ -81,16 +82,31 @@ public class QiniuOssFileHandler implements FileHandler {
// Create temp path
Path tmpPath = Paths.get(System.getProperty("java.io.tmpdir"), bucket);
StringBuilder basePath = new StringBuilder(protocol)
.append(domain)
.append("/");
try {
String basename = FilenameUtils.getBasename(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
FileRecorder fileRecorder = new FileRecorder(tmpPath.toFile());
// Get upload manager
UploadManager uploadManager = new UploadManager(configuration, fileRecorder);
// 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 body: [{}]", response.bodyString());
@ -101,13 +117,13 @@ public class QiniuOssFileHandler implements FileHandler {
QiNiuPutSet putSet = JsonUtils.jsonToObject(response.bodyString(), QiNiuPutSet.class);
// Get file full path
String filePath = protocol + StringUtils.appendIfMissing(domain, "/") + putSet.getHash();
String filePath = StringUtils.join(basePath.toString(), upFilePath.toString());
// Build upload result
UploadResult result = new UploadResult();
result.setFilename(basename);
result.setFilePath(StringUtils.isBlank(styleRule) ? filePath : filePath + styleRule);
result.setKey(putSet.getKey());
result.setKey(upFilePath.toString());
result.setSuffix(extension);
result.setWidth(putSet.getWidth());
result.setHeight(putSet.getHeight());

View File

@ -24,6 +24,11 @@ public enum QiniuOssProperties implements PropertyEnum {
*/
OSS_SECRET_KEY("oss_qiniu_secret_key", String.class, ""),
/**
* Qiniu oss source
*/
OSS_SOURCE("oss_qiniu_source", String.class, ""),
/**
* Qiniu oss domain protocol.
*/