validate([ 'post_id' => ['required', 'uuid'], 'upload_token' => ['required', 'uuid'], 'alt' => ['nullable', 'string', 'max:'.PostMediaRules::ALT_TEXT_MAX_LENGTH], ]); $workspaceId = $request->user()?->current_workspace_id; $post = $workspaceId ? Post::where('workspace_id', $workspaceId)->find(data_get($validated, 'post_id')) : null; if (! $post) { return Response::error('Post not found.'); } if ($denied = $this->denyUnlessCan($request, 'update', $post, 'Not authorized to update this post.')) { return $denied; } $media = Media::query() ->where('upload_token', data_get($validated, 'upload_token')) ->where('mediable_type', (new Workspace)->getMorphClass()) ->where('mediable_id', $workspaceId) ->first(); if (! $media) { return Response::error('Upload not found.'); } if (! in_array($media->type, $post->allowedMediaTypes(), true)) { return Response::error('No enabled platform on this post accepts this media type.'); } $item = [ 'id' => $media->id, 'path' => $media->path, 'url' => $media->url, 'type' => $media->type, 'mime_type' => $media->mime_type, 'original_filename' => $media->original_filename, ]; if (($alt = data_get($validated, 'alt')) !== null && $media->isImage()) { $item['meta'] = ['alt_text' => $alt]; } $post->appendMedia([$item]); $post->refresh()->load(['postPlatforms.socialAccount', 'labels']); return Response::structured([ 'post' => (new PostResource($post))->resolve(), ]); } public function schema(JsonSchema $schema): array { return [ 'post_id' => $schema->string()->required()->description('UUID of the post to attach the uploaded media to.'), 'upload_token' => $schema->string()->required()->description('upload_token returned by RequestMediaUploadTool, after the user has POSTed the file to the upload_url.'), 'alt' => $schema->string()->description('Optional accessibility alt text for the media (applies to images).'), ]; } }