package com.sqx.modules.file.app;

import cn.hutool.core.util.StrUtil;
import com.sqx.common.utils.IPUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.annotation.Login;
import com.sqx.modules.file.service.NewFileService;
import icu.xuyijie.secureapi.annotation.DecryptParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * @author codingliang
 * @date 2025-10-21
 */
@Api(value = "文件上传(用户端)")
@RestController
@RequestMapping(value = "app/new-file")
@RequiredArgsConstructor
public class AppNewFileController {

    private final NewFileService newFileService;

    @Login
    @ApiOperation("文件上传")
    @RequestMapping(value = "upload", method = RequestMethod.POST)
    public Result upload(@DecryptParam("file") MultipartFile file){
        String src = newFileService.upload(file);
        return Result.success().put("data",src);
    }


    @PostMapping(value = "limit-upload")
    public Result uploadWithToken(HttpServletRequest request, @DecryptParam("file") MultipartFile file){
        String clientIp = IPUtils.getIpAddr(request);

        if (StrUtil.isBlank(clientIp)){
            return Result.error("未获取到客户端IP地址");
        }

        String src = newFileService.upload(clientIp, file);
        return Result.success().put("data", src);
    }
}
