package com.sptg.gateway.controller;

import cn.hutool.core.io.FileTypeUtil;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.common.core.util.Base64DecodeMultipartFileUtils;
import lombok.RequiredArgsConstructor;
import org.apache.commons.io.FilenameUtils;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * title: FileUploadController
 * package: com.sptg.gateway.controller
 */
@RestController
@RequiredArgsConstructor
public class FileUploadController {

    private final FastFileStorageClient fastFileStorageClient;

    private static final String FILE_PNG=".png";
    private static final String FILE_JPG=".jpg";
    private static final String FILE_JPEG=".jpeg";

    /* -------------------------------------------------------------------------------------------------------------- */

    @PostMapping(value = "/common/file/upload")
    public ResultBean<StorePath> upload(@RequestParam("file") MultipartFile multipartFile)throws Exception{
        Assert.notNull(multipartFile,"参数异常");
        String filename = multipartFile.getOriginalFilename();
        String fileType = filename.substring(filename.lastIndexOf("."));
        //先判断后缀名
        if (FILE_PNG.equalsIgnoreCase(fileType) || FILE_JPG.equalsIgnoreCase(fileType) || FILE_JPEG.equalsIgnoreCase(fileType)) {
            String type ;
            try {
                type = FileTypeUtil.getType(multipartFile.getInputStream());
                //根据首部字节判断文件类型
                if (FILE_PNG.contains(type)||FILE_JPG.contains(type)||FILE_JPEG.contains(type)){
                    long size = multipartFile.getSize();
                    String extension = FilenameUtils.getExtension(multipartFile.getOriginalFilename());
                    StorePath storePath = this.fastFileStorageClient.uploadFile(multipartFile.getInputStream(), size, extension, null);
                    return new ResultBean<>(ResultEnum.SUCCESS,storePath);
                }else{
                    return new ResultBean(ResultEnum.FAILURE,"不支持的文件格式");
                }
            } catch (Exception e) {
                return new ResultBean(ResultEnum.FAILURE,"不支持的文件格式");

            }
        }else{
            return new ResultBean(ResultEnum.FAILURE,"不支持的文件格式");
        }
    }

    @PostMapping(value = "/common/file/uploadBase64")
    public ResultBean<StorePath> uploadBase64File(@RequestBody String base64Array) throws Exception{
        MultipartFile multipartFile  = Base64DecodeMultipartFileUtils.base64ToMultipart(base64Array);
        Assert.notNull(multipartFile,"参数异常");
        StorePath storePath = this.fastFileStorageClient.uploadFile(
                multipartFile.getInputStream(), multipartFile.getSize(),
                FilenameUtils.getExtension(multipartFile.getOriginalFilename()), null);
        return new ResultBean<>(ResultEnum.SUCCESS,storePath);
    }

}
