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;
|
2018-02-11 07:37:02 -05:00
|
|
|
use BookStack\Exceptions\NotFoundException;
|
2017-01-01 11:05:44 -05:00
|
|
|
use BookStack\Repos\EntityRepo;
|
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;
|
2017-01-01 11:05:44 -05:00
|
|
|
protected $entityRepo;
|
2016-10-09 13:58:22 -04:00
|
|
|
|
|
|
|
/**
|
2016-11-12 09:12:26 -05:00
|
|
|
* AttachmentController constructor.
|
|
|
|
* @param AttachmentService $attachmentService
|
|
|
|
* @param Attachment $attachment
|
2017-01-02 06:07:27 -05:00
|
|
|
* @param EntityRepo $entityRepo
|
2016-10-09 13:58:22 -04:00
|
|
|
*/
|
2017-01-02 06:07:27 -05:00
|
|
|
public function __construct(AttachmentService $attachmentService, Attachment $attachment, EntityRepo $entityRepo)
|
2016-10-09 13:58:22 -04:00
|
|
|
{
|
2016-11-12 09:12:26 -05:00
|
|
|
$this->attachmentService = $attachmentService;
|
|
|
|
$this->attachment = $attachment;
|
2017-01-01 11:05:44 -05:00
|
|
|
$this->entityRepo = $entityRepo;
|
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');
|
2017-01-01 11:05:44 -05:00
|
|
|
$page = $this->entityRepo->getById('page', $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');
|
2017-01-01 11:05:44 -05:00
|
|
|
$page = $this->entityRepo->getById('page', $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-12-04 11:51:39 -05:00
|
|
|
return $this->jsonError(trans('errors.attachment_page_mismatch'));
|
2016-10-11 15:39:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
$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');
|
2017-01-01 11:05:44 -05:00
|
|
|
$page = $this->entityRepo->getById('page', $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-12-04 11:51:39 -05:00
|
|
|
return $this->jsonError(trans('errors.attachment_page_mismatch'));
|
2016-10-11 15:39:11 -04:00
|
|
|
}
|
|
|
|
|
2016-11-12 09:12:26 -05:00
|
|
|
$attachment = $this->attachmentService->updateFile($attachment, $request->all());
|
2016-12-31 09:27:40 -05:00
|
|
|
return response()->json($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');
|
2017-01-01 11:05:44 -05:00
|
|
|
$page = $this->entityRepo->getById('page', $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
|
|
|
{
|
2017-01-01 11:05:44 -05:00
|
|
|
$page = $this->entityRepo->getById('page', $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',
|
|
|
|
]);
|
2017-01-01 11:05:44 -05:00
|
|
|
$page = $this->entityRepo->getById('page', $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-12-04 11:51:39 -05:00
|
|
|
return response()->json(['message' => trans('entities.attachments_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
|
2018-02-11 07:37:02 -05:00
|
|
|
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
|
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);
|
2017-01-01 11:05:44 -05:00
|
|
|
$page = $this->entityRepo->getById('page', $attachment->uploaded_to);
|
2018-02-11 07:37:02 -05:00
|
|
|
if ($page === null) {
|
|
|
|
throw new NotFoundException(trans('errors.attachment_not_found'));
|
|
|
|
}
|
|
|
|
|
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
|
2018-02-11 07:37:02 -05:00
|
|
|
* @throws \Exception
|
2016-10-10 15:30:27 -04:00
|
|
|
*/
|
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-12-04 11:51:39 -05:00
|
|
|
return response()->json(['message' => trans('entities.attachments_deleted')]);
|
2016-10-10 15:30:27 -04:00
|
|
|
}
|
2016-10-09 13:58:22 -04:00
|
|
|
}
|