Search in sources :

Example 26 with FileOperationException

use of run.halo.app.exception.FileOperationException in project halo-plugin-experimental by guqing.

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);
    }
}
Also used : Response(okhttp3.Response) HashMap(java.util.HashMap) FileOperationException(run.halo.app.exception.FileOperationException) RestManager(com.upyun.RestManager) UploadResult(run.halo.app.model.support.UploadResult) FileOperationException(run.halo.app.exception.FileOperationException) IOException(java.io.IOException) UpException(com.upyun.UpException)

Example 27 with FileOperationException

use of run.halo.app.exception.FileOperationException in project halo-plugin-experimental by guqing.

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);
    }
}
Also used : Response(okhttp3.Response) UpException(com.upyun.UpException) FileOperationException(run.halo.app.exception.FileOperationException) RestManager(com.upyun.RestManager) IOException(java.io.IOException)

Example 28 with FileOperationException

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();
    }
}
Also used : BosClientConfiguration(com.baidubce.services.bos.BosClientConfiguration) DefaultBceCredentials(com.baidubce.auth.DefaultBceCredentials) FileOperationException(run.halo.app.exception.FileOperationException) BosClient(com.baidubce.services.bos.BosClient) FileOperationException(run.halo.app.exception.FileOperationException)

Example 29 with FileOperationException

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());
        }
    }
}
Also used : FileOperationException(run.halo.app.exception.FileOperationException) IOException(java.io.IOException) ObsClient(com.obs.services.ObsClient) FileOperationException(run.halo.app.exception.FileOperationException) IOException(java.io.IOException)

Example 30 with FileOperationException

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());
        }
    }
}
Also used : PutObjectResult(com.obs.services.model.PutObjectResult) FileOperationException(run.halo.app.exception.FileOperationException) UploadResult(run.halo.app.model.support.UploadResult) IOException(java.io.IOException) ObsClient(com.obs.services.ObsClient) FileOperationException(run.halo.app.exception.FileOperationException) IOException(java.io.IOException) NonNull(org.springframework.lang.NonNull)

Aggregations

FileOperationException (run.halo.app.exception.FileOperationException)69 IOException (java.io.IOException)39 UploadResult (run.halo.app.model.support.UploadResult)27 Path (java.nio.file.Path)24 NonNull (org.springframework.lang.NonNull)15 ServiceException (run.halo.app.exception.ServiceException)9 OSS (com.aliyun.oss.OSS)6 OSSClientBuilder (com.aliyun.oss.OSSClientBuilder)6 DefaultBceCredentials (com.baidubce.auth.DefaultBceCredentials)6 BosClient (com.baidubce.services.bos.BosClient)6 BosClientConfiguration (com.baidubce.services.bos.BosClientConfiguration)6 ObsClient (com.obs.services.ObsClient)6 COSClient (com.qcloud.cos.COSClient)6 ClientConfig (com.qcloud.cos.ClientConfig)6 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)6 COSCredentials (com.qcloud.cos.auth.COSCredentials)6 Region (com.qcloud.cos.region.Region)6 QiniuException (com.qiniu.common.QiniuException)6 Response (com.qiniu.http.Response)6 Configuration (com.qiniu.storage.Configuration)6