2016-10-10 15:30:27 -04:00
|
|
|
<?php namespace BookStack\Http\Controllers;
|
2016-10-09 13:58:22 -04:00
|
|
|
|
|
|
|
use BookStack\Exceptions\FileUploadException;
|
2016-11-12 09:12:26 -05:00
|
|
|
use BookStack\Attachment;
|
2016-10-09 13:58:22 -04:00
|
|
|
use BookStack\Repos\PageRepo;
|
2016-11-12 09:12:26 -05:00
|
|
|
use BookStack\Services\AttachmentService;
|
2016-10-09 13:58:22 -04:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
2016-11-12 09:12:26 -05:00
|
|
|
class AttachmentController extends Controller
|
2016-10-09 13:58:22 -04:00
|
|
|
{
|
2016-11-12 09:12:26 -05:00
|
|
|
protected $attachmentService;
|
|
|
|
protected $attachment;
|
2016-10-09 13:58:22 -04:00
|
|
|
protected $pageRepo;
|
|
|
|
|
|
|
|
/**
|
2016-11-12 09:12:26 -05:00
|
|
|
* AttachmentController constructor.
|
|
|
|
* @param AttachmentService $attachmentService
|
|
|
|
* @param Attachment $attachment
|
2016-10-09 13:58:22 -04:00
|
|
|
* @param PageRepo $pageRepo
|
|
|
|
*/
|
2016-11-12 09:12:26 -05:00
|
|
|
public function __construct(AttachmentService $attachmentService, Attachment $attachment, PageRepo $pageRepo)
|
2016-10-09 13:58:22 -04:00
|
|
|
{
|
2016-11-12 09:12:26 -05:00
|
|
|
$this->attachmentService = $attachmentService;
|
|
|
|
$this->attachment = $attachment;
|
2016-10-09 13:58:22 -04:00
|
|
|
$this->pageRepo = $pageRepo;
|
2016-11-12 09:12:26 -05:00
|
|
|
parent::__construct();
|
2016-10-09 13:58:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2016-11-12 09:12:26 -05:00
|
|
|
* Endpoint at which attachments are uploaded to.
|
2016-10-09 13:58:22 -04:00
|
|
|
* @param Request $request
|
2016-11-12 09:12:26 -05:00
|
|
|
* @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\Response
|
2016-10-09 13:58:22 -04:00
|
|
|
*/
|
|
|
|
public function upload(Request $request)
|
|
|
|
{
|
|
|
|
$this->validate($request, [
|
2016-10-10 16:13:18 -04:00
|
|
|
'uploaded_to' => 'required|integer|exists:pages,id',
|
|
|
|
'file' => 'required|file'
|
2016-10-09 13:58:22 -04:00
|
|
|
]);
|
|
|
|
|
|
|
|
$pageId = $request->get('uploaded_to');
|
2016-11-12 09:21:54 -05:00
|
|
|
$page = $this->pageRepo->getById($pageId, true);
|
2016-10-10 15:30:27 -04:00
|
|
|
|
2016-11-12 09:12:26 -05:00
|
|
|
$this->checkPermission('attachment-create-all');
|
2016-10-10 15:30:27 -04:00
|
|
|
$this->checkOwnablePermission('page-update', $page);
|
|
|
|
|
|
|
|
$uploadedFile = $request->file('file');
|
2016-10-09 13:58:22 -04:00
|
|
|
|
|
|
|
try {
|
2016-11-12 09:12:26 -05:00
|
|
|
$attachment = $this->attachmentService->saveNewUpload($uploadedFile, $pageId);
|
2016-10-09 13:58:22 -04:00
|
|
|
} catch (FileUploadException $e) {
|
|
|
|
return response($e->getMessage(), 500);
|
|
|
|
}
|
|
|
|
|
2016-11-12 09:12:26 -05:00
|
|
|
return response()->json($attachment);
|
2016-10-09 13:58:22 -04:00
|
|
|
}
|
|
|
|
|
2016-10-11 15:39:11 -04:00
|
|
|
/**
|
2016-11-12 09:12:26 -05:00
|
|
|
* Update an uploaded attachment.
|
|
|
|
* @param int $attachmentId
|
2016-10-11 15:39:11 -04:00
|
|
|
* @param Request $request
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2016-11-12 09:12:26 -05:00
|
|
|
public function uploadUpdate($attachmentId, Request $request)
|
2016-10-11 15:39:11 -04:00
|
|
|
{
|
|
|
|
$this->validate($request, [
|
|
|
|
'uploaded_to' => 'required|integer|exists:pages,id',
|
|
|
|
'file' => 'required|file'
|
|
|
|
]);
|
|
|
|
|
|
|
|
$pageId = $request->get('uploaded_to');
|
2016-11-12 09:21:54 -05:00
|
|
|
$page = $this->pageRepo->getById($pageId, true);
|
2016-11-12 09:12:26 -05:00
|
|
|
$attachment = $this->attachment->findOrFail($attachmentId);
|
2016-10-11 15:39:11 -04:00
|
|
|
|
|
|
|
$this->checkOwnablePermission('page-update', $page);
|
2016-11-12 09:12:26 -05:00
|
|
|
$this->checkOwnablePermission('attachment-create', $attachment);
|
2016-10-11 15:39:11 -04:00
|
|
|
|
2016-11-12 09:12:26 -05:00
|
|
|
if (intval($pageId) !== intval($attachment->uploaded_to)) {
|
2016-10-11 15:39:11 -04:00
|
|
|
return $this->jsonError('Page mismatch during attached file update');
|
|
|
|
}
|
|
|
|
|
|
|
|
$uploadedFile = $request->file('file');
|
|
|
|
|
|
|
|
try {
|
2016-11-12 09:12:26 -05:00
|
|
|
$attachment = $this->attachmentService->saveUpdatedUpload($uploadedFile, $attachment);
|
2016-10-11 15:39:11 -04:00
|
|
|
} catch (FileUploadException $e) {
|
|
|
|
return response($e->getMessage(), 500);
|
|
|
|
}
|
|
|
|
|
2016-11-12 09:12:26 -05:00
|
|
|
return response()->json($attachment);
|
2016-10-11 15:39:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the details of an existing file.
|
2016-11-12 09:12:26 -05:00
|
|
|
* @param $attachmentId
|
2016-10-11 15:39:11 -04:00
|
|
|
* @param Request $request
|
2016-11-12 09:12:26 -05:00
|
|
|
* @return Attachment|mixed
|
2016-10-11 15:39:11 -04:00
|
|
|
*/
|
2016-11-12 09:12:26 -05:00
|
|
|
public function update($attachmentId, Request $request)
|
2016-10-11 15:39:11 -04:00
|
|
|
{
|
|
|
|
$this->validate($request, [
|
|
|
|
'uploaded_to' => 'required|integer|exists:pages,id',
|
2016-10-23 12:55:48 -04:00
|
|
|
'name' => 'required|string|min:1|max:255',
|
|
|
|
'link' => 'url|min:1|max:255'
|
2016-10-11 15:39:11 -04:00
|
|
|
]);
|
|
|
|
|
|
|
|
$pageId = $request->get('uploaded_to');
|
2016-11-12 09:21:54 -05:00
|
|
|
$page = $this->pageRepo->getById($pageId, true);
|
2016-11-12 09:12:26 -05:00
|
|
|
$attachment = $this->attachment->findOrFail($attachmentId);
|
2016-10-11 15:39:11 -04:00
|
|
|
|
|
|
|
$this->checkOwnablePermission('page-update', $page);
|
2016-11-12 09:12:26 -05:00
|
|
|
$this->checkOwnablePermission('attachment-create', $attachment);
|
2016-10-11 15:39:11 -04:00
|
|
|
|
2016-11-12 09:12:26 -05:00
|
|
|
if (intval($pageId) !== intval($attachment->uploaded_to)) {
|
2016-10-11 15:39:11 -04:00
|
|
|
return $this->jsonError('Page mismatch during attachment update');
|
|
|
|
}
|
|
|
|
|
2016-11-12 09:12:26 -05:00
|
|
|
$attachment = $this->attachmentService->updateFile($attachment, $request->all());
|
|
|
|
return $attachment;
|
2016-10-11 15:39:11 -04:00
|
|
|
}
|
|
|
|
|
2016-10-10 16:13:18 -04:00
|
|
|
/**
|
2016-11-12 09:12:26 -05:00
|
|
|
* Attach a link to a page.
|
2016-10-10 16:13:18 -04:00
|
|
|
* @param Request $request
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function attachLink(Request $request)
|
|
|
|
{
|
|
|
|
$this->validate($request, [
|
|
|
|
'uploaded_to' => 'required|integer|exists:pages,id',
|
2016-10-23 12:55:48 -04:00
|
|
|
'name' => 'required|string|min:1|max:255',
|
|
|
|
'link' => 'required|url|min:1|max:255'
|
2016-10-10 16:13:18 -04:00
|
|
|
]);
|
|
|
|
|
|
|
|
$pageId = $request->get('uploaded_to');
|
2016-11-12 09:21:54 -05:00
|
|
|
$page = $this->pageRepo->getById($pageId, true);
|
2016-10-10 16:13:18 -04:00
|
|
|
|
2016-11-12 09:12:26 -05:00
|
|
|
$this->checkPermission('attachment-create-all');
|
2016-10-10 16:13:18 -04:00
|
|
|
$this->checkOwnablePermission('page-update', $page);
|
|
|
|
|
2016-11-12 09:12:26 -05:00
|
|
|
$attachmentName = $request->get('name');
|
2016-10-10 16:13:18 -04:00
|
|
|
$link = $request->get('link');
|
2016-11-12 09:12:26 -05:00
|
|
|
$attachment = $this->attachmentService->saveNewFromLink($attachmentName, $link, $pageId);
|
2016-10-10 16:13:18 -04:00
|
|
|
|
2016-11-12 09:12:26 -05:00
|
|
|
return response()->json($attachment);
|
2016-10-10 16:13:18 -04:00
|
|
|
}
|
|
|
|
|
2016-10-09 13:58:22 -04:00
|
|
|
/**
|
2016-11-12 09:12:26 -05:00
|
|
|
* Get the attachments for a specific page.
|
2016-10-09 13:58:22 -04:00
|
|
|
* @param $pageId
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2016-10-10 15:30:27 -04:00
|
|
|
public function listForPage($pageId)
|
2016-10-09 13:58:22 -04:00
|
|
|
{
|
2016-11-12 09:21:54 -05:00
|
|
|
$page = $this->pageRepo->getById($pageId, true);
|
2016-10-10 15:30:27 -04:00
|
|
|
$this->checkOwnablePermission('page-view', $page);
|
2016-11-12 09:21:54 -05:00
|
|
|
return response()->json($page->attachments);
|
2016-10-09 13:58:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-12 09:12:26 -05:00
|
|
|
* Update the attachment sorting.
|
2016-10-09 13:58:22 -04:00
|
|
|
* @param $pageId
|
|
|
|
* @param Request $request
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2016-10-10 15:30:27 -04:00
|
|
|
public function sortForPage($pageId, Request $request)
|
2016-10-09 13:58:22 -04:00
|
|
|
{
|
|
|
|
$this->validate($request, [
|
|
|
|
'files' => 'required|array',
|
|
|
|
'files.*.id' => 'required|integer',
|
|
|
|
]);
|
|
|
|
$page = $this->pageRepo->getById($pageId);
|
2016-10-10 15:30:27 -04:00
|
|
|
$this->checkOwnablePermission('page-update', $page);
|
|
|
|
|
2016-11-12 09:12:26 -05:00
|
|
|
$attachments = $request->get('files');
|
|
|
|
$this->attachmentService->updateFileOrderWithinPage($attachments, $pageId);
|
2016-10-10 16:13:18 -04:00
|
|
|
return response()->json(['message' => 'Attachment order updated']);
|
2016-10-09 13:58:22 -04:00
|
|
|
}
|
|
|
|
|
2016-10-10 15:30:27 -04:00
|
|
|
/**
|
2016-11-12 09:12:26 -05:00
|
|
|
* Get an attachment from storage.
|
|
|
|
* @param $attachmentId
|
|
|
|
* @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Symfony\Component\HttpFoundation\Response
|
2016-10-10 15:30:27 -04:00
|
|
|
*/
|
2016-11-12 09:12:26 -05:00
|
|
|
public function get($attachmentId)
|
2016-10-10 15:30:27 -04:00
|
|
|
{
|
2016-11-12 09:12:26 -05:00
|
|
|
$attachment = $this->attachment->findOrFail($attachmentId);
|
|
|
|
$page = $this->pageRepo->getById($attachment->uploaded_to);
|
2016-10-10 15:30:27 -04:00
|
|
|
$this->checkOwnablePermission('page-view', $page);
|
2016-10-09 13:58:22 -04:00
|
|
|
|
2016-11-12 09:12:26 -05:00
|
|
|
if ($attachment->external) {
|
|
|
|
return redirect($attachment->path);
|
2016-10-10 16:13:18 -04:00
|
|
|
}
|
|
|
|
|
2016-11-12 09:12:26 -05:00
|
|
|
$attachmentContents = $this->attachmentService->getAttachmentFromStorage($attachment);
|
|
|
|
return response($attachmentContents, 200, [
|
2016-10-10 15:30:27 -04:00
|
|
|
'Content-Type' => 'application/octet-stream',
|
2016-11-12 09:12:26 -05:00
|
|
|
'Content-Disposition' => 'attachment; filename="'. $attachment->getFileName() .'"'
|
2016-10-10 15:30:27 -04:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-12 09:12:26 -05:00
|
|
|
* Delete a specific attachment in the system.
|
|
|
|
* @param $attachmentId
|
2016-10-10 15:30:27 -04:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2016-11-12 09:12:26 -05:00
|
|
|
public function delete($attachmentId)
|
2016-10-10 15:30:27 -04:00
|
|
|
{
|
2016-11-12 09:12:26 -05:00
|
|
|
$attachment = $this->attachment->findOrFail($attachmentId);
|
|
|
|
$this->checkOwnablePermission('attachment-delete', $attachment);
|
|
|
|
$this->attachmentService->deleteFile($attachment);
|
2016-10-10 16:13:18 -04:00
|
|
|
return response()->json(['message' => 'Attachment deleted']);
|
2016-10-10 15:30:27 -04:00
|
|
|
}
|
2016-10-09 13:58:22 -04:00
|
|
|
}
|