X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/7247e31936ebf630b28be5870a5760be920b0d90..refs/pull/5685/head:/app/Uploads/ImageStorage.php diff --git a/app/Uploads/ImageStorage.php b/app/Uploads/ImageStorage.php index c51450052..ddaa26a94 100644 --- a/app/Uploads/ImageStorage.php +++ b/app/Uploads/ImageStorage.php @@ -2,10 +2,8 @@ namespace BookStack\Uploads; -use Illuminate\Contracts\Filesystem\Filesystem as StorageDisk; use Illuminate\Filesystem\FilesystemManager; use Illuminate\Support\Str; -use League\Flysystem\WhitespacePathNormalizer; class ImageStorage { @@ -17,44 +15,25 @@ class ImageStorage /** * Get the storage disk for the given image type. */ - public function getDisk(string $imageType = ''): StorageDisk + public function getDisk(string $imageType = ''): ImageStorageDisk { - return $this->fileSystem->disk($this->getDiskName($imageType)); - } + $diskName = $this->getDiskName($imageType); - /** - * Check if local secure image storage (Fetched behind authentication) - * is currently active in the instance. - */ - public function usingSecureImages(string $imageType = 'gallery'): bool - { - return $this->getDiskName($imageType) === 'local_secure_images'; + return new ImageStorageDisk( + $diskName, + $this->fileSystem->disk($diskName), + ); } /** * Check if "local secure restricted" (Fetched behind auth, with permissions enforced) * is currently active in the instance. */ - public function usingSecureRestrictedImages() + public function usingSecureRestrictedImages(): bool { return config('filesystems.images') === 'local_secure_restricted'; } - /** - * Change the originally provided path to fit any disk-specific requirements. - * This also ensures the path is kept to the expected root folders. - */ - public function adjustPathForDisk(string $path, string $imageType = ''): string - { - $path = (new WhitespacePathNormalizer())->normalizePath(str_replace('uploads/images/', '', $path)); - - if ($this->usingSecureImages($imageType)) { - return $path; - } - - return 'uploads/images/' . $path; - } - /** * Clean up an image file name to be both URL and storage safe. */ @@ -78,7 +57,7 @@ class ImageStorage */ protected function getDiskName(string $imageType): string { - $storageType = config('filesystems.images'); + $storageType = strtolower(config('filesystems.images')); $localSecureInUse = ($storageType === 'local_secure' || $storageType === 'local_secure_restricted'); // Ensure system images (App logo) are uploaded to a public space @@ -131,10 +110,20 @@ class ImageStorage } /** - * Gets a public facing url for an image by checking relevant environment variables. + * Gets a public facing url for an image or location at the given path. + */ + public static function getPublicUrl(string $filePath): string + { + return static::getPublicBaseUrl() . '/' . ltrim($filePath, '/'); + } + + /** + * Get the public base URL used for images. + * Will not include any path element of the image file, just the base part + * from where the path is then expected to start from. * If s3-style store is in use it will default to guessing a public bucket URL. */ - public function getPublicUrl(string $filePath): string + protected static function getPublicBaseUrl(): string { $storageUrl = config('filesystems.url'); @@ -152,32 +141,6 @@ class ImageStorage $basePath = $storageUrl ?: url('/'); - return rtrim($basePath, '/') . $filePath; - } - - /** - * Save image data for the given path in the public space, if possible, - * for the provided storage mechanism. - */ - public function storeInPublicSpace(StorageDisk $storage, string $path, string $data): void - { - $storage->put($path, $data); - - // Set visibility when a non-AWS-s3, s3-like storage option is in use. - // Done since this call can break s3-like services but desired for other image stores. - // Attempting to set ACL during above put request requires different permissions - // hence would technically be a breaking change for actual s3 usage. - if (!$this->isS3Like()) { - $storage->setVisibility($path, 'public'); - } - } - - /** - * Check if the image storage in use is an S3-like (but not likely S3) external system. - */ - protected function isS3Like(): bool - { - $usingS3 = strtolower(config('filesystems.images')) === 's3'; - return $usingS3 && !is_null(config('filesystems.disks.s3.endpoint')); + return rtrim($basePath, '/'); } }