Added attachment creation from link/name

This commit is contained in:
Dan Brown
2016-10-10 21:13:18 +01:00
parent ac0b29fb6d
commit 89509b487a
7 changed files with 96 additions and 8 deletions

View File

@ -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;

View File

@ -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']);
}
}