(
userId: string,
searchTerm: string,
)
| 95 | } |
| 96 | |
| 97 | static async searchNames( |
| 98 | userId: string, |
| 99 | searchTerm: string, |
| 100 | ): Promise<{ success: boolean; directories: Directory[] }> { |
| 101 | const rootPath = join(await AppConfig.getFilesRootPath(), userId); |
| 102 | |
| 103 | const directories: Directory[] = []; |
| 104 | |
| 105 | try { |
| 106 | const controller = new AbortController(); |
| 107 | const commandTimeout = setTimeout(() => controller.abort(), 10_000); |
| 108 | |
| 109 | const command = new Deno.Command(`find`, { |
| 110 | args: [ |
| 111 | `.`, // proper cwd is sent below |
| 112 | `-type`, |
| 113 | `d,l`, // directories and symbolic links |
| 114 | `-iname`, |
| 115 | `*${searchTerm}*`, |
| 116 | ], |
| 117 | cwd: rootPath, |
| 118 | signal: controller.signal, |
| 119 | }); |
| 120 | |
| 121 | const { code, stdout, stderr } = await command.output(); |
| 122 | |
| 123 | if (commandTimeout) { |
| 124 | clearTimeout(commandTimeout); |
| 125 | } |
| 126 | |
| 127 | if (code !== 0) { |
| 128 | if (stderr) { |
| 129 | throw new Error(new TextDecoder().decode(stderr)); |
| 130 | } |
| 131 | |
| 132 | throw new Error(`Unknown error running "find"`); |
| 133 | } |
| 134 | |
| 135 | const output = new TextDecoder().decode(stdout); |
| 136 | const matchingDirectories = output.split('\n').map((directoryPath) => directoryPath.trim()).filter(Boolean); |
| 137 | |
| 138 | for (const relativeDirectoryPath of matchingDirectories) { |
| 139 | const fileShares = (await AppConfig.isPublicFileSharingAllowed()) |
| 140 | ? await FileShareModel.getByParentFilePath(userId, relativeDirectoryPath) |
| 141 | : []; |
| 142 | |
| 143 | const stat = await Deno.stat(join(rootPath, relativeDirectoryPath)); |
| 144 | let parentPath = `/${relativeDirectoryPath.replace('./', '/').split('/').slice(0, -1).join('')}/`; |
| 145 | const directoryName = relativeDirectoryPath.split('/').pop()!; |
| 146 | |
| 147 | if (parentPath === '//') { |
| 148 | parentPath = '/'; |
| 149 | } |
| 150 | |
| 151 | const directorySize = await getDirectorySize(join(rootPath, relativeDirectoryPath)); |
| 152 | |
| 153 | const directory: Directory = { |
| 154 | user_id: userId, |
nothing calls this directly
no test coverage detected