Skip to content

Commit

Permalink
Create sftp_get_to_attachment_newJsch.js
Browse files Browse the repository at this point in the history
  • Loading branch information
pacmano1 authored Jun 22, 2024
1 parent 49a01fd commit 8c6abc7
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions sftp_get_to_attachment_newJsch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Note - stub function here that does work - but adapt ciphers and
// session.setConfig("StrictHostKeyChecking", "no") should not be set at all.

// Import required classes from the JSch library
var JSch = com.jcraft.jsch.JSch;
var Session = com.jcraft.jsch.Session;

function sftpFileTransferGet(serverName, port, userName, password, privateKeyPath, fromFileName, mimeType, base64encode) {
var jsch = new JSch();
var fileTransferred = false;
var session = null;

// Set up the session
if (privateKeyPath) {
jsch.addIdentity(privateKeyPath);
}
session = jsch.getSession(userName, serverName, port);

if (password) {
session.setPassword(password);
}

// Configure the host key verifier (remove in production, as it's not secure)
session.setConfig("StrictHostKeyChecking", "no");

// Configure the cipher algorithms to match the server's supported algorithms you may not need these.
session.setConfig("cipher.s2c", "aes128-ctr,aes192-ctr,aes256-ctr,[email protected],[email protected],[email protected]");
session.setConfig("cipher.c2s", "aes128-ctr,aes192-ctr,aes256-ctr,[email protected],[email protected],[email protected]");

session.connect();

// Create an SFTP channel
var channel = session.openChannel("sftp");
channel.connect();
var sftp = channel;

var inputStream = sftp.get(fromFileName);
var fileContent = org.apache.commons.io.IOUtils.toByteArray(inputStream);
inputStream.close()

addAttachment(fileContent, mimeType, base64encode)

fileTransferred = true; // Mark the file as transferred

// Disconnect and close the SFTP channel
sftp.disconnect();
// Disconnect and close the SSH session
session.disconnect();

return fileTransferred; // Return true if the file was transferred successfully
}

// Using username/password authentication for upload
var serverName1 = 'some.domain.com';
var userName1 = 'username';
var password1 = 'password';
var privateKeyPath1 = null;
var fromFileName1 = '/tmp/junk.txt';

var fileGet = sftpFileTransferGet(serverName1, 22, userName1, password1, privateKeyPath1, fromFileName1, 'text/plain', false);

0 comments on commit 8c6abc7

Please sign in to comment.