Search in sources :

Example 1 with StringMap

use of com.qiniu.util.StringMap in project jeesuite-libs by vakinge.

the class QiniuProvider method createUploadToken.

@Override
public Map<String, Object> createUploadToken(UploadTokenParam param) {
    if (StringUtils.isNotBlank(param.getCallbackUrl())) {
        if (StringUtils.isBlank(param.getCallbackBody())) {
            param.setCallbackBody(DEFAULT_CALLBACK_BODY);
        }
        if (StringUtils.isBlank(param.getCallbackHost())) {
            param.setCallbackHost(host);
        }
    }
    Map<String, Object> result = new HashMap<>();
    StringMap policy = new StringMap();
    policy.putNotNull(policyFields[0], param.getCallbackUrl());
    policy.putNotNull(policyFields[1], param.getCallbackBody());
    policy.putNotNull(policyFields[2], param.getCallbackHost());
    policy.putNotNull(policyFields[3], param.getCallbackBodyType());
    policy.putNotNull(policyFields[4], param.getFileType());
    policy.putNotNull(policyFields[5], param.getFileKey());
    policy.putNotNull(policyFields[6], param.getMimeLimit());
    policy.putNotNull(policyFields[7], param.getFsizeMin());
    policy.putNotNull(policyFields[8], param.getFsizeMax());
    policy.putNotNull(policyFields[9], param.getDeleteAfterDays());
    String token = auth.uploadToken(bucketName, param.getFileKey(), param.getExpires(), policy, true);
    result.put("uptoken", token);
    result.put("host", this.urlprefix);
    result.put("dir", param.getUploadDir());
    return result;
}
Also used : StringMap(com.qiniu.util.StringMap) HashMap(java.util.HashMap) UploadObject(com.jeesuite.filesystem.UploadObject)

Example 2 with StringMap

use of com.qiniu.util.StringMap 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 3 with StringMap

use of com.qiniu.util.StringMap in project jeesuite-libs by vakinge.

the class QiniuProvider method createUploadToken.

@Override
public Map<String, Object> createUploadToken(UploadTokenParam param) {
    if (StringUtils.isNotBlank(param.getCallbackUrl())) {
        if (StringUtils.isBlank(param.getCallbackBody())) {
            param.setCallbackBody(DEFAULT_CALLBACK_BODY);
        }
        if (StringUtils.isBlank(param.getCallbackHost())) {
            param.setCallbackHost(param.getCallbackHost());
        }
    }
    Map<String, Object> result = new HashMap<>();
    StringMap policy = new StringMap();
    policy.putNotNull(policyFields[0], param.getCallbackUrl());
    policy.putNotNull(policyFields[1], param.getCallbackBody());
    policy.putNotNull(policyFields[2], param.getCallbackHost());
    policy.putNotNull(policyFields[3], param.getCallbackBodyType());
    policy.putNotNull(policyFields[4], param.getFileType());
    policy.putNotNull(policyFields[5], param.getFileKey());
    policy.putNotNull(policyFields[6], param.getMimeLimit());
    policy.putNotNull(policyFields[7], param.getFsizeMin());
    policy.putNotNull(policyFields[8], param.getFsizeMax());
    policy.putNotNull(policyFields[9], param.getDeleteAfterDays());
    String token = auth.uploadToken(param.getBucketName(), param.getFileKey(), param.getExpires(), policy, true);
    result.put("uptoken", token);
    result.put("dir", param.getUploadDir());
    return result;
}
Also used : StringMap(com.qiniu.util.StringMap) HashMap(java.util.HashMap) CUploadObject(com.jeesuite.cos.CUploadObject)

Example 4 with StringMap

use of com.qiniu.util.StringMap in project rebuild by getrebuild.

the class QiniuCloud method getUploadToken.

/**
 * @param fileKey
 * @return
 * @see #formatFileKey(String)
 */
public String getUploadToken(String fileKey) {
    // 上传策略参见 https://developer.qiniu.com/kodo/manual/1206/put-policy
    int maxSize = RebuildConfiguration.getInt(ConfigurationItem.PortalUploadMaxSize);
    StringMap policy = new StringMap().put("fsizeLimit", FileUtils.ONE_MB * maxSize);
    return getAuth().uploadToken(bucketName, fileKey, 60 * 10, policy);
}
Also used : StringMap(com.qiniu.util.StringMap)

Example 5 with StringMap

use of com.qiniu.util.StringMap in project rebuild by getrebuild.

the class QiniuCloud method stats.

/**
 * 获取空间大小
 *
 * @return bytes
 */
@SuppressWarnings("deprecation")
public long stats() {
    String time = CalendarUtils.getPlainDateFormat().format(CalendarUtils.now());
    String url = String.format("%s/v6/space?bucket=%s&begin=%s000000&end=%s235959&g=day", CONFIGURATION.apiHost(), bucketName, time, time);
    StringMap headers = getAuth().authorization(url);
    try {
        Client client = new Client(CONFIGURATION);
        Response resp = client.get(url, headers);
        if (resp.isOK()) {
            JSONObject map = JSON.parseObject(resp.bodyString());
            return map.getJSONArray("datas").getLong(0);
        }
    } catch (QiniuException e) {
        log.warn(null, e);
    }
    return -1;
}
Also used : Response(com.qiniu.http.Response) StringMap(com.qiniu.util.StringMap) QiniuException(com.qiniu.common.QiniuException) JSONObject(com.alibaba.fastjson.JSONObject) Client(com.qiniu.http.Client)

Aggregations

StringMap (com.qiniu.util.StringMap)9 QiniuException (com.qiniu.common.QiniuException)5 Response (com.qiniu.http.Response)5 Auth (com.qiniu.util.Auth)4 Configuration (com.qiniu.storage.Configuration)3 Region (com.qiniu.storage.Region)3 UploadManager (com.qiniu.storage.UploadManager)3 FileRecorder (com.qiniu.storage.persistent.FileRecorder)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 HashMap (java.util.HashMap)2 JSONObject (com.alibaba.fastjson.JSONObject)1 LoginRequired (com.diy.community.annotation.LoginRequired)1 CUploadObject (com.jeesuite.cos.CUploadObject)1 UploadObject (com.jeesuite.filesystem.UploadObject)1 Client (com.qiniu.http.Client)1 CloudFileException (org.noear.solon.cloud.exception.CloudFileException)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1