package com.cnd3b.utility.obs;

import com.obs.services.ObsClient;
import com.obs.services.exception.ObsException;
import com.obs.services.model.*;
import com.cnd3b.common.parameter.parameter;
import java.util.ArrayList;
import java.util.List;

public class Bucket {
    protected String bucketname;
    ObsClient obsClient;

    public Bucket(String siteid) {
        // 创建ObsClient实例
        obsClient = new ObsClient(parameter.Bucket_ak(), parameter.Bucket_sk(), parameter.Bucket_endPoint());
        this.bucketname = siteid.toLowerCase();
        //a();
    }

    public void setBucketCors() {
        BucketCors cors = new BucketCors();

        List<BucketCorsRule> rules = new ArrayList<BucketCorsRule>();
        BucketCorsRule rule = new BucketCorsRule();

        ArrayList<String> allowedOrigin = new ArrayList<String>();
// 指定允许跨域请求的来源
        allowedOrigin.add("*");
        rule.setAllowedOrigin(allowedOrigin);

        ArrayList<String> allowedMethod = new ArrayList<String>();
// 指定允许的跨域请求方法(GET/PUT/DELETE/POST/HEAD)
        allowedMethod.add("GET");
        allowedMethod.add("HEAD");
        allowedMethod.add("PUT");
        allowedMethod.add("POST");
        allowedMethod.add("DELETE");
        rule.setAllowedMethod(allowedMethod);

        ArrayList<String> allowedHeader = new ArrayList<String>();
// 控制在OPTIONS预取指令中Access-Control-Request-Headers头中指定的header是否被允许使用
        allowedHeader.add("*");
        rule.setAllowedHeader(allowedHeader);

        //ArrayList<String> exposeHeader = new ArrayList<String>();
// 指定允许用户从应用程序中访问的header
//        exposeHeader.add("x-obs-expose-header");
//        rule.setExposeHeader(exposeHeader);

// 指定浏览器对特定资源的预取(OPTIONS)请求返回结果的缓存时间,单位为秒
        rule.setMaxAgeSecond(3600);
        rules.add(rule);
        cors.setRules(rules);
        obsClient.setBucketCors(bucketname, cors);
    }

    public String getBucketName() {
        return this.bucketname;
    }


    /**
     * 创建桶
     */
    public boolean create() {
        // 创建桶
        try {
            ObsBucket obsBucket = new ObsBucket();
            obsBucket.setBucketName(bucketname);
            // 设置桶访问权限为公共读写
            obsBucket.setAcl(AccessControlList.REST_CANNED_PUBLIC_READ_WRITE);
            // 设置桶区域位置
            obsBucket.setLocation(parameter.Bucket_location());
            // 创建桶
            HeaderResponse response = obsClient.createBucket(obsBucket);
            setBucketCors();
        } catch (ObsException e) {
            // 创建桶失败
            System.out.println("HTTP Code: " + e.getResponseCode());
            System.out.println("Error Code:" + e.getErrorCode());
            System.out.println("Error Message: " + e.getErrorMessage());

            System.out.println("Request ID:" + e.getErrorRequestId());
            System.out.println("Host ID:" + e.getErrorHostId());
            return false;
        }
        return true;
    }

    /**
     * 是否已存在桶
     *
     * @return
     */
    public boolean isAllReadyUsed() {
        return obsClient.headBucket(bucketname);
    }


    /**
     * 删除桶
     *
     * @return
     */
    public void delete() {
        // 删除桶
        obsClient.deleteBucket(bucketname);
    }

    /**
     * 设置桶配额,单位G
     */
    public void setVolume(long volume) {
        BucketQuota quota = new BucketQuota(1024 * 1024 * 1024 * volume);
        obsClient.setBucketQuota(bucketname, quota);
    }

    /**
     * 获取桶配额,单位字节
     */
    public long getMaxVolume() {
        BucketQuota quota = obsClient.getBucketQuota(bucketname);
        return quota.getBucketQuota();
    }

    /**
     * 获取桶存量
     *
     * @return
     */
    public long getVolume() {
        BucketStorageInfo storageInfo = obsClient.getBucketStorageInfo(bucketname);
        return storageInfo.getSize();
    }

    /**
     * 获取文件数量
     *
     * @return
     */
    public long getFileCount() {
        BucketStorageInfo storageInfo = obsClient.getBucketStorageInfo(bucketname);
        return storageInfo.getObjectNumber();
    }


    public static void main(String[] args) {
        Bucket bucket = new Bucket("jxyl");
        bucket.setBucketCors();
    }
}
