package com.gic.haoban.manage.web.controller;

import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSONObject;
import com.gic.commons.util.DateUtil;
import com.gic.commons.webapi.reponse.RestResponse;
import com.gic.haoban.common.utils.HaobanResponse;
import com.gic.haoban.common.utils.UploadUtils;
import com.gic.haoban.manage.web.errCode.HaoBanErrCode;
import com.gic.thirdparty.cloudfile.CloudFileUtil;
import com.gic.thirdparty.cloudfile.enums.CloudFileBusinessOptEnum;
import com.gic.thirdparty.cloudfile.enums.CloudFileTypeEnum;
import com.gic.thirdparty.cloudfile.pojo.CloudFileInfo;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@RestController
public class UploadController extends WebBaseController {
    private static Logger logger = LoggerFactory.getLogger(UploadController.class);

    @RequestMapping("/upload-file")
    @ResponseBody
    public RestResponse upload(@RequestParam MultipartFile file) throws Exception {
        if (file == null || file.isEmpty()) {
            return RestResponse.failure("-9999","上传的文件不能为空！");
        }
        String name = file.getOriginalFilename();
        // 未加密前文件名称
        String mediaName = file.getOriginalFilename();
        mediaName = mediaName.substring(0, mediaName.lastIndexOf("."));
        if (mediaName.length() > 20) {
            mediaName = mediaName.substring(0, 20);
        }
        String suffix = name.substring(name.lastIndexOf(".") + 1);
        name = SecureUtil.md5(name) + "." + suffix;
        String location = System.getProperty("user.dir") + "/data/tmp/";
        long len = file.getSize();

        File templateFile = new File(location + name);
        FileUtils.copyInputStreamToFile(file.getInputStream(), templateFile);
        // 2.上传腾讯云
        String dayFilePath = LocalDateTime.now().format(DateTimeFormatter.ofPattern(DateUtil.FORMAT_DATETIME_14));
        String nameUpload = mediaName + "." + suffix;
        String key = "haoban/" + dayFilePath + "/" + nameUpload;
        try {
            //替换新的上传接口
            CloudFileTypeEnum cloudFileTypeEnum = CloudFileTypeEnum.OTHER;
            if (UploadUtils.isPicture(suffix)) {
                cloudFileTypeEnum = CloudFileTypeEnum.IMAGE;
            } else if (UploadUtils.isOffice(suffix)) {
                cloudFileTypeEnum = CloudFileTypeEnum.FILE;
            } else if (UploadUtils.isMusic(suffix)) {
                cloudFileTypeEnum = CloudFileTypeEnum.AUDIO;
            } else if (UploadUtils.isVedio(suffix)) {
                cloudFileTypeEnum = CloudFileTypeEnum.VIDEO;
            }
            CloudFileInfo cloudFileInfo = CloudFileUtil.uploadFile(new FileInputStream(templateFile), suffix, cloudFileTypeEnum, "gic_inner", CloudFileBusinessOptEnum.HAOBAN_COMMON);
            if (templateFile != null) {
                templateFile.delete();
            }
            JSONObject res = new JSONObject();
            res.put("name", name);
            res.put("mediaName", mediaName);
            res.put("ext", suffix);
            res.put("size", len);
            res.put("key", key);
            res.put("url", cloudFileInfo.getOrgFileUrl());
            return RestResponse.successResult(res);
        } catch (Exception e) {
            logger.info("异常:{}", e.getMessage(), e);
            return RestResponse.failure("-9999","操作失败");
        }
    }
}
