Hey all, newbie to active pieces and part time chaos coder here. I have worked with the active pieces pro gpt to write some javascript code for the code node as I would like to deposit the mp3 generated by Eleven labs text to speech onto a server where I am running some python code with pydub to combine it with various other elements. GPT suggests that the file needs to be downloaded to a temp file before sending it via SFTP. Is this something that is available to users for this purpose? If not what are the workarounds for flows like this? Here is the code I have so far.
// Setup and Installation
const Client = require('ssh2-sftp-client');
const axios = require('axios');
const fs = require('fs');
const path = require('path');
const sftp = new Client();
// Access the mapped keys directly
const user_id = keys.user_id; // mapped from keys
const sesh_id = keys.sesh_id; // mapped from keys
const aff_mp3 = keys.aff_mp3; // mapped from keys
const sftp_user = keys.sftp_user; // mapped from keys
const sftp_pass = keys.sftp_pass; // mapped from keys
// Create Directory Path from Previous Step Data
const newDirectoryName = `${user_id}_${sesh_id}`;
console.log(newDirectoryName); // For debugging purposes
// SFTP Connection and Directory Creation
const remotePath = `/root/affappaudio/audio_files/main/${newDirectoryName}`;
// Temporary local file path
const localFilePath = path.join('/tmp', 'file.mp3');
// Function to download the file
async function downloadFile(url, filePath) {
const response = await axios({
method: 'get',
url: url,
responseType: 'stream'
});
return new Promise((resolve, reject) => {
const writer = fs.createWriteStream(filePath);
response.data.pipe(writer);
writer.on('finish', resolve);
writer.on('error', reject);
});
}
// Main function to handle the SFTP process
async function main() {
try {
// Download the file from the URL
console.log(`Downloading file from ${aff_mp3}`);
await downloadFile(aff_mp3, localFilePath);
console.log('File downloaded successfully');
// Connect to the SFTP server
await sftp.connect({
host: '159.203.189.35',
port: '22',
username: sftp_user,
password: sftp_pass
});
console.log(`Connected to SFTP server. Creating directory: ${remotePath}`);
// Create the directory on the SFTP server
await sftp.mkdir(remotePath, true);
console.log(`Directory created: ${remotePath}. Uploading file.`);
// Upload the file to the SFTP server
await sftp.put(localFilePath, `${remotePath}/file.mp3`);
console.log('File uploaded successfully');
} catch (err) {
console.error('Error during SFTP operation:', err.message);
} finally {
// Clean up and end the SFTP session
await sftp.end();
// Optionally delete the temporary local file
fs.unlinkSync(localFilePath);
}
}
// Execute the main function
main();
Or is this solved using file helper node? I haven’t got either to work yet so looking for direction or a solution if someone else has already achieved this. Thanks.
Has anyone done anything like this before? I can save the files to my gDrive but I would prefer to ftp them to a server. The sftp node only deposits files with the URL in it for some reason rather than the file. I am confused because, as I say, the file lands in the gDrive fine.
I thought I had it then. Does anyone know if I am on the right track? This seems like it should be much easier than this.
On ElevenLabs step, action will return mp3 file URL. Pass this URL in Read File action of Files Helper step and choose output format as Base64 as shown in following image.
Awesome, thank you for the reply. I have made the required inputs available (but removed them for the purpose of the screenshot) as per the screenshot but get an error. "missing ) after argument list"
You have two code variables in snippet, please remove the first one. Also make sure you have added ‘ssh2-sftp-client’ and ‘buffer’ package by clicking “Add NPM package” in code editor.
Ah! This is all making sense now TY. It was throwing errors but I have worked out that the inputs.key shouldn’t be within the quote marks. Just mentioning that in case it is useful for others.