Repository navigation
Expand file tree
/
Copy pathShareFileController.php
More file actions
48 lines (37 loc) · 1.52 KB
/
Copy pathShareFileController.php
File metadata and controls
48 lines (37 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
declare(strict_types=1);
namespace App\Http\Controllers;
use App\Actions\GetPathFromVaultNode;
use App\Actions\GetReferencedImageNodesFromContent;
use App\Actions\GetVaultNodeFromPath;
use App\Models\VaultNode;
use App\Models\VaultNodeShare;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
final readonly class ShareFileController
{
public function show(
Request $request,
VaultNodeShare $share,
GetVaultNodeFromPath $getVaultNodeFromPath,
GetReferencedImageNodesFromContent $getReferencedImageNodesFromContent,
GetPathFromVaultNode $getPathFromVaultNode,
): BinaryFileResponse {
abort_unless($request->has('path'), 404);
/** @var string $path */
$path = $request->path;
$node = $getVaultNodeFromPath->handle($share->node->vault_id, $path);
abort_unless($node !== null, 404);
// Only files actually referenced as images in the shared note's current
// content are servable, regardless of what else lives in the vault.
$allowedNodeIds = array_map(
fn(VaultNode $imageNode): int => $imageNode->id,
$getReferencedImageNodesFromContent->handle($share->node),
);
abort_unless(in_array($node->id, $allowedNodeIds, true), 404);
$relativePath = $getPathFromVaultNode->handle($node);
$absolutePath = Storage::disk('local')->path($relativePath);
return response()->file($absolutePath);
}
}