use of run.halo.app.exception.FileOperationException in project halo by ruibaby.
the class LocalFileHandler method delete.
@Override
public void delete(String key) {
Assert.hasText(key, "File key must not be blank");
// Get path
Path path = Paths.get(workDir, key);
// Delete the file key
try {
Files.deleteIfExists(path);
} catch (IOException e) {
throw new FileOperationException("附件 " + key + " 删除失败", e);
}
// Delete thumb if necessary
String basename = FilenameUtils.getBasename(key);
String extension = FilenameUtils.getExtension(key);
// Get thumbnail name
String thumbnailName = basename + THUMBNAIL_SUFFIX + '.' + extension;
// Get thumbnail path
Path thumbnailPath = Paths.get(path.getParent().toString(), thumbnailName);
// Delete thumbnail file
try {
boolean deleteResult = Files.deleteIfExists(thumbnailPath);
if (!deleteResult) {
log.warn("Thumbnail: [{}] may not exist", thumbnailPath.toString());
}
} catch (IOException e) {
throw new FileOperationException("附件缩略图 " + thumbnailName + " 删除失败", e);
}
}
use of run.halo.app.exception.FileOperationException in project halo by ruibaby.
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 by ruibaby.
the class MinioFileHandler method delete.
@Override
public void delete(@NonNull String key) {
Assert.notNull(key, "File key must not be blank");
String endPoint = optionService.getByPropertyOfNonNull(MinioProperties.ENDPOINT).toString();
endPoint = StringUtils.appendIfMissing(endPoint, HaloConst.URL_SEPARATOR);
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 region = optionService.getByPropertyOrDefault(MinioProperties.REGION, String.class, "us-east-1");
MinioClient minioClient = MinioClient.builder().endpoint(endPoint).credentials(accessKey, accessSecret).region(region).build();
try {
minioClient.ignoreCertCheck();
minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucketName).object(key).build());
} catch (Exception e) {
log.error("delete MINIO file: [{}] failed", key, e);
throw new FileOperationException("附件 " + key + " 从 MinIO 删除失败", e).setErrorData(e.getMessage());
}
}
use of run.halo.app.exception.FileOperationException in project halo by ruibaby.
the class QiniuOssFileHandler method delete.
@Override
public void delete(String key) {
Assert.notNull(key, "File key must not be blank");
Region region = optionService.getQiniuRegion();
String accessKey = optionService.getByPropertyOfNonNull(QiniuOssProperties.OSS_ACCESS_KEY).toString();
String secretKey = optionService.getByPropertyOfNonNull(QiniuOssProperties.OSS_SECRET_KEY).toString();
String bucket = optionService.getByPropertyOfNonNull(QiniuOssProperties.OSS_BUCKET).toString();
// Create configuration
Configuration configuration = new Configuration(region);
// Create auth
Auth auth = Auth.create(accessKey, secretKey);
BucketManager bucketManager = new BucketManager(auth, configuration);
try {
Response response = bucketManager.delete(bucket, key);
if (!response.isOK()) {
log.warn("附件 " + key + " 从七牛云删除失败");
throw new FileOperationException("附件 " + key + " 从七牛云删除失败");
}
} catch (QiniuException e) {
log.error("Qiniu oss error response: [{}]", e.response);
throw new FileOperationException("附件 " + key + " 从七牛云删除失败", e);
}
}
use of run.halo.app.exception.FileOperationException in project halo by ruibaby.
the class QiniuOssFileHandler method upload.
@Override
public UploadResult upload(MultipartFile file) {
Assert.notNull(file, "Multipart file must not be null");
Region region = optionService.getQiniuRegion();
String accessKey = optionService.getByPropertyOfNonNull(QiniuOssProperties.OSS_ACCESS_KEY).toString();
String secretKey = optionService.getByPropertyOfNonNull(QiniuOssProperties.OSS_SECRET_KEY).toString();
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, "");
// Create configuration
Configuration configuration = new Configuration(region);
// Create auth
Auth auth = Auth.create(accessKey, secretKey);
// Build put plicy
StringMap putPolicy = new StringMap();
putPolicy.put("returnBody", "{\"key\":\"$(key)\",\"hash\":\"$(etag)\",\"size\":$(fsize),\"width\":$(imageInfo" + ".width),\"height\":$(imageInfo.height)}");
// Get upload token
String uploadToken = auth.uploadToken(bucket, null, 60 * 60, putPolicy);
// Create temp path
Path tmpPath = Paths.get(ensureSuffix(TEMP_DIR, FILE_SEPARATOR), bucket);
StringBuilder basePath = new StringBuilder(protocol).append(domain).append(URL_SEPARATOR);
try {
FilePathDescriptor pathDescriptor = new FilePathDescriptor.Builder().setBasePath(basePath.toString()).setSubPath(source).setAutomaticRename(true).setRenamePredicate(relativePath -> attachmentRepository.countByFileKeyAndType(relativePath, AttachmentType.QINIUOSS) > 0).setOriginalName(file.getOriginalFilename()).build();
// 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(), pathDescriptor.getRelativePath(), uploadToken, null, null);
if (log.isDebugEnabled()) {
log.debug("Qiniu oss response: [{}]", response.toString());
log.debug("Qiniu oss response body: [{}]", response.bodyString());
}
// Convert response
PutSet putSet = JsonUtils.jsonToObject(response.bodyString(), PutSet.class);
// Get file full path
String fullPath = pathDescriptor.getFullPath();
// Build upload result
UploadResult result = new UploadResult();
result.setFilename(pathDescriptor.getName());
result.setFilePath(StringUtils.isBlank(styleRule) ? fullPath : fullPath + styleRule);
result.setKey(pathDescriptor.getRelativePath());
result.setSuffix(pathDescriptor.getExtension());
result.setWidth(putSet.getWidth());
result.setHeight(putSet.getHeight());
result.setMediaType(MediaType.valueOf(Objects.requireNonNull(file.getContentType())));
result.setSize(file.getSize());
if (isImageType(file)) {
if (ImageUtils.EXTENSION_ICO.equals(pathDescriptor.getExtension())) {
result.setThumbPath(fullPath);
} else {
result.setThumbPath(StringUtils.isBlank(thumbnailStyleRule) ? fullPath : fullPath + thumbnailStyleRule);
}
}
return result;
} catch (IOException e) {
if (e instanceof QiniuException) {
log.error("Qiniu oss error response: [{}]", ((QiniuException) e).response);
}
throw new FileOperationException("上传附件 " + file.getOriginalFilename() + " 到七牛云失败", e);
}
}
Aggregations