filefive
Version:
SFTP/FTP/Amazon S3 client and dual-panel file manager for macOS and Linux
83 lines (82 loc) • 2.88 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const node_path_1 = require("node:path");
const Connection_1 = __importDefault(require("../Connection"));
const URI_1 = require("../utils/URI");
const Queue_1 = require("./Queue");
class RemoveQueue {
constructor(connId, paths, onState, onError, onComplete, watcher) {
this.connId = connId;
this.paths = paths;
this.onState = onState;
this.onError = onError;
this.onComplete = onComplete;
this.watcher = watcher;
this.touched = new Set();
this.stopped = false;
}
async create() {
const paths = this.paths.map(node_path_1.normalize).filter(path => !this.paths.find(ancestor => path.startsWith(ancestor + node_path_1.sep)));
let totalCnt = 0;
let doneCnt = 0;
const ls = (0, Queue_1.lsRemote)(this.connId);
const add = async (path, tree) => {
const parent = (0, node_path_1.dirname)(path);
const file = (await ls(parent))?.find(f => f.name == (0, node_path_1.basename)(path));
if (!file) {
return tree;
}
this.touched.add(parent);
let children = [];
if (file.dir) {
children = (await Promise.all((await ls(file.path) ?? []).map(({ path }) => add(path, [])))).flat(1);
}
totalCnt++;
return [...tree, { path, dir: file.dir, children }];
};
const files = [];
for (const path of paths) {
files.push(...await add(path, []));
}
if (this.stopped) {
return;
}
const rm = async (item) => {
for (const child of item.children) {
await rm(child);
}
const [conn, close] = await Connection_1.default.transmit(this.connId);
if (this.stopped) {
close();
return;
}
try {
await conn.rm(item.path, item.dir);
this.onState({
totalCnt,
doneCnt: ++doneCnt,
totalSize: 0,
doneSize: 0,
pending: 0
});
}
catch (error) {
this.onError(error);
}
finally {
close();
}
};
await Promise.allSettled(files.map(rm));
this.touched.forEach(path => this.watcher.refresh((0, URI_1.createURI)(this.connId, path)));
this.onComplete(this.stopped);
}
stop() {
this.stopped = true;
}
resolve(action, forAll) { }
}
exports.default = RemoveQueue;