package com.quinn.utils;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.OSSObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Random;

/**
 * 阿里云 OSS文件类
 *
 * @author zuojie.liang
 */
@Component
public class OSSClientUtil {

    Log log = LogFactory.getLog(OSSClientUtil.class);
    /**
     * 协议
     */
    @Value("${oss.protocol}")
    private String protocol;
    /**
     * 仓库名称
     */
    @Value("${oss.name}")
    private String bucketName;
    /**
     * 公网节点
     */
    @Value("${oss.endPoint}")
    private String endpoint;
    @Value("${oss.accessKeyId}")
    private String accessKeyId;
    /**
     * API访问值
     */
    @Value("${oss.accessKeySecret}")
    private String accessKeySecret;
    @Value("${oss.url}")
    private String url;
    private OSSClient ossClient;

    String SAVE_DIR = "data/recovery/";

    public String uploadFile(String filename, InputStream file) {
        return uploadFile(file, filename);
    }

    /**
     * 上传图片
     *
     * @param file
     * @return
     */
    public String uploadFile(MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        return uploadFile(file.getInputStream(), originalFilename);
    }

    private String uploadFile(InputStream inputStream, String fileName) {
        String backUrl = "";
        try {
            ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
            backUrl = SAVE_DIR + fileName;
            // 上传文件
            ossClient.putObject(bucketName, backUrl, inputStream);
            ossClient.setObjectAcl(bucketName, backUrl, CannedAccessControlList.PublicRead);
            // 判断是否上传成功
            boolean uploadResult = ossClient.doesObjectExist(bucketName, backUrl);

            if (!uploadResult) {
                backUrl = "";
            }
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return backUrl;
    }

    /**
     * 获得图片路径
     *
     * @param fileUrl
     * @return
     */
    public String getImgUrl(String fileUrl) {
        if (!StringUtils.isEmpty(fileUrl)) {
            String[] split = fileUrl.split("/");
            return url + "imageDir/" + split[split.length - 1];
        }
        return null;
    }

    public InputStream downloadFile(String objName){
        // ossObject包含文件所在的存储空间名称、文件名称、文件元信息以及一个输入流。
        ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        OSSObject ossObject = ossClient.getObject(bucketName, objName);
        return ossObject.getObjectContent();
    }

    public void deleteFile(String objName){
        // ossObject包含文件所在的存储空间名称、文件名称、文件元信息以及一个输入流。
        ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        try {
            ossClient.deleteObject(bucketName,objName);
        }catch (Exception e){
            log.error("删除失败",e);
        }
    }

}
