MCPcopy Create free account
hub / github.com/Make-md/makemd / copyFile

Method copyFile

src/adapters/obsidian/filesystem/filesystem.ts:318–387  ·  view source on GitHub ↗
(path: string, folder: string, newName?: string)

Source from the content-addressed store, hash-verified

316 }
317
318 public async copyFile(path: string, folder: string, newName?: string) {
319
320 const file = await this.getFile(path);
321
322 if (!file) return;
323 newName = newName ? file.extension?.length > 0 ? newName + '.' + file.extension : newName : file.filename;
324
325 let newPath = folder + "/" + newName;
326 let newFile: AFile;
327 if (file.isFolder) {
328
329
330 if (await this.fileExists(newPath)) {
331 const folders = await this.plugin.app.vault.adapter.list(folder).then(g => g.folders)
332 newName = uniqueNameFromString(file.name, folders.map(f => f.split('/').pop()))
333 newPath = folder + "/" + newName;
334 }
335 const recursiveCopy = async (folder: string, newPath: string) => {
336
337 const files = await this.plugin.app.vault.adapter.list(folder);
338 for (const f of files.files) {
339 if (newName != file.name) {
340 if (folder == path && f.split('/').pop() == file.name+ '.md') {
341 await this.plugin.app.vault.adapter.copy(f, newPath + "/" + newName + '.md');
342 continue;
343 }
344
345 }
346 await this.plugin.app.vault.adapter.copy(f, newPath + "/" + f.split('/').pop());
347 }
348 for (const f of files.folders) {
349 await this.createFolder(newPath + "/" + f.split('/').pop());
350 await recursiveCopy(f, newPath + "/" + f.split('/').pop());
351 }
352 }
353 newFile = await this.createFolder(newPath);
354 await recursiveCopy(file.path, newFile.path);
355 } else if (file) {
356 if (!(await this.fileExists(folder))) {
357 await this.createFolder(folder);
358 }
359 try {
360 if (await this.fileExists(newPath)) {
361 const files = await this.plugin.app.vault.adapter.list(folder).then(g => g.files)
362 const newName = uniqueNameFromString(file.name, files.map(f => pathToString(f)))
363 newPath = folder + "/" + newName + "." + file.extension;
364 }
365 await this.plugin.app.vault.adapter.copy((file.path), newPath)
366 } catch(e) {
367 }
368 newFile = tFileToAFile(this.plugin.app.vault.getAbstractFileByPath(newPath));
369 }
370 if (!newFile) return;
371
372 // Deep clone the original cache to avoid shared references
373 // (fixes bug where template changes sync back to new files)
374 const originalCache = this.cache.get(file.path);
375 const clonedCache = originalCache ? JSON.parse(JSON.stringify(originalCache)) : {};

Callers

nothing calls this directly

Calls 12

getFileMethod · 0.95
fileExistsMethod · 0.95
createFolderMethod · 0.95
uniqueNameFromStringFunction · 0.90
pathToStringFunction · 0.90
tFileToAFileFunction · 0.90
popMethod · 0.80
splitMethod · 0.80
parseMethod · 0.80
mapMethod · 0.45
getMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected