3 namespace BookStack\References\ModelResolvers;
5 use BookStack\Uploads\Image;
7 class ImageModelResolver implements CrossLinkModelResolver
9 public function resolve(string $link): ?Image
11 $pattern = '/^' . preg_quote(url('/uploads/images'), '/') . '\/(.+)/';
13 $match = preg_match($pattern, $link, $matches);
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, '.');
25 return !($resizedDir && $missingExtension);
28 // Build a database-format image path and search for the image entry
29 $fullPath = '/uploads/images/' . ltrim(implode('/', $originalPathSplit), '/');
31 return Image::query()->where('path', '=', $fullPath)->first();