package com.template.controller;

import com.jcraft.jsch.*;
import com.template.api.FileControllerAPI;
import com.template.common.utils.StrUtils;
import com.template.model.request.uploadFileResponse;
import com.template.model.result.CommonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

/**
 * @Author: xwt
 * @Date: 2023/7/5 星期三 9:28
 * @Description: com.template.controller
 * @Version: 1.0
 */
@RestController
public class FileController implements FileControllerAPI {

    private static Logger logger = LoggerFactory.getLogger(FileController.class);


    /**
     * 发布到服务器上后就不需要考虑堡垒机的问题，因为最终域名引导到程序上，
     * 然后程序是在那个服务器内的，所以也就避开了堡垒机验证问题，
     * 只需要把ip换成服务的ip地址即可，内网ip也同样可以访问，因为程序和服务器此时已经是在同一个网段
     * @param file
     * @return
     * @throws Exception
     */
    @Override
    public CommonResult uploadFile(MultipartFile file, String name) throws Exception {
        uploadFileResponse result= new uploadFileResponse();

        name =name == null ? StrUtils.getRandomName(file.getOriginalFilename()) : name;

        String user = "root"; //SFTP服务器用户名
        String password = "Wer@2024!!!..."; // SFTP服务器密码
        String host = "120.53.230.172";  //  SFTP服务器地址
        int port = 22; // SFTP 服务器端口
        String remoteDirPath = "/home/image"; // 远程目录路径
        //从MultipartFile对象中获取流
        InputStream inputStream = null;
        inputStream = file.getInputStream();
        //String downpath="/book/《Python爬虫开发与项目实战》.pdf";
        //String spath="C:/Users/F1339769/Desktop";
        JSch jsch = new JSch();
        Session session = null;
        ChannelSftp channelSftp = null;
        try {
            session = jsch.getSession(user, host, port);
            session.setPassword(password);
            session.setConfig("StrictHostKeyChecking", "no");
            session.connect(30000);
            System.out.println("连接成功");
            channelSftp = (ChannelSftp) session.openChannel("sftp");
            channelSftp.connect();
            // 创建远程目录
            String[] dirArr = remoteDirPath.split("/");
            String path = "";
            for (int i = 0; i < dirArr.length; i++) {
                path = path + "/" + dirArr[i];
                try {
                    channelSftp.cd(path);
                } catch (SftpException e) {
                    channelSftp.mkdir(path);
                    channelSftp.cd(path);
                }
            }
            //  文件上传到远程服务器上
            channelSftp.put(inputStream, name);
            //  从远程服务器上下载文件
            //channelSftp.get(downpath, spath);
        } catch (JSchException e) {
            e.printStackTrace();
        } finally {
            if (channelSftp != null) {
                channelSftp.disconnect();
            }
            if (session != null) {
                session.disconnect();
            }
            result.setFileUrl("https://www.zwywpt.top/homeimage/" + name);
        }

        return CommonResult.ok(result);
    }

}
