$urls * @return array{attached: array>, failed: array} */ public function attachFromUrls(Post $post, array $urls): array { $attached = []; $failed = []; foreach ($urls as $url) { ($item = $this->attachOne($post, $url)) === null ? $failed[] = $url : $attached[] = $item; } if ($attached !== []) { $post->appendMedia($attached); } return ['attached' => $attached, 'failed' => $failed]; } /** * @return array|null */ private function attachOne(Post $post, string $url): ?array { $download = $this->download($url); if ($download === null) { return null; } try { $type = MediaType::fromMime($download['mime'] ?? ''); if ($type === null || ! in_array($type, $post->allowedMediaTypes(), true)) { return null; } if ($download['bytes'] > $type->maxSizeInBytes()) { return null; } $name = basename(parse_url($url, PHP_URL_PATH) ?? '') ?: 'download.bin'; $media = $post->workspace->addMediaFromPath($download['path'], $name, 'assets'); return [ 'id' => $media->id, 'path' => $media->path, 'url' => $media->url, 'type' => $media->type, 'mime_type' => $media->mime_type, 'original_filename' => $media->original_filename, ]; } finally { @unlink($download['path']); } } /** * Stream the URL to a temp file, aborting once we exceed the largest * configured per-type cap (video). MIME is sniffed from the file's * magic bytes — far more reliable than trusting the upstream * `Content-Type` header (CDNs misconfigure, attackers spoof). * * @return array{path: string, mime: ?string, bytes: int}|null */ private function download(string $url): ?array { $cap = MediaType::Video->maxSizeInBytes(); $temp = tempnam(sys_get_temp_dir(), 'media_'); 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; } $bytes = filesize($temp) ?: 0; if (! $response->successful() || $bytes === 0) { @unlink($temp); return null; } return [ 'path' => $temp, 'mime' => mime_content_type($temp) ?: null, 'bytes' => $bytes, ]; } }