package com.njzscloud.common.oss.controller;

import com.njzscloud.common.core.tuple.Tuple2;
import com.njzscloud.common.core.utils.R;
import com.njzscloud.common.oss.service.OSSService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/oss")
@RequiredArgsConstructor
public class OSSController {
    private final OSSService ossService;

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

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

    /**
     * 获取上传链接
     */
    @GetMapping("/obtain_presigned_url")
    public R<Map<String, String>> obtainPresignedUrl(@RequestParam(value = "filename") String filename) {
        return R.success(ossService.obtainPresignedUrl(filename));
    }

    /**
     * 上传文件
     */
    @PostMapping("/upload")
    public R<?> uploadFile(@RequestPart("file") MultipartFile file) {
        return R.success(ossService.uploadFile(file));
    }

    /**
     * 下载文件
     */
    @GetMapping("/download/{bucketName}/{objectName}")
    public void obtainFile(
        @PathVariable("bucketName") String bucketName,
        @PathVariable("objectName") String objectName,
        HttpServletResponse response) {
        ossService.obtainFile(bucketName, objectName, response);
    }
}
