#javascript #electron
Вопрос:
У меня есть электронный проект, и я хочу удалить папку и ее подпапку на ftp-сервере с помощью jsftp. Я создал класс, чтобы упростить его использование.
const ftp = require('jsftp')
class Storage {
constructor() {
this.config = {
host: '127.0.0.1',
user: 'admin',
pass: '',
port: 21
}
this.nextPath = []
}
async makeDir(paths) {
const path = paths.split('/')
let nextPath = []
path.forEach(async (folderName, i) => {
nextPath.push(folderName)
const nextFolder = nextPath.length > 1 ? nextPath.join('/') : folderName
new Promise((resolve) => {
new ftp(this.config).raw('mkd', nextFolder, (err, data) => {
if (err) return console.error(err);
})
return resolve()
})
})
}
async removeDir(path) {
const removeCurrentFolder = async (dir) => {
return await new Promise((resolve) => {
new ftp(this.config).raw('rmd', `${dir}`, (err, data) => {
console.log('folder removed');
if (err) {
return console.error(err);
}
resolve();
})
})
}
const checkIfEmptyFolder = async (dir) => {
console.log(dir);
await new Promise(async (resolve) => {
await this.ls(dir).then(async (fileOrDirectory) => {
if(fileOrDirectory.length) { // check if file or directory is not empty
await Promise.all([...fileOrDirectory].map(async (el) => {
let whichFolder;
if(el.isFolder) {
this.nextPath.push(el.name);
whichFolder = this.nextPath.join('/')
await checkIfEmptyFolder(whichFolder)
}
}))
} else {
await removeCurrentFolder(this.nextPath.join('/')).then(() => { this.nextPath.pop();console.log('done removing'); })
return this.nextPath.length ? await checkIfEmptyFolder(this.nextPath.join('/')) : console.log('done');
}
})
resolve()
})
}
this.nextPath.push(path)
await checkIfEmptyFolder(path)
}
async ls(path) {
let files = []
return new Promise((resolve, reject) => {
new ftp(this.config).ls(path || '.', (err, res) => {
if(err) return reject(err)
res.forEach(file => {
files.push({ name: file.name, isFolder: file.type === 1, size: parseInt(file.size) })
})
return resolve(files)
})
})
}
}
module.exports = new Storage()
Этот класс работает, если папка содержит одну подпапку.
Если папка содержит две или более вложенных папок, появляется эта ошибка: Uncaught Error: read ECONNRESET at TCP.onStreamRead (internal/stream_base_commons.js:209)
Что я делаю не так? Что я пропустил?
Спасибо