]> BookStack Code Mirror - bookstack/blob - app/References/ModelResolvers/ImageModelResolver.php
Zip Exports: Added attachment/image link resolving & JSON null handling
[bookstack] / app / References / ModelResolvers / ImageModelResolver.php
1 <?php
2
3 namespace BookStack\References\ModelResolvers;
4
5 use BookStack\Uploads\Image;
6
7 class ImageModelResolver implements CrossLinkModelResolver
8 {
9     public function resolve(string $link): ?Image
10     {
11         $pattern = '/^' . preg_quote(url('/uploads/images'), '/') . '\/(.+)/';
12         $matches = [];
13         $match = preg_match($pattern, $link, $matches);
14         if (!$match) {
15             return null;
16         }
17
18         $path = $matches[1];
19
20         // Strip thumbnail element from path if existing
21         $originalPathSplit = array_filter(explode('/', $path), function (string $part) {
22             $resizedDir = (str_starts_with($part, 'thumbs-') || str_starts_with($part, 'scaled-'));
23             $missingExtension = !str_contains($part, '.');
24
25             return !($resizedDir && $missingExtension);
26         });
27
28         // Build a database-format image path and search for the image entry
29         $fullPath = '/uploads/images/' . ltrim(implode('/', $originalPathSplit), '/');
30
31         return Image::query()->where('path', '=', $fullPath)->first();
32     }
33 }