package com.search.supersearch.util;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.*;
import lombok.extern.slf4j.Slf4j;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;
import java.util.List;

@Slf4j
public class AliOssUtil {

/*    private static final String endpoint = "https://oss-cn-beijing.aliyuncs.com";
    private static final String accessKeyId = "LTAI5tPoP9JgGLLDorkfyWEd";
    private static final String accessKeySecret = "wRYrd8XK6gDMldbVc3KImiNQLwqear";
    public static final String BUCKET = "platomixtest";*/

    public static final String BUCKET = "dsj-sjxt-testbucket01";
    private static final String endpoint = "http://oss-cn-anshun-dtcloud-d01-a.ops.dtcloud.spic";
    private static final String accessKeyId = "G4yqKKVMwEVfwDPg";
    private static final String accessKeySecret = "rkxHoNWeYJRfgX1NaaB2qjvcU72Spk";
    private static final OSS client;

    static {
        client = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    }

    /**
     * 返回 oss
     *
     * @param
     * @return
     */
    public static OSS getOSS() {
        return client;
    }

    /**
     * 创建储存空间名称
     *
     * @param name 创建存储空间名称
     * @return
     */
    public static boolean create(String name) {
        CreateBucketRequest createBucketRequest = new CreateBucketRequest(name);
        Bucket bucket = client.createBucket(createBucketRequest);
        return bucket != null;
    }

    /**
     * 实现文件上传
     *
     * @param bucket 存储空间名称
     * @param obj    存储对象名称，带文件后缀
     * @param data   文件内容
     * @return
     */
    public static boolean upload(String bucket, String obj, byte[] data) {
        PutObjectRequest request = new PutObjectRequest(bucket, obj, new ByteArrayInputStream(data));
        request.setProcess("true");
        PutObjectResult result = client.putObject(request);
        return result.getResponse().getStatusCode() == 200;
    }

    /**
     * @param bucketName 存储空间
     * @param objectName 存储文件名
     * @param pathUrl    文件url地址
     * @throws IOException
     */
    public static void uploadByUrl(String objectName, String pathUrl) throws IOException {
        log.info("oss start");
        log.info("oss baseUrl = {}", pathUrl);
        log.info("oss objectName = {}", objectName);
        URL url = new URL(pathUrl);
        URLConnection connection = url.openConnection();
        InputStream inputStream = connection.getInputStream();
        PutObjectRequest putObjectRequest = new PutObjectRequest(BUCKET, objectName, inputStream);
        client.putObject(putObjectRequest);
        log.info("oss end");
        //client.shutdown();
    }

    public static List<Bucket> getBucketsAll() {
        //列举oss里得所有 存储空间
        List<Bucket> buckets = client.listBuckets();
        return buckets;
    }

    public static List<OSSObjectSummary> getSummarysAll() {
        ObjectListing objectListing = client.listObjects(BUCKET);
        List<OSSObjectSummary> summaries = objectListing.getObjectSummaries();
        return summaries;
    }

    /**
     * 创建访问链接
     *
     * @param bucket 存储空间名称
     * @param obj    存储对象名称，带文件名后缀
     * @param etime  访问地址的失效时间
     * @return 访问地址
     */
    public static String createUrl(String bucket, String obj, Date etime) {
        return client.generatePresignedUrl(bucket, obj, etime).toString();
    }

    /**
     * 删除文件
     *
     * @param bucket 存储空间名称
     * @param obj    存储对象名称，带文件后缀
     */
    public static void deFile(String bucket, String obj) {
        client.deleteObject(bucket, obj);
    }

}


