File upload deletion complete & added extension handling

Also fixed issue with file editing on JS side
This commit is contained in:
Dan Brown 2016-10-23 13:36:45 +01:00
parent 867fc8be64
commit 7ee695d74a
No known key found for this signature in database
GPG Key ID: 46D9F943C24A2EF9
6 changed files with 26 additions and 7 deletions

View 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

View 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() .'"'
]);
}

View 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();
}

View 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,

View 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();
}
}
}

View 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;
}