data}/{$kind}/{$folder}"); } } $projects = new Store("{$this->data}/projects", $owner); foreach ($projects->listOf($owner) as $project) { $bytes += (int) @filesize("{$this->data}/projects/{$project['id']}.json"); $bytes += self::sizeOf("{$this->data}/snapshots/{$project['id']}"); } return $bytes; } /** Why `$adding` more bytes do not fit, or null when they do (or add nothing). */ public function refusal(string $owner, string $folder, int $adding): ?string { if ($adding <= 0) { return null; } $used = $this->usage($owner, $folder); if ($used + $adding <= $this->limit) { return null; } return sprintf( 'Not enough space: this account uses %s of its %s', self::megabytes($used), self::megabytes($this->limit), ); } private static function megabytes(int $bytes): string { return number_format($bytes / 1024 / 1024, $bytes < 10 * 1024 * 1024 ? 1 : 0, '.', '') . ' MB'; } private static function sizeOf(string $dir): int { if (!is_dir($dir)) { return 0; } $bytes = 0; $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS)); foreach ($files as $file) { if ($file->isFile()) { $bytes += $file->getSize(); } } return $bytes; } }