Open the-noob opened 7 years ago
I do not think you should close the FTP connection if you are watching it.
Maybe it's not clear from my snippet but I am watching a local folder and then upload any new files being added to that folder. As it might be hours in between the connection needs to be closed.
The problem is to subscribe to the event every time you connected FTP. Bad way.
Check this.
const EventEmitter = require('events');
const fs = require('fs');
const Client = require('../lib/connection');
const config = { ... };
const client = new Client();
const emitter = new EventEmitter();
const watchPath = './__folder/';
const uploadList = [];
const ready = () => client.cwd('/keymaps/', () => emitter.emit('connected'));
client.on('error', (err) => {
console.error('ERROR: ', err);
});
client.on('close', (err) => {
console.error('CLOSE: ', err);
});
const connected = () => {
const upload = (filename, key) => {
client.put(watchPath + filename, filename, (err) => {
if (err) throw err;
console.log('Uploaded: ', filename);
delete uploadList[key]
client.list((err, list) => {
if (err) throw err;
console.log(list);
});
});
};
uploadList.forEach(upload);
client.end();
};
fs.watch(watchPath, { encoding: 'utf8' }, (eventType, filename) => {
if (eventType === 'change') {
uploadList.push(filename);
emitter.emit('upload');
}
});
client.on('ready', ready);
emitter.on('upload', (eventType, filename) => client.connect(config));
emitter.on('connected', connected);
I hope I helped.
I've just used another library that behaves as expected.
can you suggest what another library you have used because I am facing the same problem?
I have a watcher on a folder that is supposed to upload all new files.
Every time a new file is being uploaded the FTP reuploads the previous ones. i.e.