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

import com.alibaba.fastjson.JSONObject;
import com.yuchen.saas.park.service.config.MinioParkConfig;
import com.yuchen.saas.park.api.entity.park.PmgBatParkFileInfo;
import com.yuchen.saas.park.api.vo.responsevo.FaceUploadResponse;
import com.yuchen.saas.park.service.service.PmgBatParkFileInfoService;
import com.yuchen.saas.park.service.util.Base64Img;
import io.minio.MinioClient;
import io.minio.errors.*;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.nest.springwrap.core.tool.api.R;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.xmlpull.v1.XmlPullParserException;

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

/**
 * @author 刘碧清
 * @date 2021/04/26 11:20
 */
@RestController
@AllArgsConstructor
@RequestMapping("/pmgBatParkFileInfo")
@Api(value = "文件信息对象", tags = "文件信息接口")
public class PmgBatParkFileInfoController {
    @Resource
    private PmgBatParkFileInfoService pmgBatParkFileInfoService;

    @Resource
    private MinioParkConfig minioParkConfig;

    // 图片上传
    @PostMapping("/uploadBase64")
    public R<PmgBatParkFileInfo> postFileBase64(@RequestBody JSONObject img)
            throws InvalidPortException, InvalidEndpointException, IOException,
            InvalidKeyException, NoSuchAlgorithmException,
            InsufficientDataException, InvalidArgumentException,
            InternalException, NoResponseException,
            InvalidBucketNameException, XmlPullParserException, ErrorResponseException {
        String imgage = img.getString("img");

        File file = Base64Img.base64ToFile(imgage, "2.png");
        if (file == null) {
            System.out.println("file----null");
            return R.fail("上传失败");
        }
        String fileName = file.getName();
        // 文件类型
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        File tempFile = new File(String.valueOf(file));
        InputStream in = new FileInputStream(tempFile);
        // 生成新文件名，确保唯一性
        String picName = UUID.randomUUID().toString() + suffixName;

        MinioClient minioClient = new MinioClient(minioParkConfig.getEndpoint(),
                minioParkConfig.getApiKey(), minioParkConfig.getApiSecret());
//        minioClient.putObject("haiquan",objectName,new FileInputStream(tempFile),in.available(),fileType);
        minioClient.putObject(minioParkConfig.getFaceBucket(), picName, new FileInputStream(tempFile), in.available(), "image/jpg");

        FaceUploadResponse faceUploadResponse = new FaceUploadResponse(picName,
                minioParkConfig.getFaceProxy() + "/" + picName);
        PmgBatParkFileInfo pmgBatParkFileInfo = new PmgBatParkFileInfo();
        pmgBatParkFileInfo.setFileName(faceUploadResponse.getFaceImageName());
        pmgBatParkFileInfo.setFileSrc(faceUploadResponse.getFaceImageUrl());
        pmgBatParkFileInfo.setFileType("图片");
        if (pmgBatParkFileInfoService.save(pmgBatParkFileInfo)) {
            return R.data(pmgBatParkFileInfo);
        } else {
            return R.fail("上传失败!!!");
        }
    }
}
