(
userId: string,
searchTerm: string,
)
| 390 | } |
| 391 | |
| 392 | static async searchContents( |
| 393 | userId: string, |
| 394 | searchTerm: string, |
| 395 | ): Promise<{ success: boolean; files: DirectoryFile[] }> { |
| 396 | const rootPath = join(await AppConfig.getFilesRootPath(), userId); |
| 397 | |
| 398 | const files: DirectoryFile[] = []; |
| 399 | |
| 400 | try { |
| 401 | const controller = new AbortController(); |
| 402 | const commandTimeout = setTimeout(() => controller.abort(), 10_000); |
| 403 | |
| 404 | const command = new Deno.Command(`grep`, { |
| 405 | args: [ |
| 406 | `-rHisl`, |
| 407 | `${searchTerm}`, |
| 408 | `.`, // proper cwd is sent below |
| 409 | ], |
| 410 | cwd: rootPath, |
| 411 | signal: controller.signal, |
| 412 | }); |
| 413 | |
| 414 | const { code, stdout, stderr } = await command.output(); |
| 415 | |
| 416 | if (commandTimeout) { |
| 417 | clearTimeout(commandTimeout); |
| 418 | } |
| 419 | |
| 420 | if (code > 1) { |
| 421 | if (stderr) { |
| 422 | throw new Error(new TextDecoder().decode(stderr)); |
| 423 | } |
| 424 | |
| 425 | throw new Error(`Unknown error running "grep"`); |
| 426 | } |
| 427 | |
| 428 | const output = new TextDecoder().decode(stdout); |
| 429 | const matchingFiles = output.split('\n').map((filePath) => filePath.trim()).filter(Boolean); |
| 430 | |
| 431 | for (const relativeFilePath of matchingFiles) { |
| 432 | const fileShares = (await AppConfig.isPublicFileSharingAllowed()) |
| 433 | ? await FileShareModel.getByParentFilePath(userId, relativeFilePath) |
| 434 | : []; |
| 435 | |
| 436 | const stat = await Deno.stat(join(rootPath, relativeFilePath)); |
| 437 | let parentPath = `/${relativeFilePath.replace('./', '/').split('/').slice(0, -1).join('')}/`; |
| 438 | const fileName = relativeFilePath.split('/').pop()!; |
| 439 | |
| 440 | if (parentPath === '//') { |
| 441 | parentPath = '/'; |
| 442 | } |
| 443 | |
| 444 | const file: DirectoryFile = { |
| 445 | user_id: userId, |
| 446 | parent_path: parentPath, |
| 447 | file_name: fileName, |
| 448 | has_write_access: true, |
| 449 | size_in_bytes: stat.size, |
no test coverage detected