* Build a fresh snapshot of disk usage under UPLOAD_DIR and write it to disk. * * Returns the structured snapshot array (same shape as stored JSON). * * @throws RuntimeException on configuration or IO errors. */
(string $sourceId = '')
| 212 | * @throws RuntimeException on configuration or IO errors. |
| 213 | */ |
| 214 | public static function buildSnapshot(string $sourceId = ''): array |
| 215 | { |
| 216 | $start = microtime(true); |
| 217 | |
| 218 | $ctx = self::resolveSourceContext($sourceId); |
| 219 | if (empty($ctx['ok'])) { |
| 220 | throw new RuntimeException($ctx['message'] ?? 'Invalid source for disk usage scan.'); |
| 221 | } |
| 222 | |
| 223 | $rootPath = (string)($ctx['uploadRoot'] ?? UPLOAD_DIR); |
| 224 | $root = realpath($rootPath); |
| 225 | if ($root === false || !is_dir($root)) { |
| 226 | $msg = ($sourceId !== '') |
| 227 | ? 'Source upload root is not configured correctly.' |
| 228 | : 'Uploads directory is not configured correctly.'; |
| 229 | throw new RuntimeException($msg); |
| 230 | } |
| 231 | $root = rtrim($root, DIRECTORY_SEPARATOR); |
| 232 | |
| 233 | $SKIP = FS::SKIP(); |
| 234 | |
| 235 | // Folder map: key => [ |
| 236 | // 'key' => string, |
| 237 | // 'parent' => string|null, |
| 238 | // 'name' => string, |
| 239 | // 'bytes' => int, |
| 240 | // 'files' => int, |
| 241 | // 'dirs' => int, |
| 242 | // 'latest_mtime' => int |
| 243 | // ] |
| 244 | $folders = []; |
| 245 | |
| 246 | // Root entry |
| 247 | $folders['root'] = [ |
| 248 | 'key' => 'root', |
| 249 | 'parent' => null, |
| 250 | 'name' => 'root', |
| 251 | 'bytes' => 0, |
| 252 | 'files' => 0, |
| 253 | 'dirs' => 0, |
| 254 | 'latest_mtime' => 0, |
| 255 | ]; |
| 256 | |
| 257 | // File records (we may trim to TOP_FILE_LIMIT later) |
| 258 | // Each item: [ |
| 259 | // 'folder' => folderKey, |
| 260 | // 'name' => file name, |
| 261 | // 'path' => "folder/name" or just name if root, |
| 262 | // 'bytes' => int, |
| 263 | // 'mtime' => int |
| 264 | // ] |
| 265 | $files = []; |
| 266 | |
| 267 | $rootLen = strlen($root); |
| 268 | |
| 269 | $dirIter = new RecursiveDirectoryIterator( |
| 270 | $root, |
| 271 | FilesystemIterator::SKIP_DOTS |
no test coverage detected