CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/backend/files/move-files.ts
Views: 687
1
import { getHome } from "./util";
2
import { move, pathExists } from "fs-extra";
3
import { stat } from "node:fs/promises";
4
import { move_file_variations } from "@cocalc/util/delete-files";
5
import { path_split } from "@cocalc/util/misc";
6
import { join } from "path";
7
import getLogger from "@cocalc/backend/logger";
8
9
const log = getLogger("move-files");
10
11
export async function move_files(
12
paths: string[],
13
dest: string, // assumed to be a directory
14
set_deleted: (path: string) => Promise<void>,
15
home?: string,
16
): Promise<void> {
17
const HOME = getHome(home);
18
log.debug({ paths, dest });
19
if (dest == "") {
20
dest = HOME;
21
} else if (!dest.startsWith("/")) {
22
dest = join(HOME, dest);
23
}
24
if (!dest.endsWith("/")) {
25
dest += "/";
26
}
27
const to_move: { src: string; dest: string }[] = [];
28
for (let path of paths) {
29
if (!path.startsWith("/")) {
30
path = join(HOME, path);
31
}
32
const target = dest + path_split(path).tail;
33
log.debug({ path, target });
34
await set_deleted(path);
35
to_move.push({ src: path, dest: target });
36
37
// and the aux files:
38
try {
39
const s = await stat(path);
40
if (!s.isDirectory()) {
41
for (const variation of move_file_variations(path, target)) {
42
if (await pathExists(variation.src)) {
43
await set_deleted(variation.src);
44
to_move.push(variation);
45
}
46
}
47
}
48
} catch (_err) {}
49
}
50
51
for (const x of to_move) {
52
await move(x.src, x.dest);
53
}
54
}
55
56