package com.lz.common.utils;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectResult;
import com.lz.oss.MockMultipartFile;
import com.lz.oss.OSSConfigure;
import com.lz.oss.OssUploadResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.util.UUID;

@Slf4j
public class OSSUtils {

    public static String uploadImage(String filePath) {

        OssUploadResult result = new OssUploadResult();
        try {
            OSSClient ossClient = new OSSClient(OSSConfigure.endpoint, OSSConfigure.accessKeyId, OSSConfigure.accessKeySecret);
            File localFile = new File(filePath);
            FileInputStream inputStream = new FileInputStream(localFile);
            MultipartFile file = new MockMultipartFile(localFile.getName(), inputStream);

            String fileName = file.getOriginalFilename();
            if (fileName == null || fileName.length() == 0) {
                fileName = file.getName();
            }
            String contextType = getImageFileContentType(fileName);

            String path = "ppl/test/";

            String newFileName = DigestUtil.MD5_16(UUID.randomUUID().toString());
            newFileName = newFileName + fileName.substring(fileName.lastIndexOf("."));

            String fileSuffix = fileName.substring(fileName.lastIndexOf(".") + 1);
            if (fileSuffix.equals("apk")) {
                newFileName = newFileName + ".apk";
            }


            ObjectMetadata metadata = new ObjectMetadata();
            metadata.setContentLength(file.getSize());
            metadata.setContentType(contextType);
            PutObjectResult pubResult =  ossClient.putObject(Constant.OSS_BUCCKET, path + newFileName, file.getInputStream(), metadata);
            if (pubResult == null || StringUtils.isBlank(pubResult.getETag())) {
                log.error("upload to oss error, put object result is null or etag is empty,  fileName {}", fileName);
                result.setSuccess(false);
                result.setMsg("upload to oss error, put object result is null or etag is empty");
                return null;
            }
            result.setSuccess(true);
            result.setMsg("upload to oss succeed");
            result.setFileMd5(pubResult.getETag());
            result.setUrl(OSSConfigure.url + path + newFileName);
        } catch (Exception e) {
            log.error("upload to oss error", e);
            result.setSuccess(false);
            result.setMsg("upload to oss error, message is " + e.getMessage());
        }
        return result.getUrl();
    }


    public static String getImageFileContentType(String fileName) {
        if (StringUtils.isBlank(fileName) || fileName.indexOf(".") < 0) {
            return "";
        }
        String fileSuffix = fileName.substring(fileName.lastIndexOf(".") + 1);
        if ("jpeg".equals(fileSuffix) || "jpg".equals(fileSuffix)) {
            return "image/jpeg";
        } else if ("png".equals(fileSuffix)) {
            return "image/png";
        } else if ("bmp".equals(fileSuffix)) {
            return "image/bmp";
        }
        return "";
    }

    public static void main(String[] args) {
        String u= uploadImage("/Users/quyixiao/Desktop/logo.png");
        System.out.println(u);
    }

}
