package cd.casic.module.terminal.controller;

import cd.casic.module.terminal.service.TerminalSftpService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;
import jakarta.servlet.http.HttpServletResponse;

/**
 * SFTP 操作服务 api
 *
 * @author Jiahang Li
 * @version 1.0.0
 * @since 2023-12-26 22:09
 */
@Tag(name = "asset - SFTP 操作服务")
@Slf4j
@Validated
@RestController
@RequestMapping("/asset/terminal-sftp")
public class TerminalSftpController {

    @Resource
    private TerminalSftpService terminalSftpService;


    @GetMapping("/get-content")
    @Operation(summary = "获取文件内容")
    @Parameter(name = "token", description = "token", required = true)
    public void getFileContentByToken(@RequestParam("token") String token, HttpServletResponse response) throws Exception {
        terminalSftpService.getFileContentByToken(token, response);
    }

    @PostMapping("/set-content")
    @Operation(summary = "设置文件内容")
    @Parameter(name = "token", description = "token", required = true)
    @Parameter(name = "file", description = "file", required = true)
    public Boolean setFileContentByToken(@RequestParam("token") String token,
                                         @RequestParam("file") MultipartFile file) throws Exception {
        terminalSftpService.setFileContentByToken(token, file);
        return true;
    }

    @PermitAll
    @GetMapping("/download")
    @Operation(summary = "下载文件")
    @Parameter(name = "channelId", description = "channelId", required = true)
    @Parameter(name = "transferToken", description = "transferToken", required = true)
    public StreamingResponseBody downloadWithTransferToken(@RequestParam("channelId") String channelId,
                                                           @RequestParam("transferToken") String transferToken,
                                                           HttpServletResponse response) {
        return terminalSftpService.downloadWithTransferToken(channelId, transferToken, response);
    }

}

