package com.yuchen.saas.park.service.controller.file;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.yuchen.saas.park.api.vo.responsevo.FaceUploadResponse;
import com.yuchen.saas.park.service.config.MinioParkConfig;
import io.minio.MinioClient;
import io.minio.errors.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.nest.springwrap.core.tool.api.R;
import org.nest.springwrap.core.tool.utils.StringPool;
import org.springframework.boot.web.server.MimeMappings;
import org.springframework.util.MimeTypeUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.xmlpull.v1.XmlPullParserException;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

/**
 * @ClassName UploadImageController
 * @Description 上传图片控制器
 * @Author 张逸飞
 * @Date 2022/6/28 17:10
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/upload")
@Api(value = "上传图片控制器", tags = "上传图片控制器")
public class UploadController {
    /**
     * 注入 Minio 配置信息
     */
    @Resource
    private MinioParkConfig minioParkConfig;

    /**
     * 上传图片
     * @param file 图片数据
     * @return
     */
    @PostMapping("/uploadPng")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "上传图片", notes = "上传图片")
    public R postFileBase64(@RequestParam("file") MultipartFile file) throws InvalidArgumentException,
            InvalidBucketNameException, InsufficientDataException, XmlPullParserException, ErrorResponseException,
            NoSuchAlgorithmException, IOException, NoResponseException, InvalidKeyException, InternalException,
            InvalidPortException, InvalidEndpointException {

        // 图片名称 采用 UUID
        String fileName = UUID.randomUUID().toString();
        // 统一将图片名字命名为 png
        String picName = fileName + "." + "png";
        // 获取图片流媒
        InputStream inputStream = file.getInputStream();
        MinioClient minioClient = new MinioClient(minioParkConfig.getEndpoint(), minioParkConfig.getApiKey(), minioParkConfig.getApiSecret());
        minioClient.putObject(minioParkConfig.getFaceBucket(), picName, inputStream, MimeTypeUtils.IMAGE_JPEG_VALUE);
        return R.data(new FaceUploadResponse(picName, minioParkConfig.getFaceProxy()+"/" + picName));
    }

    /**
     * 上传图片
     * @param file 图片数据
     * @return
     */
    @PostMapping("/uploadImg")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "上传图片", notes = "上传图片")
    public R uploadPng(@RequestParam("file") MultipartFile file) throws InvalidArgumentException,
            InvalidBucketNameException, InsufficientDataException, XmlPullParserException, ErrorResponseException,
            NoSuchAlgorithmException, IOException, NoResponseException, InvalidKeyException, com.sun.jdi.InternalException,
            InvalidPortException, InvalidEndpointException, InternalException {

        String type =file.getContentType().substring(6);
        // 图片名称 采用 UUID
        String fileName = UUID.randomUUID().toString();
        // 统一将图片名字命名为 png
        String picName = fileName + "." + type;
        // 获取图片流媒
        InputStream inputStream = file.getInputStream();
        MinioClient minioClient = new MinioClient(minioParkConfig.getEndpoint(), minioParkConfig.getApiKey(),
                minioParkConfig.getApiSecret());
        minioClient.putObject(minioParkConfig.getFaceBucket(), picName, inputStream, MimeTypeUtils.IMAGE_JPEG_VALUE);
        return R.data(new FaceUploadResponse(picName, minioParkConfig.getFaceProxy() + "/" + picName));
    }

    @PostMapping("/uploadFile")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "上传文件", notes = "上传文件")
    public R uploadFile(@RequestParam("file") MultipartFile file) throws InvalidArgumentException,
            InvalidBucketNameException, InsufficientDataException, XmlPullParserException, ErrorResponseException,
            NoSuchAlgorithmException, IOException, NoResponseException, InvalidKeyException, com.sun.jdi.InternalException,
            InvalidPortException, InvalidEndpointException, InternalException {
        String mimeType =file.getContentType();
        String extension= StringPool.EMPTY;
        MimeMappings mappings=  new MimeMappings(MimeMappings.DEFAULT);
        mappings.add("docx","application/vnd.openxmlformats-officedocument.wordprocessingml.document");
        for (MimeMappings.Mapping item:mappings) {
            if(item.getMimeType().equals(mimeType)){
                extension=item.getExtension();
                break;
            }
        }

        // 图片名称 采用 UUID
        String fileName = UUID.randomUUID().toString();
        // 统一将图片名字命名为 png
        String picName = fileName + "." + extension;
        // 获取图片流媒
        InputStream inputStream = file.getInputStream();
        MinioClient minioClient = new MinioClient(minioParkConfig.getEndpoint(), minioParkConfig.getApiKey(),
                minioParkConfig.getApiSecret());
        minioClient.putObject(minioParkConfig.getFileBucket(), picName, inputStream, extension);
        return R.data(new FaceUploadResponse(picName, minioParkConfig.getFaceProxy() + "/" + picName));
    }

    @PostMapping("/uploadPdf")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "上传文件", notes = "上传文件")
    public R uploadPdf(@RequestParam("file") MultipartFile file) throws InvalidArgumentException,
            InvalidBucketNameException, InsufficientDataException, XmlPullParserException, ErrorResponseException,
            NoSuchAlgorithmException, IOException, NoResponseException, InvalidKeyException, com.sun.jdi.InternalException,
            InvalidPortException, InvalidEndpointException, InternalException {
        String mimeType =file.getContentType();
        String extension= StringPool.EMPTY;
        MimeMappings mappings=  new MimeMappings(MimeMappings.DEFAULT);
        mappings.add("pdf","application/pdf");
        for (MimeMappings.Mapping item:mappings) {
            if(item.getMimeType().equals(mimeType)){
                extension=item.getExtension();
                break;
            }
        }
        // 文件名称
        String fileName = file.getOriginalFilename();
        log.info("fileName: {}", fileName);
        // 获取文件流
        InputStream inputStream = file.getInputStream();
        MinioClient minioClient = new MinioClient(minioParkConfig.getEndpoint(), minioParkConfig.getApiKey(), minioParkConfig.getApiSecret());
        minioClient.putObject(minioParkConfig.getPdfBucket(), fileName, inputStream, extension);
        return R.data(new FaceUploadResponse(fileName, minioParkConfig.getPdfProxy() + "/" + fileName));
    }

    @PostMapping("/uploadFileNew")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "上传文件", notes = "上传文件")
    public R uploadFileNew(@RequestParam("file") MultipartFile file) throws InvalidArgumentException,
            InvalidBucketNameException, InsufficientDataException, XmlPullParserException, ErrorResponseException,
            NoSuchAlgorithmException, IOException, NoResponseException, InvalidKeyException, com.sun.jdi.InternalException,
            InvalidPortException, InvalidEndpointException, InternalException {
        String mimeType =file.getContentType();

        String fileName = file.getOriginalFilename();
        // 获取图片流媒
        InputStream inputStream = file.getInputStream();
        MinioClient minioClient = new MinioClient(minioParkConfig.getEndpoint(), minioParkConfig.getApiKey(),
                minioParkConfig.getApiSecret());
        minioClient.putObject(minioParkConfig.getFileBucket(), fileName, inputStream, mimeType);
        return R.data(new FaceUploadResponse(fileName, minioParkConfig.getFileProxy() + "/" + fileName));
    }


}
