package com.guwan.controller.login;

import com.guwan.common.R;
import com.guwan.config.MinioConfig;
import com.pig4cloud.plugin.oss.service.OssTemplate;
import io.minio.MinioClient;
import io.minio.PutObjectOptions;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.apache.commons.io.FilenameUtils;
import org.apache.http.entity.FileEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.time.Duration;
import java.util.Date;
import java.util.UUID;

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


    private final MinioConfig minioConfig;

    private final MinioClient minioClient;


    private final OssTemplate template;
    /**
     * 上传文件
     * 文件名采用uuid,避免原始文件名中带"-"符号导致下载的时候解析出现异常
     * @param file 资源
     * @return R(bucketName, filename)
     */
    @PostMapping("/upload")
    public String upload(@RequestParam("file") MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        String fileName = uuid + originalFilename;
        template.putObject("demo", uuid + originalFilename, file.getInputStream());




        return fileName;
    }


    /**
     * 获取外链
     * @param bucketName bucket名称
	 * @param objectName 文件名称
	 * @param minutes 过期时间，单位分钟,请注意该值必须小于7天
	 * @return url
     */

    @GetMapping("/getDownloadExternalChain")
    public String get(String bucketName, String objectName, int minutes) {
        return template.getObjectURL(bucketName, objectName, Duration.ofMinutes(minutes));
    }


    @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);
    }


}
