use of run.halo.app.exception.FileOperationException in project halo-plugin-experimental by guqing.
the class BaiduBosFileHandler method delete.
@Override
public void delete(String key) {
Assert.notNull(key, "File key must not be blank");
// Get config
String endPoint = optionService.getByPropertyOfNonNull(BaiduBosProperties.BOS_ENDPOINT).toString();
String accessKey = optionService.getByPropertyOfNonNull(BaiduBosProperties.BOS_ACCESS_KEY).toString();
String secretKey = optionService.getByPropertyOfNonNull(BaiduBosProperties.BOS_SECRET_KEY).toString();
String bucketName = optionService.getByPropertyOfNonNull(BaiduBosProperties.BOS_BUCKET_NAME).toString();
BosClientConfiguration config = new BosClientConfiguration();
config.setCredentials(new DefaultBceCredentials(accessKey, secretKey));
config.setEndpoint(endPoint);
// Init OSS client
BosClient client = new BosClient(config);
try {
client.deleteObject(bucketName, key);
} catch (Exception e) {
throw new FileOperationException("附件 " + key + " 从百度云删除失败", e);
} finally {
client.shutdown();
}
}
use of run.halo.app.exception.FileOperationException in project halo-plugin-experimental by guqing.
the class HuaweiObsFileHandler method delete.
@Override
public void delete(@NonNull String key) {
Assert.notNull(key, "File key must not be blank");
// Get config
String endPoint = optionService.getByPropertyOfNonNull(HuaweiObsProperties.OSS_ENDPOINT).toString();
String accessKey = optionService.getByPropertyOfNonNull(HuaweiObsProperties.OSS_ACCESS_KEY).toString();
String accessSecret = optionService.getByPropertyOfNonNull(HuaweiObsProperties.OSS_ACCESS_SECRET).toString();
String bucketName = optionService.getByPropertyOfNonNull(HuaweiObsProperties.OSS_BUCKET_NAME).toString();
// Init OSS client
final ObsClient obsClient = new ObsClient(accessKey, accessSecret, endPoint);
try {
obsClient.deleteObject(bucketName, key);
} catch (Exception e) {
throw new FileOperationException("附件 " + key + " 从华为云删除失败", e);
} finally {
try {
obsClient.close();
} catch (IOException e) {
log.error(e.getMessage());
}
}
}
use of run.halo.app.exception.FileOperationException in project halo-plugin-experimental by guqing.
the class HuaweiObsFileHandler method upload.
@Override
@NonNull
public UploadResult upload(@NonNull MultipartFile file) {
Assert.notNull(file, "Multipart file must not be null");
// Get config
String protocol = optionService.getByPropertyOfNonNull(HuaweiObsProperties.OSS_PROTOCOL).toString();
String domain = optionService.getByPropertyOrDefault(HuaweiObsProperties.OSS_DOMAIN, String.class, "");
String source = optionService.getByPropertyOrDefault(HuaweiObsProperties.OSS_SOURCE, String.class, "");
String endPoint = optionService.getByPropertyOfNonNull(HuaweiObsProperties.OSS_ENDPOINT).toString();
String accessKey = optionService.getByPropertyOfNonNull(HuaweiObsProperties.OSS_ACCESS_KEY).toString();
String accessSecret = optionService.getByPropertyOfNonNull(HuaweiObsProperties.OSS_ACCESS_SECRET).toString();
String bucketName = optionService.getByPropertyOfNonNull(HuaweiObsProperties.OSS_BUCKET_NAME).toString();
String styleRule = optionService.getByPropertyOrDefault(HuaweiObsProperties.OSS_STYLE_RULE, String.class, "");
String thumbnailStyleRule = optionService.getByPropertyOrDefault(HuaweiObsProperties.OSS_THUMBNAIL_STYLE_RULE, String.class, "");
// Init OSS client
final ObsClient obsClient = new ObsClient(accessKey, accessSecret, endPoint);
StringBuilder basePath = new StringBuilder(protocol);
if (StringUtils.isNotEmpty(domain)) {
basePath.append(domain).append(URL_SEPARATOR);
} else {
basePath.append(bucketName).append(".").append(endPoint).append(URL_SEPARATOR);
}
try {
FilePathDescriptor pathDescriptor = new FilePathDescriptor.Builder().setBasePath(domain).setSubPath(source).setAutomaticRename(true).setRenamePredicate(relativePath -> attachmentRepository.countByFileKeyAndType(relativePath, AttachmentType.HUAWEIOBS) > 0).setOriginalName(file.getOriginalFilename()).build();
log.info(basePath.toString());
// Upload
PutObjectResult putObjectResult = obsClient.putObject(bucketName, pathDescriptor.getRelativePath(), file.getInputStream());
if (putObjectResult == null) {
throw new FileOperationException("上传附件 " + file.getOriginalFilename() + " 到华为云失败 ");
}
// Response result
UploadResult uploadResult = new UploadResult();
uploadResult.setFilename(pathDescriptor.getName());
String fullPath = pathDescriptor.getFullPath();
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());
handleImageMetadata(file, uploadResult, () -> {
if (ImageUtils.EXTENSION_ICO.equals(pathDescriptor.getExtension())) {
return fullPath;
} else {
return StringUtils.isBlank(thumbnailStyleRule) ? fullPath : fullPath + thumbnailStyleRule;
}
});
log.info("Uploaded file: [{}] successfully", file.getOriginalFilename());
return uploadResult;
} catch (Exception e) {
throw new FileOperationException("上传附件 " + file.getOriginalFilename() + " 到华为云失败 ", e).setErrorData(file.getOriginalFilename());
} finally {
try {
obsClient.close();
} catch (IOException e) {
log.error(e.getMessage());
}
}
}
use of run.halo.app.exception.FileOperationException in project halo-plugin-experimental by guqing.
the class MinioFileHandler method upload.
@NonNull
@Override
public UploadResult upload(@NonNull MultipartFile file) {
Assert.notNull(file, "Multipart file must not be null");
// Get config
String endpoint = optionService.getByPropertyOfNonNull(MinioProperties.ENDPOINT).toString();
String accessKey = optionService.getByPropertyOfNonNull(MinioProperties.ACCESS_KEY).toString();
String accessSecret = optionService.getByPropertyOfNonNull(MinioProperties.ACCESS_SECRET).toString();
String bucketName = optionService.getByPropertyOfNonNull(MinioProperties.BUCKET_NAME).toString();
String source = optionService.getByPropertyOrDefault(MinioProperties.SOURCE, String.class, "");
String region = optionService.getByPropertyOrDefault(MinioProperties.REGION, String.class, "us-east-1");
endpoint = StringUtils.appendIfMissing(endpoint, HaloConst.URL_SEPARATOR);
MinioClient minioClient = MinioClient.builder().endpoint(endpoint).credentials(accessKey, accessSecret).region(region).build();
try {
FilePathDescriptor pathDescriptor = new FilePathDescriptor.Builder().setBasePath(endpoint + bucketName).setSubPath(source).setAutomaticRename(true).setRenamePredicate(relativePath -> attachmentRepository.countByFileKeyAndType(relativePath, AttachmentType.MINIO) > 0).setOriginalName(file.getOriginalFilename()).build();
PutObjectArgs putObjectArgs = PutObjectArgs.builder().contentType(file.getContentType()).bucket(bucketName).stream(file.getInputStream(), file.getSize(), -1).object(pathDescriptor.getRelativePath()).build();
minioClient.ignoreCertCheck();
minioClient.putObject(putObjectArgs);
UploadResult uploadResult = new UploadResult();
uploadResult.setFilename(pathDescriptor.getName());
uploadResult.setFilePath(pathDescriptor.getFullPath());
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, pathDescriptor::getFullPath);
return uploadResult;
} catch (Exception e) {
log.error("upload file to MINIO failed", e);
throw new FileOperationException("上传附件 " + file.getOriginalFilename() + " 到 MinIO 失败 ", e).setErrorData(e.getMessage());
}
}
use of run.halo.app.exception.FileOperationException in project halo-plugin-experimental by guqing.
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();
}
}
Aggregations