package com.njzscloud.supervisory.oss.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.njzscloud.common.core.http.constant.Mime;
import com.njzscloud.common.core.tuple.Tuple2;
import com.njzscloud.common.core.utils.R;
import com.njzscloud.common.mvc.util.FileResponseUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/oss")
public class OSSController {

    @Value("${oss.path}")
    private String path;

    @GetMapping("/end_mlt_upload")
    public R<?> endMltUpload(String objectName, String uploadId, int partNum) {
        // Minio.endMltUpload(objectName, uploadId, partNum);
        return R.success();
    }

    @GetMapping("/start_mlt_upload")
    public R<Tuple2<String, List<String>>> startMltUpload(
            String objectName,
            String contentType,
            int partNum) {
        // Minio.startMltUpload(objectName, contentType, partNum)
        return R.success();
    }

    @GetMapping("/obtain_presigned_url")
    public R<Map<String, String>> obtainPresignedUrl(
            @RequestParam(value = "bucketName") String bucketName,
            @RequestParam(value = "filename") String filename) {
        String objectName = IdUtil.fastSimpleUUID();
        if (StrUtil.isNotBlank(filename)) {
            objectName = objectName + "." + FileUtil.extName(filename);
        }
        // Map<String, String> data = Minio.obtainPresignedUrl(bucketName, objectName);
        return R.success();
    }

    @GetMapping("/download/{bucketName}/{objectName}")
    public void obtainFile(
            @PathVariable("bucketName") String bucketName,
            @PathVariable("objectName") String objectName,
            HttpServletResponse response) throws Exception {
        // Tuple2<InputStream, String> tuple2 = Minio.obtainFile(bucketName, objectName);
        File file = new File(path + "/" + bucketName + "/" + objectName);
        if (!file.exists()) {
            response.sendError(404);
            return;
        }
        FileInputStream inputStream = new FileInputStream(file);
        FileResponseUtil.download(response, inputStream, Mime.BINARY, objectName);
    }

    @PostMapping("/upload")
    public R<?> upload(@RequestPart("file") MultipartFile file) throws Exception {
        String filename = file.getOriginalFilename();
        String objectName = IdUtil.fastSimpleUUID();
        if (StrUtil.isNotBlank(filename)) {
            objectName = objectName + "." + FileUtil.extName(filename);
        }
        InputStream inputStream = file.getInputStream();
        IoUtil.copy(inputStream, Files.newOutputStream(Paths.get(path + "/test/" + objectName)));
        inputStream.close();

        return R.success("download/test/" + objectName);
    }
}
