package cn.reghao.oss.store.controller;

import cn.reghao.oss.store.service.ObjectMultipartUploadService;
import cn.reghao.jutil.jdk.result.WebResult;
import cn.reghao.oss.storeapi.rest.*;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

/**
 * @author reghao
 * @date 2022-12-08 20:40:55
 */
@RestController
public class ObjectMultipartUploadController {
    private final ObjectMultipartUploadService objectMultipartUploadService;

    public ObjectMultipartUploadController(ObjectMultipartUploadService objectMultipartUploadService) {
        this.objectMultipartUploadService = objectMultipartUploadService;
    }

    @PostMapping(value = "/", params = {"create"}, produces = MediaType.APPLICATION_JSON_VALUE)
    public String create(@Validated UploadPrepare uploadPrepare) {
        UploadPrepareRet uploadPrepareRet = objectMultipartUploadService.prepareUpload(uploadPrepare);
        return WebResult.success(uploadPrepareRet);
    }

    @GetMapping(value = "/", params = {"multipart"}, produces = MediaType.APPLICATION_JSON_VALUE)
    public String getUploadedPart() {
        UploadedPart uploadedPart = new UploadedPart();
        uploadedPart.setSkipUpload(false);
        uploadedPart.setNeedMerge(false);
        uploadedPart.setUrl("");
        uploadedPart.setUploaded(Collections.emptyList());
        return WebResult.success(uploadedPart);
    }

    @PostMapping(value = "/", params = {"multipart"}, produces = MediaType.APPLICATION_JSON_VALUE)
    public String uploadPart(MultipartFile file, @Validated UploadFilePart uploadFilePart) throws Exception {
        UploadFileRet uploadFileRet = objectMultipartUploadService.putFilePart(file.getInputStream(), uploadFilePart);
        return WebResult.success(uploadFileRet);
    }

    @PostMapping(value = "/", params = {"merge"}, produces = MediaType.APPLICATION_JSON_VALUE)
    public String merge() {
        return WebResult.success();
    }
}
