package ink.wgink.module.file.controller.route.v2;

import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.module.file.service.v2.IFileService;
import ink.wgink.pojo.result.ErrorResult;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @ClassName: FileRouteController
 * @Description: 文件路由
 * @Author: wanggeng
 * @Date: 2022/8/1 15:21
 * @Version: 1.0
 */
@Api(tags = ISystemConstant.API_TAGS_SYSTEM_PREFIX + "文件路由-V2")
@Controller("fileRouteControllerV2")
@RequestMapping(ISystemConstant.ROUTE_PREFIX + "/file/v2")
public class FileRouteController {

    @Autowired
    private IFileService fileService;

    @GetMapping("list")
    public ModelAndView list() {
        ModelAndView modelAndView = new ModelAndView("file/v2/list");
        return modelAndView;
    }

    @ApiOperation(value = "文件重定向下载（浏览器）", notes = "文件重定向下载（适用于浏览器访问图片）接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "isOpen", value = "是否打开,true和false", paramType = "path"),
            @ApiImplicitParam(name = "fileId", value = "文件ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("download/{isOpen}/{fileId}")
    public void download(HttpServletRequest request,
                         HttpServletResponse response,
                         @PathVariable("isOpen") Boolean isOpen,
                         @PathVariable("fileId") String fileId) {
        fileService.downloadRedirect(request, response, isOpen, fileId);
    }

    @ApiOperation(value = "文件下载", notes = "文件下载（用于直接下载）接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fileId", value = "文件ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("download/{fileId}")
    public void download(HttpServletRequest request,
                         HttpServletResponse response,
                         @PathVariable("fileId") String fileId) {
        fileService.download(request, response, fileId);
    }

}
