Path: blob/master/src/packages/backend/files/rename-file.ts
1447 views
import { getHome } from "./util";1import { move, pathExists } from "fs-extra";2import { stat } from "node:fs/promises";3import { move_file_variations } from "@cocalc/util/delete-files";4import { join } from "path";5import getLogger from "@cocalc/backend/logger";67const log = getLogger("rename-file");89export async function rename_file(10src: string,11dest: string,12set_deleted: (path: string) => Promise<void>,13home?: string,14): Promise<void> {15if (src == dest) return; // no-op16const HOME = getHome(home);17if (!src.startsWith("/")) {18src = join(HOME, src);19}20if (!dest.startsWith("/")) {21dest = join(HOME, dest);22}23log.debug({ src, dest, home, HOME });24await set_deleted(src); // todo: later may have a set_moved...25const to_move: { src: string; dest: string }[] = [{ src, dest }];2627try {28const s = await stat(src);29if (!s.isDirectory()) {30for (const variation of move_file_variations(src, dest)) {31if (await pathExists(variation.src)) {32await set_deleted(variation.src);33to_move.push(variation);34}35}36}37} catch (_err) {}3839for (const x of to_move) {40await move(x.src, x.dest);41}42}434445