Skip to content

NOW MANAGED ON CODEBERG

File upload deletion complete & added extension handling

Dan Brown7ee695d74a42
diff --git a/app/File.php b/app/File.php
index 055f217bd..152350c70 100644
--- a/app/File.php
+++ b/app/File.php
@@ -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
diff --git a/app/Http/Controllers/FileController.php b/app/Http/Controllers/FileController.php
index 4cdcf66dc..2518d6cd3 100644
--- a/app/Http/Controllers/FileController.php
+++ b/app/Http/Controllers/FileController.php
@@ -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() .'"'
]);
}
diff --git a/app/Repos/PageRepo.php b/app/Repos/PageRepo.php
index dc7bdb403..8cd5c35a9 100644
--- a/app/Repos/PageRepo.php
+++ b/app/Repos/PageRepo.php
@@ -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();
}
diff --git a/app/Services/FileService.php b/app/Services/FileService.php
index a04d84001..261695e1f 100644
--- a/app/Services/FileService.php
+++ b/app/Services/FileService.php
@@ -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,
diff --git a/database/migrations/2016_10_09_142037_create_files_table.php b/database/migrations/2016_10_09_142037_create_files_table.php
index 57ddd1202..49433f19c 100644
--- a/database/migrations/2016_10_09_142037_create_files_table.php
+++ b/database/migrations/2016_10_09_142037_create_files_table.php
@@ -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();
}
}
}
diff --git a/resources/assets/js/controllers.js b/resources/assets/js/controllers.js
index 404668768..78b3684d6 100644
--- a/resources/assets/js/controllers.js
+++ b/resources/assets/js/controllers.js
@@ -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;
}