Files
muninn/app/Support/AssetStorage/AssetStorage.php
T
m105andClaude Sonnet 5 c249a14d78 Add storyboard/video-gen/video-editing handlers and asset previews (#37-39, #45, #46)
StoryboardHandler (#37): breaks the project's script into a numbered
scene-by-scene breakdown (description + image prompt per scene) via
OpenAI in JSON mode (added json_mode support to OpenAiDriver for
reliable structured output), stored as a JSON asset.

VideoGenerationHandler (#38): generates one still image per storyboard
scene via StabilityAI — the scene *visuals*, not motion/video clips
(no text-to-video model integrated; see the task comment for why).

VideoEditingHandler (#39): the step that actually produces a video
file — assembles the scene stills into an MP4 via ffmpeg's concat
demuxer, timed to the voiceover audio's duration (probed via ffprobe)
or a fixed per-scene duration if there's no audio, then muxes the
audio track in. Verified against the real ffmpeg/ffprobe binaries on
this box, not just Process::fake().

Asset previews (#45, #46): a reusable partial renders each asset
inline by type — image/audio/video get real playback elements, text/
JSON gets a truncated <pre> block, everything else a download link.
Wired into both the awaiting_approval step view (so approving/
rejecting is an actual content review, not just a word count) and a
general project assets grid.

Also fixes a latent bug in AssetStorage::url(), found while wiring up
previews: it called Storage::url() on the default 'local' disk, which
has no configured URL driver and throws in production — only worked
before because Storage::fake() is more permissive than the real local
driver. Now serves assets through a new authenticated streaming route
instead (VideoProjectAssetController).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-06 21:15:31 +03:00

102 lines
2.9 KiB
PHP

<?php
namespace App\Support\AssetStorage;
use App\Models\Asset;
use App\Models\VideoProject;
use App\Models\WorkflowStep;
use Illuminate\Support\Facades\Storage;
/**
* Stores generated content (scripts, images, video, audio) on disk and
* records it as an Asset. Versioned per (project, type[, step]) slot —
* storing again for the same slot doesn't overwrite, it adds the next
* version, so a rejected/retried step's earlier output stays around.
*/
class AssetStorage
{
/**
* @param array<string, mixed> $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();
}
}