package cn.reghao.tnb.file.app.controller;

import cn.reghao.jutil.web.WebResult;
import cn.reghao.tnb.common.auth.AuthUser;
import cn.reghao.tnb.file.app.model.dto.GetSignedUrl;
import cn.reghao.tnb.file.app.model.vo.StsToken;
import cn.reghao.tnb.file.app.service.AliyunService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author reghao
 * @date 2024-08-30 14:31:14
 */
@Tag(name = "阿里云接口")
@RestController
@RequestMapping("/api/file/aliyun")
public class AliyunController {
    private final AliyunService aliyunService;

    public AliyunController(AliyunService aliyunService) {
        this.aliyunService = aliyunService;
    }

    @AuthUser
    @Operation(summary = "获取阿里云 sts token", description = "N")
    @PostMapping(value = "/sts_token", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getUploadStore() {
        StsToken stsToken = aliyunService.getStsToken();
        return WebResult.success(stsToken);
    }

    @AuthUser
    @Operation(summary = "获取阿里云 oss 签名 url", description = "N")
    @PostMapping(value = "/signed_url", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getSignedUrl(@RequestBody @Validated GetSignedUrl getSignedUrl) {
        String signedUrl = aliyunService.getSignedUrl(getSignedUrl.getObjectName());
        return WebResult.success(signedUrl);
    }
}
