]> BookStack Code Mirror - bookstack/blobdiff - app/Uploads/AttachmentService.php
Guest create page: name field autofocus
[bookstack] / app / Uploads / AttachmentService.php
index 7974d7ae926b1472f61f567811e527acc254e688..88bb41efb0bcc614eaafa22ac421733446484383 100644 (file)
@@ -14,7 +14,7 @@ use Symfony\Component\HttpFoundation\File\UploadedFile;
 
 class AttachmentService
 {
-    protected $fileSystem;
+    protected FilesystemManager $fileSystem;
 
     /**
      * AttachmentService constructor.
@@ -41,7 +41,7 @@ class AttachmentService
 
         // Change to our secure-attachment disk if any of the local options
         // are used to prevent escaping that location.
-        if ($storageType === 'local' || $storageType === 'local_secure') {
+        if ($storageType === 'local' || $storageType === 'local_secure' || $storageType === 'local_secure_restricted') {
             $storageType = 'local_secure_attachments';
         }
 
@@ -64,13 +64,15 @@ class AttachmentService
     }
 
     /**
-     * Get an attachment from storage.
+     * Stream an attachment from storage.
      *
      * @throws FileNotFoundException
+     *
+     * @return resource|null
      */
-    public function getAttachmentFromStorage(Attachment $attachment): string
+    public function streamAttachmentFromStorage(Attachment $attachment)
     {
-        return $this->getStorageDisk()->get($this->adjustPathForStorageDisk($attachment->path));
+        return $this->getStorageDisk()->readStream($this->adjustPathForStorageDisk($attachment->path));
     }
 
     /**
@@ -211,8 +213,6 @@ class AttachmentService
      */
     protected function putFileInStorage(UploadedFile $uploadedFile): string
     {
-        $attachmentData = file_get_contents($uploadedFile->getRealPath());
-
         $storage = $this->getStorageDisk();
         $basePath = 'uploads/files/' . date('Y-m-M') . '/';
 
@@ -221,10 +221,11 @@ class AttachmentService
             $uploadFileName = Str::random(3) . $uploadFileName;
         }
 
+        $attachmentStream = fopen($uploadedFile->getRealPath(), 'r');
         $attachmentPath = $basePath . $uploadFileName;
 
         try {
-            $storage->put($this->adjustPathForStorageDisk($attachmentPath), $attachmentData);
+            $storage->writeStream($this->adjustPathForStorageDisk($attachmentPath), $attachmentStream);
         } catch (Exception $e) {
             Log::error('Error when attempting file upload:' . $e->getMessage());