3 namespace BookStack\Uploads;
5 use Illuminate\Contracts\Filesystem\Filesystem;
6 use Illuminate\Filesystem\FilesystemAdapter;
7 use League\Flysystem\WhitespacePathNormalizer;
8 use Symfony\Component\HttpFoundation\StreamedResponse;
10 class ImageStorageDisk
12 public function __construct(
13 protected string $diskName,
14 protected Filesystem $filesystem,
19 * Check if local secure image storage (Fetched behind authentication)
20 * is currently active in the instance.
22 public function usingSecureImages(): bool
24 return $this->diskName === 'local_secure_images';
28 * Change the originally provided path to fit any disk-specific requirements.
29 * This also ensures the path is kept to the expected root folders.
31 protected function adjustPathForDisk(string $path): string
33 $path = (new WhitespacePathNormalizer())->normalizePath(str_replace('uploads/images/', '', $path));
35 if ($this->usingSecureImages()) {
39 return 'uploads/images/' . $path;
43 * Check if a file at the given path exists.
45 public function exists(string $path): bool
47 return $this->filesystem->exists($this->adjustPathForDisk($path));
51 * Get the file at the given path.
53 public function get(string $path): ?string
55 return $this->filesystem->get($this->adjustPathForDisk($path));
59 * Save the given image data at the given path. Can choose to set
60 * the image as public which will update its visibility after saving.
62 public function put(string $path, string $data, bool $makePublic = false): void
64 $path = $this->adjustPathForDisk($path);
65 $this->filesystem->put($path, $data);
67 // Set visibility when a non-AWS-s3, s3-like storage option is in use.
68 // Done since this call can break s3-like services but desired for other image stores.
69 // Attempting to set ACL during above put request requires different permissions
70 // hence would technically be a breaking change for actual s3 usage.
71 if ($makePublic && !$this->isS3Like()) {
72 $this->filesystem->setVisibility($path, 'public');
77 * Destroys an image at the given path.
78 * Searches for image thumbnails in addition to main provided path.
80 public function destroyAllMatchingNameFromPath(string $path): void
82 $path = $this->adjustPathForDisk($path);
84 $imageFolder = dirname($path);
85 $imageFileName = basename($path);
86 $allImages = collect($this->filesystem->allFiles($imageFolder));
89 $imagesToDelete = $allImages->filter(function ($imagePath) use ($imageFileName) {
90 return basename($imagePath) === $imageFileName;
92 $this->filesystem->delete($imagesToDelete->all());
94 // Cleanup of empty folders
95 $foldersInvolved = array_merge([$imageFolder], $this->filesystem->directories($imageFolder));
96 foreach ($foldersInvolved as $directory) {
97 if ($this->isFolderEmpty($directory)) {
98 $this->filesystem->deleteDirectory($directory);
104 * Get the mime type of the file at the given path.
105 * Only works for local filesystem adapters.
107 public function mimeType(string $path): string
109 $path = $this->adjustPathForDisk($path);
110 return $this->filesystem instanceof FilesystemAdapter ? $this->filesystem->mimeType($path) : '';
114 * Get a stream response for the image at the given path.
116 public function response(string $path): StreamedResponse
118 return $this->filesystem->response($this->adjustPathForDisk($path));
122 * Check if the image storage in use is an S3-like (but not likely S3) external system.
124 protected function isS3Like(): bool
126 $usingS3 = $this->diskName === 's3';
127 return $usingS3 && !is_null(config('filesystems.disks.s3.endpoint'));
131 * Check whether a folder is empty.
133 protected function isFolderEmpty(string $path): bool
135 $files = $this->filesystem->files($path);
136 $folders = $this->filesystem->directories($path);
138 return count($files) === 0 && count($folders) === 0;