package com.brframework.commonoss.core;

import com.aliyun.oss.ClientConfiguration;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.common.auth.CredentialsProvider;
import com.aliyun.oss.model.PutObjectResult;
import com.brframework.commonoss.core.sts.STSAuthorization;

import java.io.File;

/**
 * @author xu
 * @date 2019/8/21 10:42
 */
public class AliYunOSSClient extends OSSClient {

    private String defaultBucketName;
    private String defaultCDNDomain;

    public AliYunOSSClient(String endpoint, CredentialsProvider credsProvider, ClientConfiguration config,
                           String defaultBucketName, String defaultCDNDomain) {
        super(endpoint, credsProvider, config);
        this.defaultBucketName = defaultBucketName;
        if(defaultCDNDomain.endsWith("/")){
            this.defaultCDNDomain = defaultCDNDomain;
        } else {
            this.defaultCDNDomain = defaultCDNDomain + "/";
        }
    }


    public String getDefaultBucketName() {
        return defaultBucketName;
    }

    public String getDefaultCDNDomain(){return defaultCDNDomain;}

    /**
     * 通过uri生成url
     * @param uri
     * @return
     */
    public String genUrl(String uri){
        return defaultCDNDomain + uri;
    }

    public void upload(String key,File file){
        PutObjectResult result = putObject(defaultBucketName, key, file);
    }

    public OSSMessage getOSSMessage(STSAuthorization sts, String bucket, String prefix){
        return OSSMessage.builder()
                .bucket(bucket)
                .sts(sts)
                .prefix(prefix)
                .build();
    }
}
