Skip to content

Open-source social media scheduler built for AI agents and the teams behind them. Create, schedule and publish posts to every network through MCP or the REST API, or write them yourself in a composer your team will love.

refactor(api): extract inline-media hosting into a HostInlineMedia action

Paulo Castellanoa6bb39cc1a03
diff --git a/app/Actions/Post/HostInlineMedia.php b/app/Actions/Post/HostInlineMedia.php
new file mode 100644
index 000000000..3d0652c7e
--- /dev/null
+++ b/app/Actions/Post/HostInlineMedia.php
@@ -0,0 +1,42 @@
+<?php
+
+declare(strict_types=1);
+
+namespace App\Actions\Post;
+
+use App\Enums\Media\Type as MediaType;
+use App\Models\Workspace;
+use App\Services\Post\MediaAttacher;
+use Illuminate\Validation\ValidationException;
+
+class HostInlineMedia
+{
+ /**
+ * Resolve a post's inline media into hosted media — external URLs are
+ * downloaded and stored; items already on our disk pass through. Rejects
+ * (422) when any URL can't be fetched, so a post is never persisted with
+ * broken media.
+ *
+ * @param array<MediaType> $allowedTypes
+ * @param array<int, array<string, mixed>> $media
+ * @return array<int, array<string, mixed>>
+ *
+ * @throws ValidationException
+ */
+ public static function execute(Workspace $workspace, array $allowedTypes, array $media): array
+ {
+ if ($media === []) {
+ return $media;
+ }
+
+ $result = app(MediaAttacher::class)->resolveInlineMedia($workspace, $allowedTypes, $media);
+
+ if ($result['failed'] !== []) {
+ throw ValidationException::withMessages([
+ 'media' => ['Could not fetch media from URL: '.implode(', ', $result['failed'])],
+ ]);
+ }
+
+ return $result['media'];
+ }
+}
diff --git a/app/Http/Controllers/Api/PostController.php b/app/Http/Controllers/Api/PostController.php
index 3326c5fac..bd020741e 100644
--- a/app/Http/Controllers/Api/PostController.php
+++ b/app/Http/Controllers/Api/PostController.php
@@ -6,6 +6,7 @@ namespace App\Http\Controllers\Api;
use App\Actions\Post\CreatePost;
use App\Actions\Post\DeletePost;
+use App\Actions\Post\HostInlineMedia;
use App\Actions\Post\UpdatePost;
use App\Enums\Media\Type as MediaType;
use App\Enums\Post\Action as PostAction;
@@ -18,7 +19,6 @@ use App\Http\Resources\Api\PostMetricsResource;
use App\Http\Resources\Api\PostPreviewResource;
use App\Http\Resources\Api\PostResource;
use App\Models\Post;
-use App\Models\Workspace;
use App\Services\Post\MediaAttacher;
use App\Support\PostStatusRules;
use Illuminate\Http\JsonResponse;
@@ -54,7 +54,7 @@ class PostController extends Controller
$data = $request->validated();
if (array_key_exists('media', $data)) {
- $data['media'] = $this->hostInlineMedia(
+ $data['media'] = HostInlineMedia::execute(
$workspace,
Post::allowedMediaTypesFor($request->selectedPlatforms()),
$data['media'],
@@ -77,7 +77,7 @@ class PostController extends Controller
$data = $request->validated();
if (array_key_exists('media', $data)) {
- $data['media'] = $this->hostInlineMedia(
+ $data['media'] = HostInlineMedia::execute(
$request->user()->currentWorkspace,
$post->allowedMediaTypes(),
$data['media'],
@@ -99,32 +99,6 @@ class PostController extends Controller
return new PostResource($updated);
}
- /**
- * Download and host external media URLs; reject (422) if one can't be fetched.
- *
- * @param array<MediaType> $allowedTypes
- * @param array<int, array<string, mixed>> $media
- * @return array<int, array<string, mixed>>
- *
- * @throws ValidationException
- */
- private function hostInlineMedia(Workspace $workspace, array $allowedTypes, array $media): array
- {
- if ($media === []) {
- return $media;
- }
-
- $result = app(MediaAttacher::class)->resolveInlineMedia($workspace, $allowedTypes, $media);
-
- if ($result['failed'] !== []) {
- throw ValidationException::withMessages([
- 'media' => ['Could not fetch media from URL: '.implode(', ', $result['failed'])],
- ]);
- }
-
- return $result['media'];
- }
-
public function destroy(Request $request, Post $post): JsonResponse
{
$this->authorize('delete', $post);