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;

@RestController
@RequestMapping("/api/file")
@Api(tags = "文件上传")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class FileController {
    private final TencentCosUtil tencentCosUtil;

    /**
     * 文件上传
     */

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

}
