2016-10-10 21:30:27 +02:00
|
|
|
<?php namespace BookStack\Http\Controllers;
|
2016-10-09 19:58:22 +02:00
|
|
|
|
2018-09-25 17:58:03 +02:00
|
|
|
use BookStack\Entities\EntityRepo;
|
2016-10-09 19:58:22 +02:00
|
|
|
use BookStack\Exceptions\FileUploadException;
|
2018-02-11 13:37:02 +01:00
|
|
|
use BookStack\Exceptions\NotFoundException;
|
2018-09-25 17:58:03 +02:00
|
|
|
use BookStack\Uploads\Attachment;
|
2018-09-25 13:30:50 +02:00
|
|
|
use BookStack\Uploads\AttachmentService;
|
2016-10-09 19:58:22 +02:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
2016-11-12 15:12:26 +01:00
|
|
|
class AttachmentController extends Controller
|
2016-10-09 19:58:22 +02:00
|
|
|
{
|
2016-11-12 15:12:26 +01:00
|
|
|
protected $attachmentService;
|
|
|
|
protected $attachment;
|
2017-01-01 17:05:44 +01:00
|
|
|
protected $entityRepo;
|
2016-10-09 19:58:22 +02:00
|
|
|
|
|
|
|
/**
|
2016-11-12 15:12:26 +01:00
|
|
|
* AttachmentController constructor.
|
2018-09-25 13:30:50 +02:00
|
|
|
* @param \BookStack\Uploads\AttachmentService $attachmentService
|
2016-11-12 15:12:26 +01:00
|
|
|
* @param Attachment $attachment
|
2017-01-02 12:07:27 +01:00
|
|
|
* @param EntityRepo $entityRepo
|
2016-10-09 19:58:22 +02:00
|
|
|
*/
|
2017-01-02 12:07:27 +01:00
|
|
|
public function __construct(AttachmentService $attachmentService, Attachment $attachment, EntityRepo $entityRepo)
|
2016-10-09 19:58:22 +02:00
|
|
|
{
|
2016-11-12 15:12:26 +01:00
|
|
|
$this->attachmentService = $attachmentService;
|
|
|
|
$this->attachment = $attachment;
|
2017-01-01 17:05:44 +01:00
|
|
|
$this->entityRepo = $entityRepo;
|
2016-11-12 15:12:26 +01:00
|
|
|
parent::__construct();
|
2016-10-09 19:58:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2016-11-12 15:12:26 +01:00
|
|
|
* Endpoint at which attachments are uploaded to.
|
2016-10-09 19:58:22 +02:00
|
|
|
* @param Request $request
|
2016-11-12 15:12:26 +01:00
|
|
|
* @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\Response
|
2016-10-09 19:58:22 +02:00
|
|
|
*/
|
|
|
|
public function upload(Request $request)
|
|
|
|
{
|
|
|
|
$this->validate($request, [
|
2016-10-10 22:13:18 +02:00
|
|
|
'uploaded_to' => 'required|integer|exists:pages,id',
|
|
|
|
'file' => 'required|file'
|
2016-10-09 19:58:22 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
$pageId = $request->get('uploaded_to');
|
2017-01-01 17:05:44 +01:00
|
|
|
$page = $this->entityRepo->getById('page', $pageId, true);
|
2016-10-10 21:30:27 +02:00
|
|
|
|
2016-11-12 15:12:26 +01:00
|
|
|
$this->checkPermission('attachment-create-all');
|
2016-10-10 21:30:27 +02:00
|
|
|
$this->checkOwnablePermission('page-update', $page);
|
|
|
|
|
|
|
|
$uploadedFile = $request->file('file');
|
2016-10-09 19:58:22 +02:00
|
|
|
|
|
|
|
try {
|
2016-11-12 15:12:26 +01:00
|
|
|
$attachment = $this->attachmentService->saveNewUpload($uploadedFile, $pageId);
|
2016-10-09 19:58:22 +02:00
|
|
|
} catch (FileUploadException $e) {
|
|
|
|
return response($e->getMessage(), 500);
|
|
|
|
}
|
|
|
|
|
2016-11-12 15:12:26 +01:00
|
|
|
return response()->json($attachment);
|
2016-10-09 19:58:22 +02:00
|
|
|
}
|
|
|
|
|
2016-10-11 21:39:11 +02:00
|
|
|
/**
|
2016-11-12 15:12:26 +01:00
|
|
|
* Update an uploaded attachment.
|
|
|
|
* @param int $attachmentId
|
2016-10-11 21:39:11 +02:00
|
|
|
* @param Request $request
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2016-11-12 15:12:26 +01:00
|
|
|
public function uploadUpdate($attachmentId, Request $request)
|
2016-10-11 21:39:11 +02:00
|
|
|
{
|
|
|
|
$this->validate($request, [
|
|
|
|
'uploaded_to' => 'required|integer|exists:pages,id',
|
|
|
|
'file' => 'required|file'
|
|
|
|
]);
|
|
|
|
|
|
|
|
$pageId = $request->get('uploaded_to');
|
2017-01-01 17:05:44 +01:00
|
|
|
$page = $this->entityRepo->getById('page', $pageId, true);
|
2016-11-12 15:12:26 +01:00
|
|
|
$attachment = $this->attachment->findOrFail($attachmentId);
|
2016-10-11 21:39:11 +02:00
|
|
|
|
|
|
|
$this->checkOwnablePermission('page-update', $page);
|
2016-11-12 15:12:26 +01:00
|
|
|
$this->checkOwnablePermission('attachment-create', $attachment);
|
2016-10-11 21:39:11 +02:00
|
|
|
|
2016-11-12 15:12:26 +01:00
|
|
|
if (intval($pageId) !== intval($attachment->uploaded_to)) {
|
2016-12-04 17:51:39 +01:00
|
|
|
return $this->jsonError(trans('errors.attachment_page_mismatch'));
|
2016-10-11 21:39:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$uploadedFile = $request->file('file');
|
|
|
|
|
|
|
|
try {
|
2016-11-12 15:12:26 +01:00
|
|
|
$attachment = $this->attachmentService->saveUpdatedUpload($uploadedFile, $attachment);
|
2016-10-11 21:39:11 +02:00
|
|
|
} catch (FileUploadException $e) {
|
|
|
|
return response($e->getMessage(), 500);
|
|
|
|
}
|
|
|
|
|
2016-11-12 15:12:26 +01:00
|
|
|
return response()->json($attachment);
|
2016-10-11 21:39:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the details of an existing file.
|
2016-11-12 15:12:26 +01:00
|
|
|
* @param $attachmentId
|
2016-10-11 21:39:11 +02:00
|
|
|
* @param Request $request
|
2016-11-12 15:12:26 +01:00
|
|
|
* @return Attachment|mixed
|
2016-10-11 21:39:11 +02:00
|
|
|
*/
|
2016-11-12 15:12:26 +01:00
|
|
|
public function update($attachmentId, Request $request)
|
2016-10-11 21:39:11 +02:00
|
|
|
{
|
|
|
|
$this->validate($request, [
|
|
|
|
'uploaded_to' => 'required|integer|exists:pages,id',
|
2016-10-23 18:55:48 +02:00
|
|
|
'name' => 'required|string|min:1|max:255',
|
2018-05-20 12:06:10 +02:00
|
|
|
'link' => 'string|min:1|max:255'
|
2016-10-11 21:39:11 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
$pageId = $request->get('uploaded_to');
|
2017-01-01 17:05:44 +01:00
|
|
|
$page = $this->entityRepo->getById('page', $pageId, true);
|
2016-11-12 15:12:26 +01:00
|
|
|
$attachment = $this->attachment->findOrFail($attachmentId);
|
2016-10-11 21:39:11 +02:00
|
|
|
|
|
|
|
$this->checkOwnablePermission('page-update', $page);
|
2016-11-12 15:12:26 +01:00
|
|
|
$this->checkOwnablePermission('attachment-create', $attachment);
|
2016-10-11 21:39:11 +02:00
|
|
|
|
2016-11-12 15:12:26 +01:00
|
|
|
if (intval($pageId) !== intval($attachment->uploaded_to)) {
|
2016-12-04 17:51:39 +01:00
|
|
|
return $this->jsonError(trans('errors.attachment_page_mismatch'));
|
2016-10-11 21:39:11 +02:00
|
|
|
}
|
|
|
|
|
2016-11-12 15:12:26 +01:00
|
|
|
$attachment = $this->attachmentService->updateFile($attachment, $request->all());
|
2016-12-31 15:27:40 +01:00
|
|
|
return response()->json($attachment);
|
2016-10-11 21:39:11 +02:00
|
|
|
}
|
|
|
|
|
2016-10-10 22:13:18 +02:00
|
|
|
/**
|
2016-11-12 15:12:26 +01:00
|
|
|
* Attach a link to a page.
|
2016-10-10 22:13:18 +02:00
|
|
|
* @param Request $request
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function attachLink(Request $request)
|
|
|
|
{
|
|
|
|
$this->validate($request, [
|
|
|
|
'uploaded_to' => 'required|integer|exists:pages,id',
|
2016-10-23 18:55:48 +02:00
|
|
|
'name' => 'required|string|min:1|max:255',
|
2018-05-20 12:06:10 +02:00
|
|
|
'link' => 'required|string|min:1|max:255'
|
2016-10-10 22:13:18 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
$pageId = $request->get('uploaded_to');
|
2017-01-01 17:05:44 +01:00
|
|
|
$page = $this->entityRepo->getById('page', $pageId, true);
|
2016-10-10 22:13:18 +02:00
|
|
|
|
2016-11-12 15:12:26 +01:00
|
|
|
$this->checkPermission('attachment-create-all');
|
2016-10-10 22:13:18 +02:00
|
|
|
$this->checkOwnablePermission('page-update', $page);
|
|
|
|
|
2016-11-12 15:12:26 +01:00
|
|
|
$attachmentName = $request->get('name');
|
2016-10-10 22:13:18 +02:00
|
|
|
$link = $request->get('link');
|
2016-11-12 15:12:26 +01:00
|
|
|
$attachment = $this->attachmentService->saveNewFromLink($attachmentName, $link, $pageId);
|
2016-10-10 22:13:18 +02:00
|
|
|
|
2016-11-12 15:12:26 +01:00
|
|
|
return response()->json($attachment);
|
2016-10-10 22:13:18 +02:00
|
|
|
}
|
|
|
|
|
2016-10-09 19:58:22 +02:00
|
|
|
/**
|
2016-11-12 15:12:26 +01:00
|
|
|
* Get the attachments for a specific page.
|
2016-10-09 19:58:22 +02:00
|
|
|
* @param $pageId
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2016-10-10 21:30:27 +02:00
|
|
|
public function listForPage($pageId)
|
2016-10-09 19:58:22 +02:00
|
|
|
{
|
2017-01-01 17:05:44 +01:00
|
|
|
$page = $this->entityRepo->getById('page', $pageId, true);
|
2016-10-10 21:30:27 +02:00
|
|
|
$this->checkOwnablePermission('page-view', $page);
|
2016-11-12 15:21:54 +01:00
|
|
|
return response()->json($page->attachments);
|
2016-10-09 19:58:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-12 15:12:26 +01:00
|
|
|
* Update the attachment sorting.
|
2016-10-09 19:58:22 +02:00
|
|
|
* @param $pageId
|
|
|
|
* @param Request $request
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2016-10-10 21:30:27 +02:00
|
|
|
public function sortForPage($pageId, Request $request)
|
2016-10-09 19:58:22 +02:00
|
|
|
{
|
|
|
|
$this->validate($request, [
|
|
|
|
'files' => 'required|array',
|
|
|
|
'files.*.id' => 'required|integer',
|
|
|
|
]);
|
2017-01-01 17:05:44 +01:00
|
|
|
$page = $this->entityRepo->getById('page', $pageId);
|
2016-10-10 21:30:27 +02:00
|
|
|
$this->checkOwnablePermission('page-update', $page);
|
|
|
|
|
2016-11-12 15:12:26 +01:00
|
|
|
$attachments = $request->get('files');
|
|
|
|
$this->attachmentService->updateFileOrderWithinPage($attachments, $pageId);
|
2016-12-04 17:51:39 +01:00
|
|
|
return response()->json(['message' => trans('entities.attachments_order_updated')]);
|
2016-10-09 19:58:22 +02:00
|
|
|
}
|
|
|
|
|
2016-10-10 21:30:27 +02:00
|
|
|
/**
|
2016-11-12 15:12:26 +01: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 13:37:02 +01:00
|
|
|
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
|
2018-05-20 12:06:10 +02:00
|
|
|
* @throws NotFoundException
|
2016-10-10 21:30:27 +02:00
|
|
|
*/
|
2016-11-12 15:12:26 +01:00
|
|
|
public function get($attachmentId)
|
2016-10-10 21:30:27 +02:00
|
|
|
{
|
2016-11-12 15:12:26 +01:00
|
|
|
$attachment = $this->attachment->findOrFail($attachmentId);
|
2017-01-01 17:05:44 +01:00
|
|
|
$page = $this->entityRepo->getById('page', $attachment->uploaded_to);
|
2018-02-11 13:37:02 +01:00
|
|
|
if ($page === null) {
|
|
|
|
throw new NotFoundException(trans('errors.attachment_not_found'));
|
|
|
|
}
|
|
|
|
|
2016-10-10 21:30:27 +02:00
|
|
|
$this->checkOwnablePermission('page-view', $page);
|
2016-10-09 19:58:22 +02:00
|
|
|
|
2016-11-12 15:12:26 +01:00
|
|
|
if ($attachment->external) {
|
|
|
|
return redirect($attachment->path);
|
2016-10-10 22:13:18 +02:00
|
|
|
}
|
|
|
|
|
2016-11-12 15:12:26 +01:00
|
|
|
$attachmentContents = $this->attachmentService->getAttachmentFromStorage($attachment);
|
2018-09-22 12:34:09 +02:00
|
|
|
return $this->downloadResponse($attachmentContents, $attachment->getFileName());
|
2016-10-10 21:30:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-12 15:12:26 +01:00
|
|
|
* Delete a specific attachment in the system.
|
|
|
|
* @param $attachmentId
|
2016-10-10 21:30:27 +02:00
|
|
|
* @return mixed
|
2018-02-11 13:37:02 +01:00
|
|
|
* @throws \Exception
|
2016-10-10 21:30:27 +02:00
|
|
|
*/
|
2016-11-12 15:12:26 +01:00
|
|
|
public function delete($attachmentId)
|
2016-10-10 21:30:27 +02:00
|
|
|
{
|
2016-11-12 15:12:26 +01:00
|
|
|
$attachment = $this->attachment->findOrFail($attachmentId);
|
|
|
|
$this->checkOwnablePermission('attachment-delete', $attachment);
|
|
|
|
$this->attachmentService->deleteFile($attachment);
|
2016-12-04 17:51:39 +01:00
|
|
|
return response()->json(['message' => trans('entities.attachments_deleted')]);
|
2016-10-10 21:30:27 +02:00
|
|
|
}
|
2016-10-09 19:58:22 +02:00
|
|
|
}
|