filefive
Version:
SFTP/FTP/Amazon S3 client and dual-panel file manager for macOS and Linux
62 lines (61 loc) • 2.34 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.open = open;
const types_1 = require("./types");
const URI_1 = require("./utils/URI");
const commands_1 = require("./commands");
const node_os_1 = require("node:os");
const node_path_1 = require("node:path");
const promises_1 = require("node:fs/promises");
const FileWatcher_1 = __importDefault(require("./FileWatcher"));
const Connection_1 = __importDefault(require("./Connection"));
const App_1 = __importDefault(require("./App"));
const files = new Map();
const watcher = new FileWatcher_1.default(path => send(path));
async function open(file, onLoad) {
const { path } = (0, URI_1.parseURI)(file);
try {
const tmpDir = await (0, promises_1.mkdtemp)((0, node_path_1.join)((0, node_os_1.tmpdir)(), 'f5-'));
const tmpName = (0, node_path_1.join)(tmpDir, (0, node_path_1.basename)(path));
return commands_1.commands.copy([file], (0, URI_1.createURI)(types_1.LocalFileSystemID, tmpDir), false, null, null, null, () => {
files.set(tmpName, { file, deletion: resetDeletion(tmpName, null), sending: false, changed: false });
watcher.watch(tmpName);
onLoad(tmpName);
});
}
catch (err) {
console.error(err);
}
}
async function send(file) {
const watched = files.get(file);
if (!watched) {
return;
}
if (watched.sending) {
watched.changed = true;
return;
}
watched.sending = true;
watched.deletion = resetDeletion(file, watched.deletion);
const { id, path } = (0, URI_1.parseURI)(watched.file);
const [conn, close] = await Connection_1.default.transmit(id);
await conn.put(file, path);
close();
App_1.default.remoteWatcher.refresh((0, URI_1.createURI)(id, (0, node_path_1.dirname)(path)));
watched.sending = false;
if (watched.changed) {
watched.changed = false;
send(file);
}
}
function resetDeletion(file, current) {
clearTimeout(current);
return setTimeout(() => {
files.delete(file);
(0, promises_1.rm)((0, node_path_1.dirname)(file), { force: true, recursive: true });
}, 1000 * 60 * 60);
}