3 namespace BookStack\Uploads;
5 use BookStack\Exceptions\FileUploadException;
7 use Illuminate\Contracts\Filesystem\Filesystem as Storage;
8 use Illuminate\Filesystem\FilesystemAdapter;
9 use Illuminate\Filesystem\FilesystemManager;
10 use Illuminate\Support\Facades\Log;
11 use Illuminate\Support\Str;
12 use League\Flysystem\WhitespacePathNormalizer;
13 use Symfony\Component\HttpFoundation\File\UploadedFile;
17 public function __construct(
18 protected FilesystemManager $fileSystem,
23 * @return resource|null
25 public function getReadStream(string $path)
27 return $this->getStorageDisk()->readStream($this->adjustPathForStorageDisk($path));
30 public function getSize(string $path): int
32 return $this->getStorageDisk()->size($this->adjustPathForStorageDisk($path));
35 public function delete(string $path, bool $removeEmptyDir = false): void
37 $storage = $this->getStorageDisk();
38 $adjustedPath = $this->adjustPathForStorageDisk($path);
39 $dir = dirname($adjustedPath);
41 $storage->delete($adjustedPath);
42 if ($removeEmptyDir && count($storage->allFiles($dir)) === 0) {
43 $storage->deleteDirectory($dir);
48 * @throws FileUploadException
50 public function uploadFile(UploadedFile $file, string $subDirectory, string $suffix, string $extension): string
52 $storage = $this->getStorageDisk();
53 $basePath = trim($subDirectory, '/') . '/';
55 $uploadFileName = Str::random(16) . ($suffix ? "-{$suffix}" : '') . ($extension ? ".{$extension}" : '');
56 while ($storage->exists($this->adjustPathForStorageDisk($basePath . $uploadFileName))) {
57 $uploadFileName = Str::random(3) . $uploadFileName;
60 $fileStream = fopen($file->getRealPath(), 'r');
61 $filePath = $basePath . $uploadFileName;
64 $storage->writeStream($this->adjustPathForStorageDisk($filePath), $fileStream);
65 } catch (Exception $e) {
66 Log::error('Error when attempting file upload:' . $e->getMessage());
68 throw new FileUploadException(trans('errors.path_not_writable', ['filePath' => $filePath]));
75 * Check whether the configured storage is remote from the host of this app.
77 public function isRemote(): bool
79 return $this->getStorageDiskName() === 's3';
83 * Get the actual path on system for the given relative file path.
85 public function getSystemPath(string $filePath): string
87 if ($this->isRemote()) {
91 return storage_path('uploads/files/' . ltrim($this->adjustPathForStorageDisk($filePath), '/'));
95 * Get the storage that will be used for storing files.
97 protected function getStorageDisk(): Storage
99 return $this->fileSystem->disk($this->getStorageDiskName());
103 * Get the name of the storage disk to use.
105 protected function getStorageDiskName(): string
107 $storageType = trim(strtolower(config('filesystems.attachments')));
109 // Change to our secure-attachment disk if any of the local options
110 // are used to prevent escaping that location.
111 if ($storageType === 'local' || $storageType === 'local_secure' || $storageType === 'local_secure_restricted') {
112 $storageType = 'local_secure_attachments';
119 * Change the originally provided path to fit any disk-specific requirements.
120 * This also ensures the path is kept to the expected root folders.
122 protected function adjustPathForStorageDisk(string $path): string
124 $path = (new WhitespacePathNormalizer())->normalizePath(str_replace('uploads/files/', '', $path));
126 if ($this->getStorageDiskName() === 'local_secure_attachments') {
130 return 'uploads/files/' . $path;