Search in sources :

Example 6 with JeesuiteBaseException

use of com.mendmix.common.JeesuiteBaseException in project jeesuite-libs by vakinge.

the class AwsProvider method upload.

@Override
public CUploadResult upload(CUploadObject object) {
    try {
        String bucketName = object.getBucketName();
        if (StringUtils.isEmpty(bucketName)) {
            throw new JeesuiteBaseException("BucketName 不能为空");
        }
        String fileKey = object.getFileKey();
        PutObjectResponse putObjectResponse = null;
        long size = 0;
        if (object.getFile() != null) {
            size = object.getFile().length();
            PutObjectRequest putRequest = PutObjectRequest.builder().bucket(bucketName).key(fileKey).contentType(object.getMimeType()).build();
            putObjectResponse = s3Client.putObject(putRequest, object.getFile().toPath());
        } else if (object.getInputStream() != null) {
            size = object.getInputStream().available();
            PutObjectRequest putRequest = PutObjectRequest.builder().bucket(bucketName).key(fileKey).contentType(object.getMimeType()).build();
            putObjectResponse = s3Client.putObject(putRequest, RequestBody.fromInputStream(object.getInputStream(), object.getInputStream().available()));
        } else if (object.getBytes() != null) {
            size = object.getBytes().length;
            PutObjectRequest putRequest = PutObjectRequest.builder().bucket(bucketName).key(fileKey).contentType(object.getMimeType()).build();
            putObjectResponse = s3Client.putObject(putRequest, RequestBody.fromBytes(object.getBytes()));
        }
        if (putObjectResponse != null) {
            CUploadResult uploadResult = new CUploadResult(fileKey, getDownloadUrl(bucketName, fileKey, 300), null);
            uploadResult.setMimeType(object.getMimeType());
            uploadResult.setFileSize(size);
            return uploadResult;
        }
    } catch (JeesuiteBaseException e) {
        throw e;
    } catch (Exception e) {
        LOGGER.warn("上传失败, e={}", ExceptionUtils.getMessage(e), e);
        throw new JeesuiteBaseException(e.getMessage());
    }
    return null;
}
Also used : CUploadResult(com.mendmix.cos.CUploadResult) JeesuiteBaseException(com.mendmix.common.JeesuiteBaseException) PutObjectResponse(software.amazon.awssdk.services.s3.model.PutObjectResponse) PutObjectRequest(software.amazon.awssdk.services.s3.model.PutObjectRequest) JeesuiteBaseException(com.mendmix.common.JeesuiteBaseException)

Example 7 with JeesuiteBaseException

use of com.mendmix.common.JeesuiteBaseException in project jeesuite-libs by vakinge.

the class HuaweicloudProvider method generatePresignedUrl.

@Override
protected String generatePresignedUrl(String bucketName, String fileKey, int expireInSeconds) {
    // 默认5分钟, 最长7天
    if (!exists(bucketName, fileKey)) {
        throw new JeesuiteBaseException("对象[bucketName=" + bucketName + ",fileKey=" + fileKey + "]不存在");
    }
    TemporarySignatureRequest req = new TemporarySignatureRequest(HttpMethodEnum.GET, expireInSeconds);
    req.setBucketName(bucketName);
    req.setObjectKey(fileKey);
    TemporarySignatureResponse res = obsClient.createTemporarySignature(req);
    String signedUrl = res.getSignedUrl();
    return signedUrl;
}
Also used : JeesuiteBaseException(com.mendmix.common.JeesuiteBaseException) TemporarySignatureResponse(com.obs.services.model.TemporarySignatureResponse) TemporarySignatureRequest(com.obs.services.model.TemporarySignatureRequest)

Example 8 with JeesuiteBaseException

use of com.mendmix.common.JeesuiteBaseException in project jeesuite-libs by vakinge.

the class MinioProvider method upload.

@Override
public CUploadResult upload(CUploadObject object) {
    try {
        String bucketName = object.getBucketName();
        if (StringUtils.isEmpty(bucketName)) {
            throw new JeesuiteBaseException("BucketName 不能为空");
        }
        String fileKey = object.getFileKey();
        InputStream inputStream = object.getInputStream();
        byte[] objectBytes = object.getBytes();
        ObjectWriteResponse objectWriteResponse = null;
        long size = 0;
        if (object.getFile() != null) {
            objectWriteResponse = minioClient.uploadObject(UploadObjectArgs.builder().bucket(bucketName).filename(object.getFile().getAbsolutePath()).object(fileKey).contentType(object.getMimeType()).build());
            size = object.getFile().length();
        } else if (objectBytes != null) {
            byte[] bytes = objectBytes;
            ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
            objectWriteResponse = minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(fileKey).contentType(object.getMimeType()).stream(bis, bytes.length, -1).build());
            size = bytes.length;
            bis.close();
        } else if (inputStream != null) {
            objectWriteResponse = minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(fileKey).contentType(object.getMimeType()).stream(inputStream, inputStream.available(), -1).build());
            size = inputStream.available();
        } else {
            throw new JeesuiteBaseException("upload object is NULL");
        }
        if (objectWriteResponse != null) {
            CUploadResult uploadResult = new CUploadResult(fileKey, getDownloadUrl(object.getBucketName(), fileKey, 300), null);
            uploadResult.setMimeType(object.getMimeType());
            uploadResult.setFileSize(size);
            return uploadResult;
        }
    } catch (JeesuiteBaseException jbex) {
        throw jbex;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return null;
}
Also used : ObjectWriteResponse(io.minio.ObjectWriteResponse) CUploadResult(com.mendmix.cos.CUploadResult) JeesuiteBaseException(com.mendmix.common.JeesuiteBaseException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) JeesuiteBaseException(com.mendmix.common.JeesuiteBaseException)

Example 9 with JeesuiteBaseException

use of com.mendmix.common.JeesuiteBaseException in project jeesuite-libs by vakinge.

the class QcloudProvider method upload.

@Override
public CUploadResult upload(CUploadObject object) {
    PutObjectRequest request;
    String fileKey = object.getFileKey();
    String bucketName = buildBucketName(object.getBucketName());
    if (object.getFile() != null) {
        request = new PutObjectRequest(bucketName, fileKey, object.getFile());
    } else if (object.getBytes() != null) {
        ByteArrayInputStream inputStream = new ByteArrayInputStream(object.getBytes());
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentLength(object.getFileSize());
        request = new PutObjectRequest(bucketName, fileKey, inputStream, objectMetadata);
    } else if (object.getInputStream() != null) {
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentLength(object.getFileSize());
        request = new PutObjectRequest(bucketName, fileKey, object.getInputStream(), objectMetadata);
    } else {
        throw new IllegalArgumentException("upload object is NULL");
    }
    try {
        if (object.getFileSize() > conf.getMaxAllowdSingleFileSize()) {
            Upload upload = transferManager.upload(request);
            com.qcloud.cos.model.UploadResult result = upload.waitForUploadResult();
            return new CUploadResult(fileKey, getFullPath(object.getBucketName(), fileKey), result.getCrc64Ecma());
        } else {
            PutObjectResult result = cosclient.putObject(request);
            return new CUploadResult(fileKey, getFullPath(object.getBucketName(), fileKey), result.getContentMd5());
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new JeesuiteBaseException(500, buildMessage(bucketName, e));
    }
}
Also used : PutObjectResult(com.qcloud.cos.model.PutObjectResult) Upload(com.qcloud.cos.transfer.Upload) CosServiceException(com.qcloud.cos.exception.CosServiceException) JeesuiteBaseException(com.mendmix.common.JeesuiteBaseException) IOException(java.io.IOException) CUploadResult(com.mendmix.cos.CUploadResult) JeesuiteBaseException(com.mendmix.common.JeesuiteBaseException) ByteArrayInputStream(java.io.ByteArrayInputStream) CObjectMetadata(com.mendmix.cos.CObjectMetadata) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest)

Example 10 with JeesuiteBaseException

use of com.mendmix.common.JeesuiteBaseException in project jeesuite-libs by vakinge.

the class SignatureRequestHandler method process.

@Override
public Builder process(ServerWebExchange exchange, BizSystemModule module, Builder requestBuilder) {
    ApiInfo apiInfo = module.getApiInfo(exchange.getRequest().getMethodValue(), exchange.getRequest().getPath().value());
    if (apiInfo == null || !apiInfo.isOpenApi()) {
        throw new JeesuiteBaseException("该接口未开放访问权限");
    }
    HttpHeaders headers = exchange.getRequest().getHeaders();
    String sign = headers.getFirst(GatewayConstants.X_SIGN_HEADER);
    if (StringUtils.isBlank(sign))
        return requestBuilder;
    if (StringUtils.isBlank(sign))
        return requestBuilder;
    String timestamp = headers.getFirst(GatewayConstants.TIMESTAMP_HEADER);
    String appId = headers.getFirst(GatewayConstants.APP_ID_HEADER);
    if (StringUtils.isAnyBlank(timestamp, appId)) {
        throw new JeesuiteBaseException("认证头信息不完整");
    }
    String secret = appIdSecretMappings.get(appId);
    if (StringUtils.isBlank(secret)) {
        throw new JeesuiteBaseException("appId不存在");
    }
    Object body = RuequestHelper.getCachingBodyString(exchange);
    Map<String, Object> map = JsonUtils.toHashMap(body.toString(), Object.class);
    String signBaseString = StringUtils.trimToEmpty(ParameterUtils.mapToQueryParams(map)) + timestamp + secret;
    String expectSign = DigestUtils.md5(signBaseString);
    if (!expectSign.equals(sign)) {
        throw new JeesuiteBaseException("签名错误");
    }
    return requestBuilder;
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) JeesuiteBaseException(com.mendmix.common.JeesuiteBaseException) ApiInfo(com.mendmix.common.model.ApiInfo)

Aggregations

JeesuiteBaseException (com.mendmix.common.JeesuiteBaseException)44 IOException (java.io.IOException)13 CObjectMetadata (com.mendmix.cos.CObjectMetadata)4 CUploadResult (com.mendmix.cos.CUploadResult)4 CosServiceException (com.qcloud.cos.exception.CosServiceException)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 InputStream (java.io.InputStream)4 HashMap (java.util.HashMap)4 Request (okhttp3.Request)4 ApiInfo (com.mendmix.common.model.ApiInfo)2 WrapperResponse (com.mendmix.common.model.WrapperResponse)2 BizSystemModule (com.mendmix.gateway.model.BizSystemModule)2 COSObject (com.qcloud.cos.model.COSObject)2 QiniuException (com.qiniu.common.QiniuException)2 InputStreamReader (java.io.InputStreamReader)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 InvalidKeyException (java.security.InvalidKeyException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 SignatureException (java.security.SignatureException)2 Map (java.util.Map)2