diff --git a/app/Services/MediaCacher.php b/app/Services/MediaCacher.php index 6829797..29d9438 100644 --- a/app/Services/MediaCacher.php +++ b/app/Services/MediaCacher.php @@ -2,6 +2,7 @@ namespace App\Services; +use Illuminate\Support\Facades\File; use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Storage; @@ -74,23 +75,38 @@ abstract class MediaCacher private function download(string $url, string $pathWithoutExtension): ?string { + // Stream the response straight to disk via Guzzle's `sink` option + // instead of buffering the whole body in memory — a several-MB + // video/reel can easily exceed PHP's default memory_limit if + // fetched the normal way (Http::get()->body() reads it all into a + // string first). The extension isn't known until we see the + // response, so this writes to a temp file and renames it after. + $tempPath = $pathWithoutExtension.'.downloading-'.uniqid(); + $absoluteTempPath = Storage::disk('public')->path($tempPath); + + File::ensureDirectoryExists(dirname($absoluteTempPath)); + try { - $response = Http::withHeaders(['User-Agent' => 'Mozilla/5.0'])->timeout(30)->get($url); + $response = Http::withHeaders(['User-Agent' => 'Mozilla/5.0']) + ->timeout(60) + ->withOptions(['sink' => $absoluteTempPath]) + ->get($url); } catch (Throwable $e) { Log::warning(static::class.": failed to fetch {$url}: {$e->getMessage()}"); + @unlink($absoluteTempPath); return null; } if (! $response->successful()) { Log::warning(static::class.": got {$response->status()} fetching {$url}"); + @unlink($absoluteTempPath); return null; } $path = $pathWithoutExtension.'.'.$this->guessExtension($url, $response->header('Content-Type')); - - Storage::disk('public')->put($path, $response->body()); + Storage::disk('public')->move($tempPath, $path); return '/storage/'.$path; } diff --git a/resources/js/components/dredge/Feed/PostCard.vue b/resources/js/components/dredge/Feed/PostCard.vue index 61a1242..a9272cb 100644 --- a/resources/js/components/dredge/Feed/PostCard.vue +++ b/resources/js/components/dredge/Feed/PostCard.vue @@ -1,5 +1,6 @@ + + + + + Your browser doesn't support video playback. + + + + + + + + +