package com.ruixuan.common.cos;

import com.ruixuan.common.utils.Check;
import com.ruixuan.common.utils.DateUtils;
import com.ruixuan.common.utils.LoadFileUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.UUID;

/**
 *
 * @ClassName CosUtil
 * @Author ZHAOXA
 * @date 2022-06-06 09:58
 */
public class CosUpload {

    private static final Logger log = LoggerFactory.getLogger(CosUpload.class);
    /**
     * 请求前缀
     */
    public static final String HTTPS_PREFIX = "https:";

    /**
     * 根据原始视频链接，上传到cos，mp4格式
     */
    public static String getMp4CosUrl(String url, String upFileName) {
        try {
            if (!url.contains("http")) {
                url = HTTPS_PREFIX + url;
            }
            InputStream imageStream = LoadFileUtil.getFileStream(url);
            if (!Check.isNull(imageStream)) {
                String path = upFileName + "/" + DateUtils.formatDate(new Date(), DateUtils.YYYYMMDD) + "/";
                String videoName = UUID.randomUUID().toString().replace("-", "") + ".mp4";
                CosFileDTO file = CosUtils.uploadDetailInputStreamV2(imageStream, videoName, "mp4", 0L, path);
                return file.getFileUrl();
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("视频上传到cos失败");
        }
        return null;
    }

    /**
     * 根据原始图片链接，上传到cos，JPG格式
     */
    public static String getJPGCosUrl(String url, String upFileName, String fileName) {
        try {
            if (!url.contains("http")) {
                url = HTTPS_PREFIX + url;
            }
            InputStream imageStream = LoadFileUtil.getFileStream(url);
            if (Check.isNotNull(imageStream)) {
                String path = upFileName + "/" + DateUtils.formatDate(new Date(), DateUtils.YYYYMMDD) + "/";
                String videoName = fileName + ".jpg";
                CosFileDTO file = CosUtils.uploadDetailInputStreamV2(imageStream, videoName, "jpg", 0L, path);
                return file.getFileUrl();
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("图片上传到cos失败");
        }
        return null;
    }

    /**
     * 根据文件，上传到cos，文件自己后缀格式
     */
    public static String getFileCosUrl(MultipartFile file, String upFileName) {
        InputStream inputStream = null;
        try {
            //存储路径
            String path = upFileName + "/" + DateUtils.formatDate(new Date(), DateUtils.YYYYMMDD) + "/";
            //原文件名
            String fileName = file.getOriginalFilename();
            //文件后缀
            String fileSuffix = "";
            if (fileName.contains(".")) {
                fileSuffix = fileName.substring(fileName.lastIndexOf("."), fileName.length());
            }
            inputStream = file.getInputStream();
            CosFileDTO resultFile = CosUtils.uploadDetailInputStreamV2(inputStream, fileName, fileSuffix, file.getSize(), path);
            return resultFile.getFileUrl();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("图片上传到cos失败");
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
}

