package cn.com.capcloud.api.action.service;

import cn.com.capcloud.ccui.core.common.json.JackJSONObject;
import cn.com.capcloud.core.util.FileExtContentTypeTool;
import cn.com.capcloud.core.util.MinioUtils;
import cn.com.capcloud.core.util.ReadProperty;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @Author LiaoJiaHui
 * @Create 2022/11/30 13:39
 */
@Service
public class MinioService {
    @Resource
    private ReadProperty readProperty;
    @Resource
    private ApiService apiService;

    public Map<String,Object> uploadToMinio(JackJSONObject retJson, HttpServletRequest request) throws Exception {
        MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
        List<MultipartFile> files = multipartHttpServletRequest.getFiles("uploadFile");
        MultipartFile file =files.get(0);
        String realFileName=files.get(0).getOriginalFilename();
        Map<String,Object> returnMap=new HashMap<>();
        if (!file.isEmpty()) {
            try {
                if (file.getSize() > 102400000 * 100) {//100MB
                    return returnMap;
                }
                String extName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1, file.getOriginalFilename().length());
                String realName = realFileName.replace("."+extName, "");
                String fileExt= FileExtContentTypeTool.getByFileExt(extName);
                if(fileExt==null){
                    retJson.putAll(JackJSONObject.fromObject(apiService.getReturnDataByCode("7060")));
                    retJson.put("status","no_code");
                    retJson.put("message","不支持'"+extName+"'文件上传");
                    return new HashMap<>();
                }
                DateTime d = new DateTime();
                String fileName= UUID.randomUUID().toString().replaceAll("-","");
                returnMap.put("uuid", fileName);
                fileName = fileName + "." + extName;
                MinioUtils.putObject(readProperty.getBucketName(), file, d.toString("yyyyMM")+"/"+fileName, FileExtContentTypeTool.getByFileExt(extName));
                returnMap.put("source", "minio");
                returnMap.put("realFileName", realFileName);
                returnMap.put("realName", realName);
                returnMap.put("diskFileName", readProperty.getBucketName()+"/"+d.toString("yyyyMM")+"/"+fileName);
                returnMap.put("suffix",  extName);
                returnMap.put("fileSize",file.getSize());
                returnMap.put("fileShowSize",ApiDao.readableFileSize(file.getSize()));
                //判断如果是图片将宽高放到map里
                if(extName.equalsIgnoreCase("jpg")||extName.equalsIgnoreCase("png")||extName.equalsIgnoreCase("jpeg")){
                    returnMap.put("width",  request.getParameter("width"));
                    returnMap.put("height",  request.getParameter("height"));
                }
                return returnMap;
            } catch (Exception e) {
                e.printStackTrace();
                return returnMap;
            }
        } else {
            retJson.putAll(JackJSONObject.fromObject(apiService.getReturnDataByCode("7060")));
            retJson.put("status","no_code");
            retJson.put("message","空文件");
            return new HashMap<>();
        }
    }
}
