package cd.casic.ci.api;

import cd.casic.ci.process.process.service.sftpFile.SftpFileService;
import cd.casic.framework.commons.pojo.CommonResult;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * @author HopeLi
 * @version v1.0
 * @ClassName SftpFileController
 * @Date: 2025/7/1 9:03
 * @Description:
 */
@RestController
@RequestMapping("/sftpFile")
public class SftpFileController {

    @Resource
    private SftpFileService sftpFileService;


    @PostMapping("/upload")
    public CommonResult<String> uploadFile(
            @RequestParam String remoteDir,
            @RequestParam String remoteFileName,
            @RequestParam MultipartFile file) {


        String uploadFilePath = sftpFileService.uploadFile(
                file, remoteDir, remoteFileName);

        return CommonResult.success(uploadFilePath);
    }

    @GetMapping("/download")
    public void downloadFile(
            @RequestParam String remoteFilePath,
            HttpServletResponse response) {
        remoteFilePath = URLDecoder.decode(remoteFilePath,StandardCharsets.UTF_8);
        sftpFileService.downloadFile(
                remoteFilePath, response);
    }

    @PostMapping("/download/zip")
    public void downloadFilesAsZip(
            @RequestBody List<String> remoteFilePaths,
            @RequestParam String zipFileName,
            HttpServletResponse response) {

        sftpFileService.downloadFilesAsZip(
                remoteFilePaths, zipFileName, response);
    }


}
