Skip to content

Commit cc5225a

Browse files
committed
Fix download controller for large files
1 parent 972859e commit cc5225a

4 files changed

Lines changed: 21 additions & 23 deletions

File tree

src/Http/Controllers/Api/DownloadController.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,6 @@ public function show(string $globalFilter, string $entityKey, ?string $instanceI
2525
trans('sharp::errors.file_not_found'),
2626
);
2727

28-
return response(
29-
content: Storage::disk($disk)->get($path),
30-
headers: [
31-
'Content-Type' => Storage::disk($disk)->mimeType($path),
32-
'Content-Disposition' => 'attachment',
33-
],
34-
);
28+
return Storage::disk($disk)->download($path);
3529
}
3630
}

src/Http/Middleware/HandleSharpErrors.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ public function handle(Request $request, Closure $next)
2020
/** @var Response $response */
2121
$response = $next($request);
2222

23-
if ($response->exception instanceof ValidationException
24-
|| $response->exception instanceof AuthenticationException) {
23+
if (isset($response->exception) && (
24+
$response->exception instanceof ValidationException
25+
|| $response->exception instanceof AuthenticationException
26+
)) {
2527
return $response;
2628
}
2729

src/Http/Middleware/InvalidateCache.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,19 @@
33
namespace Code16\Sharp\Http\Middleware;
44

55
use Closure;
6+
use Symfony\Component\HttpFoundation\Response;
67

78
class InvalidateCache
89
{
910
public function handle($request, Closure $next)
1011
{
12+
/** @var Response $response */
1113
$response = $next($request);
1214

13-
return $response->header('Pragma', 'no-cache')
14-
->header('Expires', 'Fri, 01 Jan 1990 00:00:00 GMT')
15-
->header('Cache-Control', 'no-cache, must-revalidate, no-store, max-age=0, private');
15+
$response->headers->set('Pragma', 'no-cache');
16+
$response->headers->set('Expires', 'Fri, 01 Jan 1990 00:00:00 GMT');
17+
$response->headers->set('Cache-Control', 'no-cache, must-revalidate, no-store, max-age=0, private');
18+
19+
return $response;
1620
}
1721
}

tests/Http/Api/DownloadControllerTest.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
$file = UploadedFile::fake()->image('test.jpg', 600, 600);
1616
$file->storeAs('/files', 'test.jpg', ['disk' => 'local']);
1717

18-
$response = $this
18+
$this
1919
->get(
2020
route('code16.sharp.download.show', [
2121
'entityKey' => 'person',
@@ -24,17 +24,16 @@
2424
'path' => '/files/test.jpg',
2525
]),
2626
)
27-
->assertOk();
28-
29-
expect($response->content())
30-
->toEqual(Storage::disk('local')->get('files/test.jpg'));
27+
->assertOk()
28+
->assertStreamedContent(Storage::disk('local')->get('files/test.jpg'))
29+
->assertHeader('Content-Disposition', 'attachment; filename=test.jpg');
3130
});
3231

3332
it('allows to download a file from a show field', function () {
3433
$file = UploadedFile::fake()->image('test.jpg', 600, 600);
3534
$file->storeAs('/files', 'test.jpg', ['disk' => 'local']);
3635

37-
$response = $this
36+
$this
3837
->get(
3938
route('code16.sharp.download.show', [
4039
'entityKey' => 'person',
@@ -43,10 +42,9 @@
4342
'path' => '/files/test.jpg',
4443
]),
4544
)
46-
->assertOk();
47-
48-
expect($response->content())
49-
->toEqual(Storage::disk('local')->get('files/test.jpg'));
45+
->assertOk()
46+
->assertStreamedContent(Storage::disk('local')->get('files/test.jpg'))
47+
->assertHeader('Content-Disposition', 'attachment; filename=test.jpg');
5048
});
5149

5250
it('returns a 404 for a missing file', function () {
@@ -55,7 +53,7 @@
5553
route('code16.sharp.download.show', [
5654
'entityKey' => 'person',
5755
'instanceId' => 1,
58-
'fileName' => 'test.jpg',
56+
'path' => '/files/unknown.jpg',
5957
]),
6058
)
6159
->assertNotFound();

0 commit comments

Comments
 (0)