|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Redberry\MailboxForLaravel\Http\Controllers; |
| 4 | + |
| 5 | +use Illuminate\Http\Response as HttpResponse; |
| 6 | +use Illuminate\Support\Facades\Response; |
| 7 | +use Redberry\MailboxForLaravel\CaptureService; |
| 8 | + |
| 9 | +class AssetController |
| 10 | +{ |
| 11 | + public function __invoke(CaptureService $capture, string $message, string $asset) |
| 12 | + { |
| 13 | + $payload = $capture->get($message); |
| 14 | + |
| 15 | + abort_unless(!!$payload, 404); |
| 16 | + |
| 17 | + $attachment = collect($payload['attachments'] ?? []) |
| 18 | + ->first(fn ($a) => ($a['filename'] ?? null) === $asset); |
| 19 | + abort_unless($attachment, 404); |
| 20 | + |
| 21 | + $headers = [ |
| 22 | + 'Content-Type' => $attachment['contentType'] ?? 'application/octet-stream', |
| 23 | + 'Cache-Control' => 'public, max-age=31536000, immutable', |
| 24 | + ]; |
| 25 | + |
| 26 | + // decide disposition inline vs attachment |
| 27 | + if (($attachment['disposition'] ?? '') !== '') { |
| 28 | + $headers['Content-Disposition'] = $attachment['disposition']; |
| 29 | + } |
| 30 | + |
| 31 | + if (isset($attachment['path'])) { |
| 32 | + return Response::stream(function () use ($attachment) { |
| 33 | + $stream = fopen($attachment['path'], 'rb'); |
| 34 | + if ($stream) { |
| 35 | + fpassthru($stream); |
| 36 | + fclose($stream); |
| 37 | + } |
| 38 | + }, 200, $headers); |
| 39 | + } |
| 40 | + |
| 41 | + if (isset($attachment['content'])) { |
| 42 | + $content = base64_decode($attachment['content']); |
| 43 | + |
| 44 | + return new HttpResponse($content, 200, $headers); |
| 45 | + } |
| 46 | + |
| 47 | + abort(404); |
| 48 | + } |
| 49 | +} |
0 commit comments