$metadata */ public function store( VideoProject $project, string $type, string $contents, string $extension, ?WorkflowStep $step = null, ?string $mimeType = null, array $metadata = [], ): Asset { $version = $this->nextVersion($project, $type, $step); $path = "video-projects/{$project->id}/{$type}/v{$version}.".ltrim($extension, '.'); Storage::put($path, $contents); return $project->assets()->create([ 'workflow_step_id' => $step?->id, 'type' => $type, 'disk_path' => $path, 'mime_type' => $mimeType, 'size_bytes' => strlen($contents), 'version' => $version, 'metadata' => $metadata, ]); } public function nextVersion(VideoProject $project, string $type, ?WorkflowStep $step = null): int { $query = $project->assets()->where('type', $type); if ($step) { $query->where('workflow_step_id', $step->id); } return ((int) $query->max('version')) + 1; } public function contents(Asset $asset): string { return Storage::get($asset->disk_path); } /** * A URL to view/download this asset. Not Storage::url() — the default * 'local' disk has no configured URL driver and throws if asked for * one, so this goes through the authenticated streaming route instead. */ public function url(Asset $asset): ?string { return Storage::exists($asset->disk_path) ? route('video-projects.assets.show', [$asset->video_project_id, $asset->id]) : null; } public function delete(Asset $asset): void { Storage::delete($asset->disk_path); $asset->delete(); } /** * Keep only the newest $keep versions for a (project, type[, step]) * slot; delete the file + record for the rest. Returns how many were * pruned. */ public function pruneOldVersions(VideoProject $project, string $type, ?WorkflowStep $step = null, int $keep = 3): int { $query = $project->assets()->where('type', $type); if ($step) { $query->where('workflow_step_id', $step->id); } $stale = $query->orderByDesc('version')->get()->slice($keep); foreach ($stale as $asset) { $this->delete($asset); } return $stale->count(); } }