MCPcopy Create free account
hub / github.com/MCSManager/MCSManager / getPath

Method getPath

daemon/src/entity/file_writer.ts:37–77  ·  view source on GitHub ↗
(cwd: string, dir: string, filename: string, overwrite: boolean)

Source from the content-addressed store, hash-verified

35 }
36
37 static async getPath(cwd: string, dir: string, filename: string, overwrite: boolean) {
38 const fileManager = new FileManager(cwd);
39
40 const ext = path.extname(filename);
41 const basename = path.basename(filename, ext);
42
43 let tempFileSaveName = basename + ext;
44 let counter = 1;
45
46 const checkFile = async (name: string) => {
47 const absolutePath = fileManager.toAbsolutePath(path.normalize(path.join(dir, name)));
48 const isLock = await lockfile
49 .check(absolutePath)
50 .then((isLock) => isLock)
51 .catch(() => false);
52 const isAccess = await fs
53 .access(absolutePath)
54 .then(() => true)
55 .catch(() => false);
56 return isAccess && !isLock && !overwrite;
57 };
58
59 while (await checkFile(tempFileSaveName)) {
60 if (counter == 1) {
61 tempFileSaveName = `${basename}-copy${ext}`;
62 } else {
63 tempFileSaveName = `${basename}-copy-${counter}${ext}`;
64 }
65 counter++;
66 if (counter > 100) {
67 throw new Error("Access denied: File name already exists!");
68 }
69 }
70
71 const fileSaveRelativePath = path.normalize(path.join(dir, tempFileSaveName));
72
73 if (!fileManager.checkPath(fileSaveRelativePath))
74 throw new Error("Access denied: Invalid destination");
75
76 return fileManager.toAbsolutePath(fileSaveRelativePath);
77 }
78
79 async init() {
80 if (this.fd != null) return;

Callers 1

http_router.tsFile · 0.80

Calls 2

checkPathMethod · 0.95
toAbsolutePathMethod · 0.95

Tested by

no test coverage detected