Skip to content

Commit 99670bd

Browse files
committed
Fix: Restored asset controller
1 parent bcad844 commit 99670bd

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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

Comments
 (0)