trypost/app/Http/Requests/Api/Post/AttachExistingAssetRequest.php

86 lines
2.3 KiB
PHP
Raw Permalink Normal View History

feat: Asset Library list, preview, and attach via API and MCP (#282) * feat: list, preview, and attach Asset Library media via API and MCP Let API and MCP clients reuse workspace assets instead of re-uploading, sharing the same scoped query, signed preview, and idempotent attach path. Co-authored-by: Cursor <cursoragent@cursor.com> * Align Asset Library API and MCP with main media patterns. Drop the signed-preview stack, return Storage URLs and PostResource like existing attach flows, and query medias by morph owner instead of getMedia(). Co-authored-by: Cursor <cursoragent@cursor.com> * Paginate workspace assets with the app default page size. Keep list pagination in the action via config('app.pagination.default') instead of a hardcoded API page size. Co-authored-by: Cursor <cursoragent@cursor.com> * Move asset API and MCP input rules into FormRequests. Keep controllers and tools free of inline field validation; MCP tools reuse the request rule definitions. Co-authored-by: Cursor <cursoragent@cursor.com> * Document asset MCP tools with explicit parameters and constraints. Spell out workspace scope, return fields, sibling tools, and rejection cases so agents can call list/get/attach without guessing. Co-authored-by: Cursor <cursoragent@cursor.com> * Harden asset attach against races and keep library metadata on the post. Co-authored-by: Cursor <cursoragent@cursor.com> * Relock the library asset on attach so a deleted file cannot land on the post. Co-authored-by: Cursor <cursoragent@cursor.com> * Document that omitting alt on attach keeps the library alt text. Co-authored-by: Cursor <cursoragent@cursor.com> --------- Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-16 20:08:20 +00:00
<?php
declare(strict_types=1);
namespace App\Http\Requests\Api\Post;
use App\Actions\Media\FindWorkspaceAsset;
use App\Actions\Post\AttachExistingAsset;
use App\Models\Media;
use App\Models\Post;
use App\Support\PostMediaRules;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Validator;
use Symfony\Component\HttpFoundation\Response;
class AttachExistingAssetRequest extends FormRequest
{
private ?Media $resolvedAsset = null;
public function authorize(): bool
{
return true;
}
/**
* @return array<string, mixed>
*/
public function rules(): array
{
return [
'asset_id' => ['required', 'uuid'],
'alt' => ['nullable', 'string', 'max:'.PostMediaRules::ALT_TEXT_MAX_LENGTH],
];
}
public function withValidator(Validator $validator): void
{
$validator->after(function (Validator $validator): void {
if ($validator->errors()->has('asset_id')) {
return;
}
$post = $this->route('post');
$workspace = $this->user()?->currentWorkspace;
if (! $post instanceof Post || $workspace === null) {
$validator->errors()->add('asset_id', AttachExistingAsset::ASSET_NOT_FOUND_MESSAGE);
return;
}
// Cross-tenant posts must 404 from PostPolicy, not 422 from a
// type check against the foreign post's platforms.
if ($post->workspace_id !== $this->user()?->current_workspace_id) {
return;
}
$asset = FindWorkspaceAsset::execute($workspace, (string) $this->input('asset_id'));
if ($asset === null) {
$validator->errors()->add('asset_id', AttachExistingAsset::ASSET_NOT_FOUND_MESSAGE);
return;
}
if (! in_array($asset->type, $post->allowedMediaTypes(), true)) {
$validator->errors()->add(
'asset_id',
AttachExistingAsset::UNSUPPORTED_TYPE_MESSAGE,
);
return;
}
$this->resolvedAsset = $asset;
});
}
public function asset(): Media
{
abort_if($this->resolvedAsset === null, Response::HTTP_NOT_FOUND);
return $this->resolvedAsset;
}
}