22
33use BookStack \Exceptions \FileUploadException ;
44use BookStack \Attachment ;
5- use BookStack \Repos \PageRepo ;
5+ use BookStack \Repos \EntityRepo ;
66use BookStack \Services \AttachmentService ;
77use Illuminate \Http \Request ;
88
99class AttachmentController extends Controller
1010{
1111 protected $ attachmentService ;
1212 protected $ attachment ;
13- protected $ pageRepo ;
13+ protected $ entityRepo ;
1414
1515 /**
1616 * AttachmentController constructor.
1717 * @param AttachmentService $attachmentService
1818 * @param Attachment $attachment
19- * @param PageRepo $pageRepo
19+ * @param EntityRepo $entityRepo
2020 */
21- public function __construct (AttachmentService $ attachmentService , Attachment $ attachment , PageRepo $ pageRepo )
21+ public function __construct (AttachmentService $ attachmentService , Attachment $ attachment , EntityRepo $ entityRepo )
2222 {
2323 $ this ->attachmentService = $ attachmentService ;
2424 $ this ->attachment = $ attachment ;
25- $ this ->pageRepo = $ pageRepo ;
25+ $ this ->entityRepo = $ entityRepo ;
2626 parent ::__construct ();
2727 }
2828
@@ -40,7 +40,7 @@ public function upload(Request $request)
4040 ]);
4141
4242 $ pageId = $ request ->get ('uploaded_to ' );
43- $ page = $ this ->pageRepo ->getById ($ pageId , true );
43+ $ page = $ this ->entityRepo ->getById (' page ' , $ pageId , true );
4444
4545 $ this ->checkPermission ('attachment-create-all ' );
4646 $ this ->checkOwnablePermission ('page-update ' , $ page );
@@ -70,14 +70,14 @@ public function uploadUpdate($attachmentId, Request $request)
7070 ]);
7171
7272 $ pageId = $ request ->get ('uploaded_to ' );
73- $ page = $ this ->pageRepo ->getById ($ pageId , true );
73+ $ page = $ this ->entityRepo ->getById (' page ' , $ pageId , true );
7474 $ attachment = $ this ->attachment ->findOrFail ($ attachmentId );
7575
7676 $ this ->checkOwnablePermission ('page-update ' , $ page );
7777 $ this ->checkOwnablePermission ('attachment-create ' , $ attachment );
7878
7979 if (intval ($ pageId ) !== intval ($ attachment ->uploaded_to )) {
80- return $ this ->jsonError (' Page mismatch during attached file update ' );
80+ return $ this ->jsonError (trans ( ' errors.attachment_page_mismatch ' ) );
8181 }
8282
8383 $ uploadedFile = $ request ->file ('file ' );
@@ -106,18 +106,18 @@ public function update($attachmentId, Request $request)
106106 ]);
107107
108108 $ pageId = $ request ->get ('uploaded_to ' );
109- $ page = $ this ->pageRepo ->getById ($ pageId , true );
109+ $ page = $ this ->entityRepo ->getById (' page ' , $ pageId , true );
110110 $ attachment = $ this ->attachment ->findOrFail ($ attachmentId );
111111
112112 $ this ->checkOwnablePermission ('page-update ' , $ page );
113113 $ this ->checkOwnablePermission ('attachment-create ' , $ attachment );
114114
115115 if (intval ($ pageId ) !== intval ($ attachment ->uploaded_to )) {
116- return $ this ->jsonError (' Page mismatch during attachment update ' );
116+ return $ this ->jsonError (trans ( ' errors.attachment_page_mismatch ' ) );
117117 }
118118
119119 $ attachment = $ this ->attachmentService ->updateFile ($ attachment , $ request ->all ());
120- return $ attachment ;
120+ return response ()-> json ( $ attachment) ;
121121 }
122122
123123 /**
@@ -134,7 +134,7 @@ public function attachLink(Request $request)
134134 ]);
135135
136136 $ pageId = $ request ->get ('uploaded_to ' );
137- $ page = $ this ->pageRepo ->getById ($ pageId , true );
137+ $ page = $ this ->entityRepo ->getById (' page ' , $ pageId , true );
138138
139139 $ this ->checkPermission ('attachment-create-all ' );
140140 $ this ->checkOwnablePermission ('page-update ' , $ page );
@@ -153,7 +153,7 @@ public function attachLink(Request $request)
153153 */
154154 public function listForPage ($ pageId )
155155 {
156- $ page = $ this ->pageRepo ->getById ($ pageId , true );
156+ $ page = $ this ->entityRepo ->getById (' page ' , $ pageId , true );
157157 $ this ->checkOwnablePermission ('page-view ' , $ page );
158158 return response ()->json ($ page ->attachments );
159159 }
@@ -170,12 +170,12 @@ public function sortForPage($pageId, Request $request)
170170 'files ' => 'required|array ' ,
171171 'files.*.id ' => 'required|integer ' ,
172172 ]);
173- $ page = $ this ->pageRepo ->getById ($ pageId );
173+ $ page = $ this ->entityRepo ->getById (' page ' , $ pageId );
174174 $ this ->checkOwnablePermission ('page-update ' , $ page );
175175
176176 $ attachments = $ request ->get ('files ' );
177177 $ this ->attachmentService ->updateFileOrderWithinPage ($ attachments , $ pageId );
178- return response ()->json (['message ' => ' Attachment order updated ' ]);
178+ return response ()->json (['message ' => trans ( ' entities.attachments_order_updated ' ) ]);
179179 }
180180
181181 /**
@@ -186,7 +186,7 @@ public function sortForPage($pageId, Request $request)
186186 public function get ($ attachmentId )
187187 {
188188 $ attachment = $ this ->attachment ->findOrFail ($ attachmentId );
189- $ page = $ this ->pageRepo ->getById ($ attachment ->uploaded_to );
189+ $ page = $ this ->entityRepo ->getById (' page ' , $ attachment ->uploaded_to );
190190 $ this ->checkOwnablePermission ('page-view ' , $ page );
191191
192192 if ($ attachment ->external ) {
@@ -210,6 +210,6 @@ public function delete($attachmentId)
210210 $ attachment = $ this ->attachment ->findOrFail ($ attachmentId );
211211 $ this ->checkOwnablePermission ('attachment-delete ' , $ attachment );
212212 $ this ->attachmentService ->deleteFile ($ attachment );
213- return response ()->json (['message ' => ' Attachment deleted ' ]);
213+ return response ()->json (['message ' => trans ( ' entities.attachments_deleted ' ) ]);
214214 }
215215}
0 commit comments