package com.guwan.controller.login;

import com.guwan.common.R;
import com.guwan.config.GlobalValue;
import com.guwan.config.MinioConfig;
import com.guwan.util.IPUtils;
import com.guwan.util.PathUtil;
import io.minio.MinioClient;
import io.minio.PutObjectOptions;
import lombok.RequiredArgsConstructor;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

@RestController
@RequestMapping("")
@RequiredArgsConstructor
public class UploadController {


    private final MinioConfig minioConfig;

    private final MinioClient minioClient;
    private final GlobalValue globalValue;

    @PostMapping("/GBUpload")
    public R gbUpload(MultipartFile file) {
        if (file.isEmpty() || file.getSize() == 0) {
            System.out.println("文件不能为空");
        }
        String fileName = null;

        String catalogue;

        try {
            String extension = FilenameUtils.getExtension(file.getOriginalFilename()); //后缀名
            if (extension.equals("png") || extension.equals("jpg")){
                catalogue = "photo/";
            }else if (extension.equals("mp4")){
                catalogue = "video/";
            }else {
                return R.error().put("info", "不允许上传此类型!");
            }

            fileName = catalogue + "t_" +
                    UUID.randomUUID().toString().replace("-","") +
                    "." + extension;
            InputStream inputStream = file.getInputStream();
            PutObjectOptions putObjectOptions = new PutObjectOptions(inputStream.available(), -1);
            putObjectOptions.setContentType(file.getContentType());
            minioClient.putObject(
                    minioConfig.getBucketName(), fileName, inputStream, putObjectOptions);
            inputStream.close();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        System.out.println("方法结束");


        UUID uuid = UUID.randomUUID();
        String replace = uuid.toString().replace("-", "");

        int lastDotIndex = file.getOriginalFilename().lastIndexOf('.');

        String substring = file.getOriginalFilename().substring(0, lastDotIndex);

     /*   FileEntity fileEntity = new FileEntity(replace,
                substring, fileName, new Date());
        cqFileMapper.insert(fileEntity);

        return R.ok().put("data", fileEntity);*/
        return R.ok().put("data", minioConfig.getMinioEndpoint() + "/" +
                minioConfig.getBucketName() + "/" + fileName);
    }




    @PostMapping("/demo111")
    public void uploadFile(MultipartFile file) {


        String path = PathUtil.path("");

        try {
            // 确保临时路径存在
            File tempDir = new File(path);
            if (!tempDir.exists()) {
                tempDir.mkdirs(); // 创建目录
            }

            // 构建文件的完整路径
            File serverFile = new File(path + File.separator + file.getOriginalFilename());

            // 将上传的文件保存到服务器
            file.transferTo(serverFile);

            System.out.println("文件上传成功，保存路径为：" + serverFile.getAbsolutePath());
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("文件上传失败：" + e.getMessage());
        }
    }


}
