* Retrieves the list of files in a given folder, enriched with metadata, along with global tags. * * @param string $folder The folder name (e.g., "root" or a subfolder). * @return array Returns an associative array with keys "files" and "globalTags". */
(string $folder, array $options = [])
| 3580 | * @return array Returns an associative array with keys "files" and "globalTags". |
| 3581 | */ |
| 3582 | public static function getFileList(string $folder, array $options = []): array |
| 3583 | { |
| 3584 | // --- caps for safe inlining --- |
| 3585 | if (!defined('LISTING_CONTENT_BYTES_MAX')) { |
| 3586 | define('LISTING_CONTENT_BYTES_MAX', 8192); // 8 KB snippet |
| 3587 | } |
| 3588 | if (!defined('INDEX_TEXT_BYTES_MAX')) { |
| 3589 | define('INDEX_TEXT_BYTES_MAX', 5 * 1024 * 1024); // only sample files ≤ 5 MB |
| 3590 | } |
| 3591 | |
| 3592 | $includeContent = !empty($options['includeContent']); |
| 3593 | $pageSizeRaw = isset($options['pageSize']) ? (int)$options['pageSize'] : 0; |
| 3594 | $pageSize = ($pageSizeRaw > 0) ? max(1, min(500, $pageSizeRaw)) : 0; |
| 3595 | $cursorRaw = isset($options['cursor']) ? trim((string)$options['cursor']) : ''; |
| 3596 | $cursorOffset = (ctype_digit($cursorRaw)) ? (int)$cursorRaw : 0; |
| 3597 | if ($cursorOffset < 0) { |
| 3598 | $cursorOffset = 0; |
| 3599 | } |
| 3600 | |
| 3601 | $sortByRaw = trim((string)($options['sortBy'] ?? '')); |
| 3602 | $sortBy = strtolower($sortByRaw); |
| 3603 | if (!in_array($sortBy, ['name', 'modified', 'uploaded', 'size', 'uploader'], true)) { |
| 3604 | $sortBy = ''; |
| 3605 | } |
| 3606 | if ($sortBy === '' && $pageSize > 0) { |
| 3607 | $sortBy = 'modified'; |
| 3608 | } |
| 3609 | $sortDir = strtolower(trim((string)($options['sortDir'] ?? ''))); |
| 3610 | if ($sortDir !== 'asc' && $sortDir !== 'desc') { |
| 3611 | $sortDir = ($sortBy === 'name' || $sortBy === 'uploader') ? 'asc' : 'desc'; |
| 3612 | } |
| 3613 | $uploaderExact = trim((string)($options['uploaderExact'] ?? '')); |
| 3614 | |
| 3615 | $folder = trim($folder) ?: 'root'; |
| 3616 | $storage = self::storage(); |
| 3617 | $activeSourceId = class_exists('SourceContext') ? SourceContext::getActiveId() : ''; |
| 3618 | $parseUploadedTs = static function (string $v): int { |
| 3619 | $v = trim($v); |
| 3620 | if ($v === '' || strcasecmp($v, 'Unknown') === 0) { |
| 3621 | return 0; |
| 3622 | } |
| 3623 | $ts = @strtotime($v); |
| 3624 | return ($ts === false) ? 0 : (int)$ts; |
| 3625 | }; |
| 3626 | |
| 3627 | // Determine the target directory. |
| 3628 | $baseDir = rtrim(self::uploadRoot(), '/\\'); |
| 3629 | if (strtolower($folder) !== 'root') { |
| 3630 | $directory = $baseDir . DIRECTORY_SEPARATOR . $folder; |
| 3631 | } else { |
| 3632 | $directory = $baseDir; |
| 3633 | } |
| 3634 | |
| 3635 | // Validate folder. |
| 3636 | if (strtolower($folder) !== 'root' && !preg_match(REGEX_FOLDER_NAME, $folder)) { |
| 3637 | return ["error" => "Invalid folder name."]; |
| 3638 | } |
| 3639 |
no test coverage detected