Search in sources :

Example 1 with RestManager

use of com.upyun.RestManager in project oss-spring-boot-starter by ArtIsLong.

the class UpOssConfiguration method restManager.

public RestManager restManager(UpOssConfig upOssConfig) {
    RestManager restManager = new RestManager(upOssConfig.getBucketName(), upOssConfig.getUserName(), upOssConfig.getPassword());
    // 手动设置超时时间:默认为30秒
    restManager.setTimeout(upOssConfig.getTimeout());
    // 选择最优的接入点
    restManager.setApiDomain(upOssConfig.getApiDomain().toString());
    return restManager;
}
Also used : RestManager(com.upyun.RestManager)

Example 2 with RestManager

use of com.upyun.RestManager in project halo by halo-dev.

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 3 with RestManager

use of com.upyun.RestManager 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 4 with RestManager

use of com.upyun.RestManager 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 5 with RestManager

use of com.upyun.RestManager 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);
    }
}
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)

Aggregations

RestManager (com.upyun.RestManager)8 UpException (com.upyun.UpException)6 IOException (java.io.IOException)6 Response (okhttp3.Response)6 FileOperationException (run.halo.app.exception.FileOperationException)6 HashMap (java.util.HashMap)3 UploadResult (run.halo.app.model.support.UploadResult)3 ParallelUploader (com.upyun.ParallelUploader)1