package com.ruoyi.api;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.TencentCosUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * COS 上传兼容接口
 */
@RestController
@RequestMapping("/api/cos")
@Api(tags = "COS文件上传")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class CosController {

    private final TencentCosUtil tencentCosUtil;

    @ApiOperation("COS上传接口")
    @PostMapping("/upload")
    public AjaxResult upload(
            @ApiParam(name = "file", value = "文件", required = true)
            @RequestParam("file") MultipartFile file) throws Exception {
        String uploadUrl = tencentCosUtil.uploadMultipartFile(file, true);
        AjaxResult ajax = AjaxResult.success(uploadUrl);
        ajax.put("url", uploadUrl);
        ajax.put("oldName", file.getOriginalFilename());
        return ajax;
    }
}
