Skip to content

Instantly share code, notes, and snippets.

@scott1028
Last active March 24, 2024 15:55
Show Gist options
  • Save scott1028/08e82e0f11f3f9cc918c5f2a5f4b2c10 to your computer and use it in GitHub Desktop.
Save scott1028/08e82e0f11f3f9cc918c5f2a5f4b2c10 to your computer and use it in GitHub Desktop.
getRemoteFilesByFtp.js
const fs = require('fs');
// https://github.com/mscdex/node-ftp
const ftp = require('ftp');
/**
* fishing rod(chatGPT): https://chat.openai.com/share/f45faeb4-13a1-4221-b98f-9a1428d694a4
*/
// Configuration for the FTP server
const ftpConfig = {
host: '127.0.0.1',
port: 2121, // Default FTP port is 21
user: 'USER',
password: 'USER'
};
// Create a new FTP client
const client = new ftp();
// Connect to the FTP server
client.connect(ftpConfig);
const promisify = (func, pipes = [], root = client) => {
return (...args) => {
return new Promise((res, rej) => {
func.bind(root)(...args, (err, data) => {
if (err) {
rej(err);
} else {
let output = data;
pipes.forEach(pipe => {
output = pipe(output)
})
res(output);
}
})
});
}
}
const list = promisify(client.list);
const pwd = promisify(client.pwd, [value => value.startsWith('/') ? `.${value}` : value]);
const cwd = promisify(client.cwd);
const get = promisify(client.get);
// const put = promisify(client.put); // upload file function
const end = promisify(client.end);
const mkdir = promisify(fs.mkdir);
const saveFile = (path, stream) => stream.pipe(fs.createWriteStream(path));
const listCurrentRemoteDirectory = async () => {
const entries = await list();
for (let i = 0; i < entries.length; i++) {
const entry = entries[i];
if (entry.type === 'd') {
if (entry.name === '..') continue;
if (entry.name === '.') continue;
await enterRemoteDirectory(entry);
} else {
const fullCurrentPath = await pwd();
const saveFilePath = `${fullCurrentPath}/${entry.name}`;
// TOOD: if you want to check file exists or not, add your code here before download it.
const stream = await get(entry.name);
saveFile(saveFilePath, stream);
}
}
};
const enterRemoteDirectory = async (entry) => {
await cwd(entry.name);
const fullCurrentPath = await pwd();
await mkdir(fullCurrentPath, { recursive: true });
await listCurrentRemoteDirectory();
await cwd('..');
}
// When connected successfully
client.on('ready', async () => {
await listCurrentRemoteDirectory();
await end();
});
// When an error occurs
client.on('error', err => {
console.error('FTP Error:', err);
});
// When disconnected from the FTP server
client.on('end', () => {
console.log('Disconnected from FTP server');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment