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.
PHP ai-agentsblueskyfacebookinstagramlaravellinkedinmastodonmcpmcp-serveropen-sourcerest-apiself-hostedsocial-mediasocial-media-managementsocial-media-schedulerthreadstiktokvuejsxyoutube
refactor: move post-state concerns out of MediaAttacher
Paulo Castellano335180d7a158
diff --git a/app/Models/Post.php b/app/Models/Post.phpindex 77328c361..04f0c9fee 100644--- a/app/Models/Post.php+++ b/app/Models/Post.php@@ -5,6 +5,7 @@ declare(strict_types=1);namespace App\Models;use App\DataTransferObjects\MediaItem;+use App\Enums\Media\Type;use App\Enums\Post\Status as PostStatus;use Database\Factories\PostFactory;use Illuminate\Database\Eloquent\Builder;@@ -16,6 +17,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;use Illuminate\Database\Eloquent\Relations\BelongsToMany;use Illuminate\Database\Eloquent\Relations\HasMany;use Illuminate\Support\Collection;+use Illuminate\Support\Facades\DB;class Post extends Model{@@ -129,4 +131,50 @@ class Post extends Model{$this->update(['status' => PostStatus::Failed]);}++ /**+ * MediaTypes accepted by this post — the intersection of what every+ * enabled platform allows. With no platform enabled, accept anything.+ *+ * @return array<Type>+ */+ public function allowedMediaTypes(): array+ {+ $platforms = $this->postPlatforms()+ ->where('enabled', true)+ ->with('socialAccount')+ ->get()+ ->pluck('socialAccount.platform')+ ->filter();++ if ($platforms->isEmpty()) {+ return Type::cases();+ }++ $sets = $platforms+ ->map(fn ($platform) => array_map(fn ($type) => $type->value, $platform->allowedMediaTypes()))+ ->all();++ return array_map(+ Type::from(...),+ array_values(array_intersect(...$sets)),+ );+ }++ /**+ * Append items to the JSON `media` column under a row lock so+ * concurrent writers don't overwrite each other's appends.+ *+ * @param array<int, array<string, mixed>> $items+ */+ public function appendMedia(array $items): void+ {+ DB::transaction(function () use ($items): void {+ $fresh = static::whereKey($this->id)->lockForUpdate()->first();+ $fresh->update([+ 'media' => collect($fresh->media ?? [])->concat($items)->all(),+ ]);+ $this->setRawAttributes($fresh->getAttributes(), true);+ });+ }}diff --git a/app/Services/Post/MediaAttacher.php b/app/Services/Post/MediaAttacher.phpindex 13a6160d7..4698961e9 100644--- a/app/Services/Post/MediaAttacher.php+++ b/app/Services/Post/MediaAttacher.php@@ -6,36 +6,17 @@ namespace App\Services\Post;use App\Enums\Media\Type as MediaType;use App\Models\Post;-use App\Models\Workspace;-use Illuminate\Support\Facades\DB;use Illuminate\Support\Facades\Http;use RuntimeException;/**- * Downloads public URLs and attaches them as media to a post. Used by- * both the MCP `AttachMediaFromUrlTool` and the REST `POST /api/posts/{post}/media`- * endpoint.+ * Downloads public URLs and attaches them as media to a post — used by+ * the MCP `AttachMediaFromUrlTool` and the REST attach-media endpoint.*- * URL syntax (`url:http,https`) and DNS resolvability (`active_url`) are- * enforced at the request validation layer. SSRF defense beyond that is- * the responsibility of network-level egress controls in production.- *- * Flow per URL:- * 1. Stream the body to a temp file via Http::sink + a progress- * callback that aborts once we exceed the largest configured- * per-type cap (video = 1 GB by default, see config/trypost.php).- * 2. Resolve the MediaType from the response's Content-Type via- * `MediaType::fromMime()`. Reject if the type isn't accepted by- * the intersection of platforms enabled on the post.- * 3. Enforce the per-type cap (`MediaType::Image->maxSizeInBytes()`- * vs `Video`) — a 100 MB jpeg is rejected even though we- * streamed up to the video cap.- * 4. Hand off to `Workspace::addMediaFromPath()` so storage path,- * MIME re-detection, image normalization, and the Media row stay- * in one place (same path as the web upload flow).- * 5. Append the resulting media item to the post's `media[]` JSON- * column under a row lock so concurrent attach calls don't clobber- * each other.+ * URL syntax + DNS resolvability are validated at the request layer+ * (`url:http,https`, `active_url`). Locking, intersection of accepted+ * media types, and the JSON-column merge live on the Post model so this+ * class can stay focused on the network → MIME-validate → handoff flow.*/class MediaAttacher{@@ -45,78 +26,46 @@ class MediaAttacher*/public function attachFromUrls(Post $post, array $urls): array{- $allowedTypes = $this->allowedMediaTypesFor($post);-$attached = [];$failed = [];foreach ($urls as $url) {- $item = $this->processOne($post->workspace, $url, $allowedTypes);-- if ($item === null) {- $failed[] = $url;-- continue;- }-- $attached[] = $item;+ ($item = $this->attachOne($post, $url)) === null+ ? $failed[] = $url+ : $attached[] = $item;}if ($attached !== []) {- $this->mergeIntoPostMedia($post, $attached);+ $post->appendMedia($attached);}return ['attached' => $attached, 'failed' => $failed];}/**- * @param array<MediaType> $allowedTypes* @return array<string, mixed>|null*/- private function processOne(Workspace $workspace, string $url, array $allowedTypes): ?array+ private function attachOne(Post $post, string $url): ?array{- // Use the largest configured per-type cap as the streaming-abort- // threshold; the actual per-type limit is checked below once we- // know the MIME.- $streamCap = MediaType::Video->maxSizeInBytes();+ $download = $this->download($url);- $temp = tempnam(sys_get_temp_dir(), 'media_');+ if ($download === null) {+ return null;+ }try {- $response = Http::timeout(20)- ->sink($temp)- ->withOptions([- 'allow_redirects' => false,- 'progress' => static function ($total, $downloaded) use ($streamCap): void {- if ($downloaded > $streamCap) {- throw new RuntimeException('exceeded max bytes');- }- },- ])- ->get($url);-- $bytes = filesize($temp) ?: 0;+ $type = MediaType::fromMime($download['mime'] ?? '');- if (! $response->successful() || $bytes === 0) {+ if ($type === null || ! in_array($type, $post->allowedMediaTypes(), true)) {return null;}- $mime = trim(explode(';', (string) $response->header('Content-Type'))[0]);- $type = MediaType::fromMime($mime);-- if ($type === null || ! in_array($type, $allowedTypes, true)) {+ if ($download['bytes'] > $type->maxSizeInBytes()) {return null;}- // Per-type size enforcement. Image is 10 MB even though we- // streamed up to the video cap, so a 100 MB jpeg is rejected- // here before we persist it.- if ($bytes > $type->maxSizeInBytes()) {- return null;- }-- $originalFilename = basename(parse_url($url, PHP_URL_PATH) ?? '') ?: 'download.bin';- $media = $workspace->addMediaFromPath($temp, $originalFilename, 'assets');+ $name = basename(parse_url($url, PHP_URL_PATH) ?? '') ?: 'download.bin';+ $media = $post->workspace->addMediaFromPath($download['path'], $name, 'assets');return ['id' => $media->id,@@ -126,55 +75,55 @@ class MediaAttacher'mime_type' => $media->mime_type,'original_filename' => $media->original_filename,];- } catch (RuntimeException) {- return null;} finally {- @unlink($temp);+ @unlink($download['path']);}}/**- * Lock-then-merge so concurrent attach calls don't overwrite each- * other's appended items in the JSON `media` column.+ * Stream the URL to a temp file, aborting once we exceed the largest+ * configured per-type cap (video). The actual per-type limit is+ * enforced by the caller after we know the MIME.*- * @param array<int, array<string, mixed>> $attached+ * @return array{path: string, mime: ?string, bytes: int}|null*/- private function mergeIntoPostMedia(Post $post, array $attached): void+ private function download(string $url): ?array{- DB::transaction(function () use ($post, $attached): void {- $fresh = Post::whereKey($post->id)->lockForUpdate()->first();- $fresh->update([- 'media' => collect($fresh->media ?? [])->concat($attached)->all(),- ]);- $post->setRawAttributes($fresh->getAttributes(), true);- });- }+ $cap = MediaType::Video->maxSizeInBytes();+ $temp = tempnam(sys_get_temp_dir(), 'media_');- /**- * Intersection of allowed media types across platforms enabled on- * the post. With no enabled platform, accept anything we support.- *- * @return array<MediaType>- */- private function allowedMediaTypesFor(Post $post): array- {- $enabledPlatforms = $post->postPlatforms()- ->where('enabled', true)- ->with('socialAccount')- ->get()- ->pluck('socialAccount.platform')- ->filter();-- if ($enabledPlatforms->isEmpty()) {- return [MediaType::Image, MediaType::Video];+ try {+ $response = Http::timeout(20)+ ->sink($temp)+ ->withOptions([+ 'allow_redirects' => false,+ 'progress' => static function ($total, $downloaded) use ($cap): void {+ if ($downloaded > $cap) {+ throw new RuntimeException('exceeded max bytes');+ }+ },+ ])+ ->get($url);+ } catch (RuntimeException) {+ @unlink($temp);++ return null;}- $sets = $enabledPlatforms- ->map(fn ($platform) => array_map(fn ($type) => $type->value, $platform->allowedMediaTypes()))- ->all();+ $bytes = filesize($temp) ?: 0;++ if (! $response->successful() || $bytes === 0) {+ @unlink($temp);++ return null;+ }- $intersection = array_values(array_intersect(...$sets));+ $mime = trim(explode(';', (string) $response->header('Content-Type'))[0]);- return array_map(fn ($value) => MediaType::from($value), $intersection);+ return [+ 'path' => $temp,+ 'mime' => $mime !== '' ? $mime : null,+ 'bytes' => $bytes,+ ];}}