]> BookStack Code Mirror - bookstack/blobdiff - app/Services/FileService.php
Added view, deletion and permissions for files
[bookstack] / app / Services / FileService.php
index 689fa0600360107c85a7de815d568aa8a7158165..7429f0e6425224c7bb4fbbf5d154fa225e550e66 100644 (file)
@@ -4,12 +4,24 @@
 use BookStack\Exceptions\FileUploadException;
 use BookStack\File;
 use Exception;
+use Illuminate\Contracts\Filesystem\FileNotFoundException;
 use Illuminate\Support\Collection;
 use Symfony\Component\HttpFoundation\File\UploadedFile;
 
 class FileService extends UploadService
 {
 
+    /**
+     * Get a file from storage.
+     * @param File $file
+     * @return string
+     */
+    public function getFile(File $file)
+    {
+        $filePath = $this->getStorageBasePath() . $file->path;
+        return $this->getStorage()->get($filePath);
+    }
+
     /**
      * Store a new file upon user upload.
      * @param UploadedFile $uploadedFile
@@ -76,4 +88,22 @@ class FileService extends UploadService
         }
     }
 
+    /**
+     * Delete a file and any empty folders the deletion leaves.
+     * @param File $file
+     */
+    public function deleteFile(File $file)
+    {
+        $storedFilePath = $this->getStorageBasePath() . $file->path;
+        $storage = $this->getStorage();
+        $dirPath = dirname($storedFilePath);
+
+        $storage->delete($storedFilePath);
+        if (count($storage->allFiles($dirPath)) === 0) {
+            $storage->deleteDirectory($dirPath);
+        }
+
+        $file->delete();
+    }
+
 }
\ No newline at end of file