mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-06-01 05:51:52 +08:00
Added attachment creation from link/name
This commit is contained in:
@ -71,8 +71,13 @@ abstract class Controller extends BaseController
|
||||
*/
|
||||
protected function showPermissionError()
|
||||
{
|
||||
Session::flash('error', trans('errors.permission'));
|
||||
$response = request()->wantsJson() ? response()->json(['error' => trans('errors.permissionJson')], 403) : redirect('/');
|
||||
if (request()->wantsJson()) {
|
||||
$response = response()->json(['error' => trans('errors.permissionJson')], 403);
|
||||
} else {
|
||||
$response = redirect('/');
|
||||
session()->flash('error', trans('errors.permission'));
|
||||
}
|
||||
|
||||
throw new HttpResponseException($response);
|
||||
}
|
||||
|
||||
@ -83,7 +88,7 @@ abstract class Controller extends BaseController
|
||||
*/
|
||||
protected function checkPermission($permissionName)
|
||||
{
|
||||
if (!$this->currentUser || !$this->currentUser->can($permissionName)) {
|
||||
if (!user() || !user()->can($permissionName)) {
|
||||
$this->showPermissionError();
|
||||
}
|
||||
return true;
|
||||
|
@ -36,7 +36,8 @@ class FileController extends Controller
|
||||
{
|
||||
// TODO - ensure uploads are deleted on page delete.
|
||||
$this->validate($request, [
|
||||
'uploaded_to' => 'required|integer|exists:pages,id'
|
||||
'uploaded_to' => 'required|integer|exists:pages,id',
|
||||
'file' => 'required|file'
|
||||
]);
|
||||
|
||||
$pageId = $request->get('uploaded_to');
|
||||
@ -56,6 +57,32 @@ class FileController extends Controller
|
||||
return response()->json($file);
|
||||
}
|
||||
|
||||
/**
|
||||
* Attach a link to a page as a file.
|
||||
* @param Request $request
|
||||
* @return mixed
|
||||
*/
|
||||
public function attachLink(Request $request)
|
||||
{
|
||||
$this->validate($request, [
|
||||
'uploaded_to' => 'required|integer|exists:pages,id',
|
||||
'name' => 'string',
|
||||
'link' => 'url'
|
||||
]);
|
||||
|
||||
$pageId = $request->get('uploaded_to');
|
||||
$page = $this->pageRepo->getById($pageId);
|
||||
|
||||
$this->checkPermission('file-create-all');
|
||||
$this->checkOwnablePermission('page-update', $page);
|
||||
|
||||
$fileName = $request->get('name');
|
||||
$link = $request->get('link');
|
||||
$file = $this->fileService->saveNewFromLink($fileName, $link, $pageId);
|
||||
|
||||
return response()->json($file);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the files for a specific page.
|
||||
* @param $pageId
|
||||
@ -85,7 +112,7 @@ class FileController extends Controller
|
||||
|
||||
$files = $request->get('files');
|
||||
$this->fileService->updateFileOrderWithinPage($files, $pageId);
|
||||
return response()->json(['message' => 'File order updated']);
|
||||
return response()->json(['message' => 'Attachment order updated']);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -98,6 +125,10 @@ class FileController extends Controller
|
||||
$page = $this->pageRepo->getById($file->uploaded_to);
|
||||
$this->checkOwnablePermission('page-view', $page);
|
||||
|
||||
if ($file->external) {
|
||||
return redirect($file->path);
|
||||
}
|
||||
|
||||
$fileContents = $this->fileService->getFile($file);
|
||||
return response($fileContents, 200, [
|
||||
'Content-Type' => 'application/octet-stream',
|
||||
@ -113,8 +144,8 @@ class FileController extends Controller
|
||||
public function delete($fileId)
|
||||
{
|
||||
$file = $this->file->findOrFail($fileId);
|
||||
$this->checkOwnablePermission($file, 'file-delete');
|
||||
$this->checkOwnablePermission('file-delete', $file);
|
||||
$this->fileService->deleteFile($file);
|
||||
return response()->json(['message' => 'File deleted']);
|
||||
return response()->json(['message' => 'Attachment deleted']);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user