1
0
mirror of https://github.com/BookStackApp/BookStack.git synced 2025-07-30 04:23:11 +03:00

ZIP Exports: Added detection/handling of images with external storage

Added test to cover.
This commit is contained in:
Dan Brown
2024-11-26 15:59:39 +00:00
parent 95d62e7f57
commit 0a182a45ba
4 changed files with 85 additions and 7 deletions

View File

@ -3,19 +3,22 @@
namespace BookStack\References\ModelResolvers;
use BookStack\Uploads\Image;
use BookStack\Uploads\ImageStorage;
class ImageModelResolver implements CrossLinkModelResolver
{
protected ?string $pattern = null;
public function resolve(string $link): ?Image
{
$pattern = '/^' . preg_quote(url('/uploads/images'), '/') . '\/(.+)/';
$pattern = $this->getUrlPattern();
$matches = [];
$match = preg_match($pattern, $link, $matches);
if (!$match) {
return null;
}
$path = $matches[1];
$path = $matches[2];
// Strip thumbnail element from path if existing
$originalPathSplit = array_filter(explode('/', $path), function (string $part) {
@ -30,4 +33,26 @@ class ImageModelResolver implements CrossLinkModelResolver
return Image::query()->where('path', '=', $fullPath)->first();
}
/**
* Get the regex pattern to identify image URLs.
* Caches the pattern since it requires looking up to settings/config.
*/
protected function getUrlPattern(): string
{
if ($this->pattern) {
return $this->pattern;
}
$urls = [url('/uploads/images')];
$baseImageUrl = ImageStorage::getPublicUrl('/uploads/images');
if ($baseImageUrl !== $urls[0]) {
$urls[] = $baseImageUrl;
}
$imageUrlRegex = implode('|', array_map(fn ($url) => preg_quote($url, '/'), $urls));
$this->pattern = '/^(' . $imageUrlRegex . ')\/(.+)/';
return $this->pattern;
}
}