|
1 | | -import { Notice, TFile } from "obsidian"; |
| 1 | +import { Notice } from "obsidian"; |
2 | 2 | import { LOGGER } from "./Logger"; |
3 | 3 | import pLimit from "p-limit"; |
4 | 4 | import { RowDataType } from "cdm/FolderModel"; |
5 | 5 | import { LocalSettings } from "cdm/SettingsModel"; |
6 | 6 | import { resolveNewFilePath } from "helpers/FileManagement"; |
7 | | -import { postMoveFile } from "helpers/VaultManagement"; |
| 7 | +import { MetadataColumns } from "helpers/Constants"; |
8 | 8 |
|
9 | 9 | const limitMovingFiles = pLimit(1); |
10 | 10 | const limitCreatingFolders = pLimit(1); |
11 | 11 | const limitBatchDeletionAndOrganization = pLimit(1); |
12 | 12 |
|
13 | 13 | export class FileGroupingService { |
14 | | - private static instance: FileGroupingService; |
15 | | - private constructor() { } |
16 | | - public static getInstance(): FileGroupingService { |
17 | | - if (!this.instance) { |
18 | | - this.instance = new FileGroupingService(); |
| 14 | + private static async removeEmptyFoldersRecursively( |
| 15 | + directory: string, |
| 16 | + deletedFolders: Set<string> |
| 17 | + ) { |
| 18 | + let list = await app.vault.adapter.list(directory); |
| 19 | + for (const folder of list.folders) { |
| 20 | + deletedFolders = await FileGroupingService.removeEmptyFoldersRecursively( |
| 21 | + folder, |
| 22 | + deletedFolders |
| 23 | + ); |
19 | 24 | } |
20 | | - return this.instance; |
21 | | - } |
22 | 25 |
|
23 | | - static moveFile = async (folderPath: string, file: TFile): Promise<void> => |
24 | | - limitMovingFiles(() => FileGroupingService._moveFile(folderPath, file)); |
25 | | - private static async _moveFile( |
26 | | - folderPath: string, |
27 | | - file: TFile, |
28 | | - ): Promise<void> { |
29 | | - try { |
30 | | - await FileGroupingService.createFolder(folderPath); |
31 | | - } catch (error) { |
32 | | - LOGGER.error(` moveFile Error: ${error.message} `); |
33 | | - throw error; |
| 26 | + list = await app.vault.adapter.list(directory); |
| 27 | + if (list.files.length === 0 && list.folders.length === 0) { |
| 28 | + await app.vault.adapter.rmdir(directory, false); |
| 29 | + deletedFolders.add(directory); |
34 | 30 | } |
35 | | - const filePath = `${folderPath}/${file.name}`; |
36 | | - await app.fileManager.renameFile(file, filePath); |
| 31 | + |
| 32 | + return deletedFolders; |
37 | 33 | } |
38 | 34 |
|
39 | | - static createFolder = async (folderPath: string): Promise<void> => |
40 | | - limitCreatingFolders(() => FileGroupingService._createFolder(folderPath)); |
41 | | - private static async _createFolder(folderPath: string): Promise<void> { |
42 | | - await app.vault.adapter.exists(folderPath).then(async (exists) => { |
43 | | - if (!exists) { |
44 | | - await app.vault.createFolder(`${folderPath}/`); |
| 35 | + static moveFile = async (folderPath: string, row: RowDataType): Promise<boolean> => |
| 36 | + limitMovingFiles(async () => { |
| 37 | + let file = row.__note__.getFile(); |
| 38 | + const filePath = `${folderPath}/${file.name}`; |
| 39 | + const fileIsAlreadyInCorrectFolder = row.__note__.filepath === filePath; |
| 40 | + if (fileIsAlreadyInCorrectFolder) return false; |
| 41 | + try { |
| 42 | + await FileGroupingService.createFolder(folderPath); |
| 43 | + } catch (error) { |
| 44 | + LOGGER.error(` moveFile Error: ${error.message} `); |
| 45 | + throw error; |
45 | 46 | } |
| 47 | + |
| 48 | + await app.fileManager.renameFile(file, filePath); |
| 49 | + row[MetadataColumns.FILE] = `${file.basename}|${folderPath}/${file.name}`; |
| 50 | + row.__note__.filepath = `${folderPath}/${file.name}`; |
| 51 | + return true; |
| 52 | + }); |
| 53 | + |
| 54 | + static createFolder = async (folderPath: string): Promise<void> => |
| 55 | + limitCreatingFolders(async () => { |
| 56 | + await app.vault.adapter.exists(folderPath).then(async (exists) => { |
| 57 | + if (!exists) { |
| 58 | + await app.vault.createFolder(`${folderPath}/`); |
| 59 | + } |
| 60 | + }); |
46 | 61 | }); |
47 | | - } |
48 | 62 |
|
49 | 63 | static organizeNotesIntoSubfolders = async ( |
50 | 64 | folderPath: string, |
51 | 65 | rows: Array<RowDataType>, |
52 | 66 | ddbbConfig: LocalSettings, |
53 | | - ): Promise<number> => |
54 | | - limitBatchDeletionAndOrganization(() => |
55 | | - FileGroupingService._organizeNotesIntoSubfolders( |
56 | | - folderPath, |
57 | | - rows, |
58 | | - ddbbConfig, |
59 | | - ), |
60 | | - ); |
61 | | - private static async _organizeNotesIntoSubfolders( |
62 | | - folderPath: string, |
63 | | - rows: Array<RowDataType>, |
64 | | - ddbbConfig: LocalSettings, |
65 | | - ): Promise<number> { |
66 | | - try { |
67 | | - if (!ddbbConfig.automatically_group_files) return 0; |
68 | | - let numberOfMovedFiles = 0; |
69 | | - const pathColumns: string[] = (ddbbConfig.group_folder_column || '') |
| 67 | + ): Promise<RowDataType[]> => |
| 68 | + limitBatchDeletionAndOrganization(async () => { |
| 69 | + if (!ddbbConfig.automatically_group_files) return []; |
| 70 | + const movedRows: RowDataType[] = []; |
| 71 | + const pathColumns: string[] = (ddbbConfig.group_folder_column || "") |
70 | 72 | .split(",") |
71 | 73 | .filter(Boolean); |
72 | 74 | for (const row of rows) { |
73 | | - let rowTFile = row.__note__.getFile(); |
74 | | - |
75 | 75 | const newFilePath = resolveNewFilePath({ |
76 | 76 | pathColumns, |
77 | 77 | row, |
78 | 78 | ddbbConfig, |
79 | | - folderPath, |
| 79 | + folderPath |
80 | 80 | }); |
81 | | - // Check if file is already in the correct folder |
82 | | - const auxPath = `${newFilePath}/${rowTFile.name}`; |
83 | | - const fileIsAlreadyInCorrectFolder = row.__note__.filepath === auxPath; |
84 | | - if (fileIsAlreadyInCorrectFolder) continue; |
85 | 81 |
|
86 | | - await FileGroupingService.moveFile(newFilePath, rowTFile); |
87 | | - await postMoveFile({ file: rowTFile, row, newFilePath }); |
88 | | - numberOfMovedFiles++; |
| 82 | + try { |
| 83 | + const fileWasMoved = await FileGroupingService.moveFile(newFilePath, row); |
| 84 | + if (fileWasMoved) { |
| 85 | + movedRows.push(row); |
| 86 | + } |
| 87 | + } catch (error) { |
| 88 | + new Notice(`Error while moving files into subfolders: ${error.message}`, 5000); |
| 89 | + throw error; |
| 90 | + } |
89 | 91 | } |
90 | | - if (numberOfMovedFiles > 0) |
| 92 | + if (movedRows.length > 0) { |
91 | 93 | new Notice( |
92 | | - `Moved ${numberOfMovedFiles} file${numberOfMovedFiles > 1 ? "s" : "" |
| 94 | + `Moved ${movedRows.length} file${ |
| 95 | + movedRows.length > 1 ? "s" : "" |
93 | 96 | } into subfolders`, |
94 | | - 1500, |
| 97 | + 1500 |
95 | 98 | ); |
| 99 | + } |
| 100 | + return movedRows; |
| 101 | + }); |
96 | 102 |
|
97 | | - return numberOfMovedFiles; |
98 | | - } catch (error) { |
99 | | - new Notice( |
100 | | - `Error while moving files into subfolders: ${error.message}`, |
101 | | - 5000, |
102 | | - ); |
103 | | - throw error; |
104 | | - } |
105 | | - } |
106 | | - |
107 | | - private static async removeEmptyFoldersRecursively( |
108 | | - directory: string, |
109 | | - deletedFolders: Set<string>, |
110 | | - ) { |
111 | | - let list = await app.vault.adapter.list(directory); |
112 | | - for (const folder of list.folders) { |
113 | | - deletedFolders = await FileGroupingService.removeEmptyFoldersRecursively( |
114 | | - folder, |
115 | | - deletedFolders, |
116 | | - ); |
117 | | - } |
118 | | - |
119 | | - list = await app.vault.adapter.list(directory); |
120 | | - if (list.files.length === 0 && list.folders.length === 0) { |
121 | | - await app.vault.adapter.rmdir(directory, false); |
122 | | - deletedFolders.add(directory); |
123 | | - } |
124 | | - |
125 | | - return deletedFolders; |
126 | | - } |
127 | | - |
128 | | - static removeEmptyFolders = async ( |
129 | | - directory: string, |
130 | | - ddbbConfig: LocalSettings, |
131 | | - ) => |
132 | | - limitBatchDeletionAndOrganization(() => |
133 | | - FileGroupingService._removeEmptyFolders(directory, ddbbConfig), |
134 | | - ); |
135 | | - private static async _removeEmptyFolders( |
136 | | - directory: string, |
137 | | - ddbbConfig: LocalSettings, |
138 | | - ) { |
139 | | - if(!ddbbConfig.automatically_group_files) return; |
140 | | - if (!ddbbConfig.remove_empty_folders) return; |
141 | | - try { |
142 | | - const removedDirectories = |
143 | | - await FileGroupingService.removeEmptyFoldersRecursively( |
144 | | - directory, |
145 | | - new Set(), |
146 | | - ); |
147 | | - const n = removedDirectories.size; |
148 | | - if (n > 0) { |
149 | | - const message = `Removed ${n} empty director${n === 0 || n > 1 ? "ies" : "y" |
150 | | - }`; |
151 | | - new Notice(message, 1500); |
| 103 | + static removeEmptyFolders = async (directory: string, ddbbConfig: LocalSettings) => |
| 104 | + limitBatchDeletionAndOrganization(async () => { |
| 105 | + if (!ddbbConfig.automatically_group_files) return; |
| 106 | + if (!ddbbConfig.remove_empty_folders) return; |
| 107 | + try { |
| 108 | + const removedDirectories = |
| 109 | + await FileGroupingService.removeEmptyFoldersRecursively(directory, new Set()); |
| 110 | + const n = removedDirectories.size; |
| 111 | + if (n > 0) { |
| 112 | + const message = `Removed ${n} empty director${n === 0 || n > 1 ? "ies" : "y"}`; |
| 113 | + new Notice(message, 1500); |
| 114 | + } |
| 115 | + } catch (error) { |
| 116 | + new Notice(`Error while removing empty folders: ${error.message}`, 5000); |
| 117 | + throw error; |
152 | 118 | } |
153 | | - } catch (error) { |
154 | | - new Notice(`Error while removing empty folders: ${error.message}`, 5000); |
155 | | - throw error; |
156 | | - } |
157 | | - } |
| 119 | + }); |
158 | 120 | } |
0 commit comments