use of run.halo.app.exception.FileOperationException in project halo by ruibaby.
the class TencentCosFileHandler method upload.
@Override
public UploadResult upload(MultipartFile file) {
Assert.notNull(file, "Multipart file must not be null");
// Get config
String protocol = optionService.getByPropertyOfNonNull(TencentCosProperties.COS_PROTOCOL).toString();
String domain = optionService.getByPropertyOrDefault(TencentCosProperties.COS_DOMAIN, String.class, "");
String region = optionService.getByPropertyOfNonNull(TencentCosProperties.COS_REGION).toString();
String secretId = optionService.getByPropertyOfNonNull(TencentCosProperties.COS_SECRET_ID).toString();
String secretKey = optionService.getByPropertyOfNonNull(TencentCosProperties.COS_SECRET_KEY).toString();
String bucketName = optionService.getByPropertyOfNonNull(TencentCosProperties.COS_BUCKET_NAME).toString();
String source = optionService.getByPropertyOrDefault(TencentCosProperties.COS_SOURCE, String.class, "");
String styleRule = optionService.getByPropertyOrDefault(TencentCosProperties.COS_STYLE_RULE, String.class, "");
String thumbnailStyleRule = optionService.getByPropertyOrDefault(TencentCosProperties.COS_THUMBNAIL_STYLE_RULE, String.class, "");
COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
Region regionConfig = new Region(region);
ClientConfig clientConfig = new ClientConfig(regionConfig);
// Init OSS client
COSClient cosClient = new COSClient(cred, clientConfig);
StringBuilder basePath = new StringBuilder(protocol);
if (StringUtils.isNotEmpty(domain)) {
basePath.append(domain).append(URL_SEPARATOR);
} else {
basePath.append(bucketName).append(".cos.").append(region).append(".myqcloud.com").append(URL_SEPARATOR);
}
try {
FilePathDescriptor pathDescriptor = new FilePathDescriptor.Builder().setBasePath(basePath.toString()).setSubPath(source).setAutomaticRename(true).setRenamePredicate(relativePath -> attachmentRepository.countByFileKeyAndType(relativePath, AttachmentType.TENCENTCOS) > 0).setOriginalName(file.getOriginalFilename()).build();
// Upload
ObjectMetadata objectMetadata = new ObjectMetadata();
// 提前告知输入流的长度, 否则可能导致 oom
objectMetadata.setContentLength(file.getSize());
// 设置 Content type, 默认是 application/octet-stream
objectMetadata.setContentType(file.getContentType());
PutObjectResult putObjectResponseFromInputStream = cosClient.putObject(bucketName, pathDescriptor.getRelativePath(), file.getInputStream(), objectMetadata);
if (putObjectResponseFromInputStream == null) {
throw new FileOperationException("上传附件 " + file.getOriginalFilename() + " 到腾讯云失败 ");
}
String fullPath = pathDescriptor.getFullPath();
// Response result
UploadResult uploadResult = new UploadResult();
uploadResult.setFilename(pathDescriptor.getName());
uploadResult.setFilePath(StringUtils.isBlank(styleRule) ? fullPath : fullPath + styleRule);
uploadResult.setKey(pathDescriptor.getRelativePath());
uploadResult.setMediaType(MediaType.valueOf(Objects.requireNonNull(file.getContentType())));
uploadResult.setSuffix(pathDescriptor.getExtension());
uploadResult.setSize(file.getSize());
// Handle thumbnail
handleImageMetadata(file, uploadResult, () -> {
if (ImageUtils.EXTENSION_ICO.equals(pathDescriptor.getExtension())) {
uploadResult.setThumbPath(fullPath);
return fullPath;
} else {
return StringUtils.isBlank(thumbnailStyleRule) ? fullPath : fullPath + thumbnailStyleRule;
}
});
return uploadResult;
} catch (Exception e) {
throw new FileOperationException("附件 " + file.getOriginalFilename() + " 上传失败(腾讯云)", e);
} finally {
cosClient.shutdown();
}
}
use of run.halo.app.exception.FileOperationException in project halo by ruibaby.
the class UpOssFileHandler method upload.
@Override
public UploadResult upload(MultipartFile file) {
Assert.notNull(file, "Multipart file must not be null");
String source = optionService.getByPropertyOfNonNull(UpOssProperties.OSS_SOURCE).toString();
String password = optionService.getByPropertyOfNonNull(UpOssProperties.OSS_PASSWORD).toString();
String bucket = optionService.getByPropertyOfNonNull(UpOssProperties.OSS_BUCKET).toString();
String protocol = optionService.getByPropertyOfNonNull(UpOssProperties.OSS_PROTOCOL).toString();
String domain = optionService.getByPropertyOfNonNull(UpOssProperties.OSS_DOMAIN).toString();
String operator = optionService.getByPropertyOfNonNull(UpOssProperties.OSS_OPERATOR).toString();
// style rule can be null
String styleRule = optionService.getByPropertyOrDefault(UpOssProperties.OSS_STYLE_RULE, String.class, "");
String thumbnailStyleRule = optionService.getByPropertyOrDefault(UpOssProperties.OSS_THUMBNAIL_STYLE_RULE, String.class, "");
RestManager manager = new RestManager(bucket, operator, password);
manager.setTimeout(60 * 10);
manager.setApiDomain(RestManager.ED_AUTO);
Map<String, String> params = new HashMap<>();
try {
// Get file basename
String basename = FilenameUtils.getBasename(Objects.requireNonNull(file.getOriginalFilename()));
// Get file extension
String extension = FilenameUtils.getExtension(file.getOriginalFilename());
// Get md5 value of the file
String md5OfFile = DigestUtils.md5DigestAsHex(file.getInputStream());
// Build file path
String upFilePath = StringUtils.appendIfMissing(source, "/") + md5OfFile + '.' + extension;
// Set md5Content
params.put(RestManager.PARAMS.CONTENT_MD5.getValue(), md5OfFile);
// Write file
Response result = manager.writeFile(upFilePath, file.getInputStream(), params);
if (!result.isSuccessful()) {
throw new FileOperationException("上传附件 " + file.getOriginalFilename() + " 到又拍云失败" + upFilePath);
}
String filePath = protocol + StringUtils.removeEnd(domain, "/") + upFilePath;
// Build upload result
UploadResult uploadResult = new UploadResult();
uploadResult.setFilename(basename);
uploadResult.setFilePath(StringUtils.isBlank(styleRule) ? filePath : filePath + styleRule);
uploadResult.setKey(upFilePath);
uploadResult.setMediaType(MediaType.valueOf(Objects.requireNonNull(file.getContentType())));
uploadResult.setSuffix(extension);
uploadResult.setSize(file.getSize());
// Handle thumbnail
handleImageMetadata(file, uploadResult, () -> {
if (ImageUtils.EXTENSION_ICO.equals(extension)) {
uploadResult.setThumbPath(filePath);
return filePath;
} else {
return StringUtils.isBlank(thumbnailStyleRule) ? filePath : filePath + thumbnailStyleRule;
}
});
result.close();
return uploadResult;
} catch (Exception e) {
throw new FileOperationException("上传附件 " + file.getOriginalFilename() + " 到又拍云失败", e);
}
}
use of run.halo.app.exception.FileOperationException in project halo by ruibaby.
the class UpOssFileHandler method delete.
@Override
public void delete(String key) {
Assert.notNull(key, "File key must not be blank");
// Get config
String password = optionService.getByPropertyOfNonNull(UpOssProperties.OSS_PASSWORD).toString();
String bucket = optionService.getByPropertyOfNonNull(UpOssProperties.OSS_BUCKET).toString();
String operator = optionService.getByPropertyOfNonNull(UpOssProperties.OSS_OPERATOR).toString();
RestManager manager = new RestManager(bucket, operator, password);
manager.setTimeout(60 * 10);
manager.setApiDomain(RestManager.ED_AUTO);
try {
Response result = manager.deleteFile(key, null);
if (!result.isSuccessful()) {
log.warn("附件 " + key + " 从又拍云删除失败");
throw new FileOperationException("附件 " + key + " 从又拍云删除失败");
}
} catch (IOException | UpException e) {
e.printStackTrace();
throw new FileOperationException("附件 " + key + " 从又拍云删除失败", e);
}
}
use of run.halo.app.exception.FileOperationException in project halo by ruibaby.
the class AliOssFileHandler method delete.
@Override
public void delete(@NonNull String key) {
Assert.notNull(key, "File key must not be blank");
// Get config
String endPoint = optionService.getByPropertyOfNonNull(AliOssProperties.OSS_ENDPOINT).toString();
String accessKey = optionService.getByPropertyOfNonNull(AliOssProperties.OSS_ACCESS_KEY).toString();
String accessSecret = optionService.getByPropertyOfNonNull(AliOssProperties.OSS_ACCESS_SECRET).toString();
String bucketName = optionService.getByPropertyOfNonNull(AliOssProperties.OSS_BUCKET_NAME).toString();
// Init OSS client
OSS ossClient = new OSSClientBuilder().build(endPoint, accessKey, accessSecret);
try {
ossClient.deleteObject(new DeleteObjectsRequest(bucketName).withKey(key));
} catch (Exception e) {
throw new FileOperationException("附件 " + key + " 从阿里云删除失败", e);
} finally {
ossClient.shutdown();
}
}
Aggregations