package com.guwan.controller.login;

import com.guwan.config.MinioConfig;
import com.guwan.util.UUIDUtil;
import io.minio.MinioClient;
import io.minio.PutObjectOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.regex.Pattern;

@RestController
public class FaceController {

    @Autowired
    private MinioConfig minioConfig;
    @Autowired
    private MinioClient minioClient;


    @GetMapping("/11")
    public String test() {
        try {
            // 调用statObject()来判断对象是否存在。
            // 如果不存在, statObject()抛出异常,
            // 否则则代表对象存在。
            minioClient.statObject(minioConfig.getBucketName(), "tempOrgImageFile");

            //判断人脸照片是否合格
            //1.保存到本地
            InputStream tempInputStream = minioClient.getObject(minioConfig.getBucketName(), tempOrgImageFile);
            String tempPath = globalValue.getStaticLocations() + "/";//+globalValue.getTempDir()+"/";
            String tempOrgFilePath = tempPath + tempOrgImageFile;
            int index;
            byte[] bytes = new byte[1024];
            File outFile = new File(tempOrgFilePath);
            FileOutputStream downloadFile = new FileOutputStream(outFile);
            while ((index = tempInputStream.read(bytes)) != -1) {
                downloadFile.write(bytes, 0, index);
                downloadFile.flush();
            }
            downloadFile.close();
            tempInputStream.close();
            //2.测试上传的文件
            String tempFaceFileName = UUIDUtil.uuid() + ".jpg";
            String tempFaceFilePath = tempPath + tempFaceFileName;

            String osName = System.getProperty("os.name");//获取指定键（即os.name）的系统属性,如：Windows 7。
//                    if (Pattern.matches("Windows.*", osName)) {
            if (!Pattern.matches("Windows.*", osName)) {
                int res = PicSDK.getFace(tempOrgFilePath, tempFaceFilePath);
                if (res != 0) {
                    log.error("图片不合格,未检测到人脸");
//                            return 2;
                }
                //保存底片文件到oss
                InputStream inputStream = new FileInputStream(tempFaceFilePath);
                PutObjectOptions putObjectOptions = new PutObjectOptions(inputStream.available(), -1);
                putObjectOptions.setContentType("image/jpeg");
                minioClient.putObject(
                        minioConfig.getBucketName(), faceImageFileName, inputStream, putObjectOptions);
                inputStream.close();
            } else {
                minioClient.copyObject(
                        minioConfig.getBucketName(),
                        faceImageFileName,
                        null,
                        null,
                        minioConfig.getBucketName(),
                        tempOrgImageFile,
                        null,
                        null);
            }
            //拷贝临时文件正式文件
            minioClient.copyObject(
                    minioConfig.getBucketName(),
                    orgImageFileName,
                    null,
                    null,
                    minioConfig.getBucketName(),
                    tempOrgImageFile,
                    null,
                    null);

            entity.setOrgImage(orgImageFileName);
            entity.setFaceImage(faceImageFileName);
            //删除临时文件oss
            minioClient.removeObject(minioConfig.getBucketName(), tempOrgImageFile);
            //删除本地临时文件
            new File(tempFaceFilePath).delete();
        } catch (Exception e) {
            entity.setOrgImage("");
            entity.setFaceImage("");
            e.printStackTrace();
        }
    }

}
