Search in sources :

Example 1 with FileRecorder

use of com.qiniu.storage.persistent.FileRecorder 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);
    }
}
Also used : Path(java.nio.file.Path) StringMap(com.qiniu.util.StringMap) Configuration(com.qiniu.storage.Configuration) FileOperationException(run.halo.app.exception.FileOperationException) FileRecorder(com.qiniu.storage.persistent.FileRecorder) IOException(java.io.IOException) Response(com.qiniu.http.Response) QiniuException(com.qiniu.common.QiniuException) Auth(com.qiniu.util.Auth) Region(com.qiniu.storage.Region) UploadResult(run.halo.app.model.support.UploadResult) UploadManager(com.qiniu.storage.UploadManager)

Example 2 with FileRecorder

use of com.qiniu.storage.persistent.FileRecorder in project halo by halo-dev.

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);
    }
}
Also used : Path(java.nio.file.Path) StringMap(com.qiniu.util.StringMap) Configuration(com.qiniu.storage.Configuration) FileOperationException(run.halo.app.exception.FileOperationException) FileRecorder(com.qiniu.storage.persistent.FileRecorder) IOException(java.io.IOException) Response(com.qiniu.http.Response) QiniuException(com.qiniu.common.QiniuException) Auth(com.qiniu.util.Auth) Region(com.qiniu.storage.Region) UploadResult(run.halo.app.model.support.UploadResult) UploadManager(com.qiniu.storage.UploadManager)

Example 3 with FileRecorder

use of com.qiniu.storage.persistent.FileRecorder in project oss-spring-boot-starter by ArtIsLong.

the class QiNiuOssClient method upLoadCheckPoint.

@Override
public OssInfo upLoadCheckPoint(File file, String targetName) {
    String key = getKey(targetName, false);
    SliceConfig sliceConfig = qiNiuOssConfig.getSliceConfig();
    Configuration cfg = new Configuration(qiNiuOssConfig.getRegion().buildRegion());
    // 指定分片上传版本
    cfg.resumableUploadAPIVersion = Configuration.ResumableUploadAPIVersion.V2;
    // 设置分片上传并发,1:采用同步上传;大于1:采用并发上传
    cfg.resumableUploadMaxConcurrentTaskCount = sliceConfig.getTaskNum();
    cfg.resumableUploadAPIV2BlockSize = sliceConfig.getPartSize().intValue();
    try {
        FileRecorder fileRecorder = new FileRecorder(file.getParent());
        UploadManager uploadManager = new UploadManager(cfg, fileRecorder);
        uploadManager.put(file.getPath(), key, getUpToken());
    } catch (Exception e) {
        String errorMsg = String.format("%s上传失败", targetName);
        log.error(errorMsg, e);
        throw new OssException(errorMsg, e);
    }
    return getInfo(targetName);
}
Also used : Configuration(com.qiniu.storage.Configuration) SliceConfig(io.github.artislong.model.SliceConfig) FileRecorder(com.qiniu.storage.persistent.FileRecorder) UploadManager(com.qiniu.storage.UploadManager) QiniuException(com.qiniu.common.QiniuException) OssException(io.github.artislong.exception.OssException) OssException(io.github.artislong.exception.OssException)

Example 4 with FileRecorder

use of com.qiniu.storage.persistent.FileRecorder in project halo-plugin-experimental by guqing.

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);
    }
}
Also used : Path(java.nio.file.Path) StringMap(com.qiniu.util.StringMap) Configuration(com.qiniu.storage.Configuration) FileOperationException(run.halo.app.exception.FileOperationException) FileRecorder(com.qiniu.storage.persistent.FileRecorder) IOException(java.io.IOException) Response(com.qiniu.http.Response) QiniuException(com.qiniu.common.QiniuException) Auth(com.qiniu.util.Auth) Region(com.qiniu.storage.Region) UploadResult(run.halo.app.model.support.UploadResult) UploadManager(com.qiniu.storage.UploadManager)

Aggregations

QiniuException (com.qiniu.common.QiniuException)4 Configuration (com.qiniu.storage.Configuration)4 UploadManager (com.qiniu.storage.UploadManager)4 FileRecorder (com.qiniu.storage.persistent.FileRecorder)4 Response (com.qiniu.http.Response)3 Region (com.qiniu.storage.Region)3 Auth (com.qiniu.util.Auth)3 StringMap (com.qiniu.util.StringMap)3 IOException (java.io.IOException)3 Path (java.nio.file.Path)3 FileOperationException (run.halo.app.exception.FileOperationException)3 UploadResult (run.halo.app.model.support.UploadResult)3 OssException (io.github.artislong.exception.OssException)1 SliceConfig (io.github.artislong.model.SliceConfig)1