]> BookStack Code Mirror - bookstack/commitdiff
File upload deletion complete & added extension handling
authorDan Brown <redacted>
Sun, 23 Oct 2016 12:36:45 +0000 (13:36 +0100)
committerDan Brown <redacted>
Sun, 23 Oct 2016 12:36:45 +0000 (13:36 +0100)
Also fixed issue with file editing on JS side

app/File.php
app/Http/Controllers/FileController.php
app/Repos/PageRepo.php
app/Services/FileService.php
database/migrations/2016_10_09_142037_create_files_table.php
resources/assets/js/controllers.js

index 055f217bd309efeb464bee8292a8df48a6fc28a6..152350c70a98076911c741c36eed397cdc2f8e15 100644 (file)
@@ -5,6 +5,16 @@ class File extends Ownable
 {
     protected $fillable = ['name', 'order'];
 
+    /**
+     * Get the downloadable file name for this upload.
+     * @return mixed|string
+     */
+    public function getFileName()
+    {
+        if (str_contains($this->name, '.')) return $this->name;
+        return $this->name . '.' . $this->extension;
+    }
+
     /**
      * Get the page this file was uploaded to.
      * @return Page
index 4cdcf66dceb8dafac6fd31d8f864b79b8593c4dd..2518d6cd31004130f1f34644018c87ca8d02b1e0 100644 (file)
@@ -196,7 +196,7 @@ class FileController extends Controller
         $fileContents = $this->fileService->getFile($file);
         return response($fileContents, 200, [
             'Content-Type' => 'application/octet-stream',
-            'Content-Disposition' => 'attachment; filename="'. $file->name .'"'
+            'Content-Disposition' => 'attachment; filename="'. $file->getFileName() .'"'
         ]);
     }
 
index dc7bdb40304ec674f27fac3228f7b6226aba2f62..8cd5c35a9d6ac87ae1eaac6ff70914ac64889b69 100644 (file)
@@ -5,6 +5,7 @@ use BookStack\Book;
 use BookStack\Chapter;
 use BookStack\Entity;
 use BookStack\Exceptions\NotFoundException;
+use BookStack\Services\FileService;
 use Carbon\Carbon;
 use DOMDocument;
 use DOMXPath;
@@ -633,6 +634,13 @@ class PageRepo extends EntityRepo
         $page->revisions()->delete();
         $page->permissions()->delete();
         $this->permissionService->deleteJointPermissionsForEntity($page);
+
+        // Delete AttachedFiles
+        $fileService = app(FileService::class);
+        foreach ($page->files as $file) {
+            $fileService->deleteFile($file);
+        }
+
         $page->delete();
     }
 
index a04d840018a92ce1bcf4ce6416ec9ff4083ac530..261695e1f066c8856e4e028375ea955d8f22ed77 100644 (file)
@@ -38,6 +38,7 @@ class FileService extends UploadService
         $file = File::forceCreate([
             'name' => $fileName,
             'path' => $filePath,
+            'extension' => $uploadedFile->getClientOriginalExtension(),
             'uploaded_to' => $page_id,
             'created_by' => user()->id,
             'updated_by' => user()->id,
@@ -67,6 +68,7 @@ class FileService extends UploadService
         $file->name = $fileName;
         $file->path = $filePath;
         $file->external = false;
+        $file->extension = $uploadedFile->getClientOriginalExtension();
         $file->save();
         return $file;
     }
@@ -85,6 +87,7 @@ class FileService extends UploadService
             'name' => $name,
             'path' => $link,
             'external' => true,
+            'extension' => '',
             'uploaded_to' => $page_id,
             'created_by' => user()->id,
             'updated_by' => user()->id,
index 57ddd1202141273ff045900604e51127b0baacf2..49433f19cf6215b5a5f292edffa5829cb6b048ff 100644 (file)
@@ -17,6 +17,7 @@ class CreateFilesTable extends Migration
             $table->increments('id');
             $table->string('name');
             $table->string('path');
+            $table->string('extension', 20);
             $table->integer('uploaded_to');
 
             $table->boolean('external');
@@ -59,16 +60,12 @@ class CreateFilesTable extends Migration
     {
         Schema::dropIfExists('files');
 
-        // Get roles with permissions we need to change
-        $adminRoleId = DB::table('roles')->where('system_name', '=', 'admin')->first()->id;
-
         // Create & attach new entity permissions
         $ops = ['Create All', 'Create Own', 'Update All', 'Update Own', 'Delete All', 'Delete Own'];
         $entity = 'File';
         foreach ($ops as $op) {
             $permName = strtolower($entity) . '-' . strtolower(str_replace(' ', '-', $op));
-            $permission = DB::table('role_permissions')->where('name', '=', $permName)->get();
-            DB::table('permission_role')->where('permission_id', '=', $permission->id)->delete();
+            DB::table('role_permissions')->where('name', '=', $permName)->delete();
         }
     }
 }
index 40466876822539661ab769f67f75700c5e0a7437..78b3684d6e21817d1dc51322e49c5a760d6ade39 100644 (file)
@@ -674,6 +674,7 @@ module.exports = function (ngApp, events) {
                     if ($scope.editFile && !file.external) {
                         $scope.editFile.link = '';
                     }
+                    $scope.editFile = false;
                     events.emit('success', 'Attachment details updated');
                 });
             };
@@ -686,7 +687,7 @@ module.exports = function (ngApp, events) {
              */
             function filesIndexOf(file) {
                 for (let i = 0; i < $scope.files.length; i++) {
-                    if ($scope.files[i].id == file.id) return file.id;
+                    if ($scope.files[i].id == file.id) return i;
                 }
                 return -1;
             }