* Return direct children (folders + files) of a given folder key. * * @param string $folderKey * @return array */
(string $folderKey, string $sourceId = '')
| 773 | * @return array |
| 774 | */ |
| 775 | public static function getChildren(string $folderKey, string $sourceId = ''): array |
| 776 | { |
| 777 | $folderKey = ($folderKey === '' || $folderKey === '/') ? 'root' : $folderKey; |
| 778 | |
| 779 | $ctx = self::resolveSourceContext($sourceId); |
| 780 | if (empty($ctx['ok'])) { |
| 781 | return [ |
| 782 | 'ok' => false, |
| 783 | 'error' => $ctx['error'] ?? 'invalid_source', |
| 784 | 'message' => $ctx['message'] ?? 'Invalid source.', |
| 785 | ]; |
| 786 | } |
| 787 | |
| 788 | $snapshot = self::loadSnapshot($sourceId); |
| 789 | if ($snapshot === null) { |
| 790 | return [ |
| 791 | 'ok' => false, |
| 792 | 'error' => 'no_snapshot', |
| 793 | ]; |
| 794 | } |
| 795 | |
| 796 | $rootBytes = (int)($snapshot['root_bytes'] ?? 0); |
| 797 | $folders = is_array($snapshot['folders'] ?? null) ? $snapshot['folders'] : []; |
| 798 | $files = is_array($snapshot['files'] ?? null) ? $snapshot['files'] : []; |
| 799 | |
| 800 | // Index folders by key |
| 801 | $folderByKey = []; |
| 802 | foreach ($folders as $f) { |
| 803 | if (!isset($f['key'])) { |
| 804 | continue; |
| 805 | } |
| 806 | $folderByKey[(string)$f['key']] = $f; |
| 807 | } |
| 808 | if (!isset($folderByKey[$folderKey])) { |
| 809 | return [ |
| 810 | 'ok' => false, |
| 811 | 'error' => 'folder_not_found', |
| 812 | ]; |
| 813 | } |
| 814 | |
| 815 | $childrenFolders = []; |
| 816 | foreach ($folders as $f) { |
| 817 | if (!isset($f['parent']) || !isset($f['key'])) { |
| 818 | continue; |
| 819 | } |
| 820 | if ((string)$f['parent'] === $folderKey) { |
| 821 | $bytes = (int)($f['bytes'] ?? 0); |
| 822 | $pct = ($rootBytes > 0) ? ($bytes * 100.0 / $rootBytes) : 0.0; |
| 823 | $childrenFolders[] = [ |
| 824 | 'type' => 'folder', |
| 825 | 'folder' => (string)$f['key'], |
| 826 | 'name' => (string)$f['name'], |
| 827 | 'bytes' => $bytes, |
| 828 | 'files' => (int)($f['files'] ?? 0), |
| 829 | 'dirs' => (int)($f['dirs'] ?? 0), |
| 830 | 'latest_mtime' => (int)($f['latest_mtime'] ?? 0), |
| 831 | 'percentOfTotal' => $pct, |
| 832 | ]; |
no test coverage detected