(
userId: string,
searchTerm: string,
)
| 313 | } |
| 314 | |
| 315 | static async searchNames( |
| 316 | userId: string, |
| 317 | searchTerm: string, |
| 318 | ): Promise<{ success: boolean; files: DirectoryFile[] }> { |
| 319 | const rootPath = join(await AppConfig.getFilesRootPath(), userId); |
| 320 | |
| 321 | const files: DirectoryFile[] = []; |
| 322 | |
| 323 | try { |
| 324 | const controller = new AbortController(); |
| 325 | const commandTimeout = setTimeout(() => controller.abort(), 10_000); |
| 326 | |
| 327 | const command = new Deno.Command(`find`, { |
| 328 | args: [ |
| 329 | `.`, // proper cwd is sent below |
| 330 | `-type`, |
| 331 | `f`, |
| 332 | `-iname`, |
| 333 | `*${searchTerm}*`, |
| 334 | ], |
| 335 | cwd: rootPath, |
| 336 | signal: controller.signal, |
| 337 | }); |
| 338 | |
| 339 | const { code, stdout, stderr } = await command.output(); |
| 340 | |
| 341 | if (commandTimeout) { |
| 342 | clearTimeout(commandTimeout); |
| 343 | } |
| 344 | |
| 345 | if (code !== 0) { |
| 346 | if (stderr) { |
| 347 | throw new Error(new TextDecoder().decode(stderr)); |
| 348 | } |
| 349 | |
| 350 | throw new Error(`Unknown error running "find"`); |
| 351 | } |
| 352 | |
| 353 | const output = new TextDecoder().decode(stdout); |
| 354 | const matchingFiles = output.split('\n').map((filePath) => filePath.trim()).filter(Boolean); |
| 355 | |
| 356 | for (const relativeFilePath of matchingFiles) { |
| 357 | const fileShares = (await AppConfig.isPublicFileSharingAllowed()) |
| 358 | ? await FileShareModel.getByParentFilePath(userId, relativeFilePath) |
| 359 | : []; |
| 360 | |
| 361 | const stat = await Deno.stat(join(rootPath, relativeFilePath)); |
| 362 | let parentPath = `/${relativeFilePath.replace('./', '/').split('/').slice(0, -1).join('')}/`; |
| 363 | const fileName = relativeFilePath.split('/').pop()!; |
| 364 | |
| 365 | if (parentPath === '//') { |
| 366 | parentPath = '/'; |
| 367 | } |
| 368 | |
| 369 | const file: DirectoryFile = { |
| 370 | user_id: userId, |
| 371 | parent_path: parentPath, |
| 372 | file_name: fileName, |
no test coverage detected