1 <?php namespace BookStack\Http\Controllers;
3 use BookStack\Exceptions\FileUploadException;
5 use BookStack\Repos\PageRepo;
6 use BookStack\Services\FileService;
7 use Illuminate\Http\Request;
9 use BookStack\Http\Requests;
11 class FileController extends Controller
13 protected $fileService;
18 * FileController constructor.
19 * @param FileService $fileService
21 * @param PageRepo $pageRepo
23 public function __construct(FileService $fileService, File $file, PageRepo $pageRepo)
25 $this->fileService = $fileService;
27 $this->pageRepo = $pageRepo;
32 * Endpoint at which files are uploaded to.
33 * @param Request $request
35 public function upload(Request $request)
37 // TODO - ensure uploads are deleted on page delete.
38 $this->validate($request, [
39 'uploaded_to' => 'required|integer|exists:pages,id',
40 'file' => 'required|file'
43 $pageId = $request->get('uploaded_to');
44 $page = $this->pageRepo->getById($pageId);
46 $this->checkPermission('file-create-all');
47 $this->checkOwnablePermission('page-update', $page);
49 $uploadedFile = $request->file('file');
52 $file = $this->fileService->saveNewUpload($uploadedFile, $pageId);
53 } catch (FileUploadException $e) {
54 return response($e->getMessage(), 500);
57 return response()->json($file);
61 * Attach a link to a page as a file.
62 * @param Request $request
65 public function attachLink(Request $request)
67 $this->validate($request, [
68 'uploaded_to' => 'required|integer|exists:pages,id',
73 $pageId = $request->get('uploaded_to');
74 $page = $this->pageRepo->getById($pageId);
76 $this->checkPermission('file-create-all');
77 $this->checkOwnablePermission('page-update', $page);
79 $fileName = $request->get('name');
80 $link = $request->get('link');
81 $file = $this->fileService->saveNewFromLink($fileName, $link, $pageId);
83 return response()->json($file);
87 * Get the files for a specific page.
91 public function listForPage($pageId)
93 $page = $this->pageRepo->getById($pageId);
94 $this->checkOwnablePermission('page-view', $page);
95 return response()->json($page->files);
99 * Update the file sorting.
101 * @param Request $request
104 public function sortForPage($pageId, Request $request)
106 $this->validate($request, [
107 'files' => 'required|array',
108 'files.*.id' => 'required|integer',
110 $page = $this->pageRepo->getById($pageId);
111 $this->checkOwnablePermission('page-update', $page);
113 $files = $request->get('files');
114 $this->fileService->updateFileOrderWithinPage($files, $pageId);
115 return response()->json(['message' => 'Attachment order updated']);
119 * Get a file from storage.
122 public function get($fileId)
124 $file = $this->file->findOrFail($fileId);
125 $page = $this->pageRepo->getById($file->uploaded_to);
126 $this->checkOwnablePermission('page-view', $page);
128 if ($file->external) {
129 return redirect($file->path);
132 $fileContents = $this->fileService->getFile($file);
133 return response($fileContents, 200, [
134 'Content-Type' => 'application/octet-stream',
135 'Content-Disposition' => 'attachment; filename="'. $file->name .'"'
140 * Delete a specific file in the system.
144 public function delete($fileId)
146 $file = $this->file->findOrFail($fileId);
147 $this->checkOwnablePermission('file-delete', $file);
148 $this->fileService->deleteFile($file);
149 return response()->json(['message' => 'Attachment deleted']);