]> BookStack Code Mirror - bookstack/blobdiff - app/Http/Controllers/Api/AttachmentApiController.php
Fixed occurances of altered titles in search results
[bookstack] / app / Http / Controllers / Api / AttachmentApiController.php
index 7aa4ee4934b89cd7b8aec08aeff335a6fd6f2319..67815bc47bf4d47230a27787c7b50039cea06de9 100644 (file)
@@ -17,16 +17,16 @@ class AttachmentApiController extends ApiController
 
     protected $rules = [
         'create' => [
-            'name' => 'required|min:1|max:255|string',
-            'uploaded_to' => 'required|integer|exists:pages,id',
-            'file' => 'required_without:link|file',
-            'link' => 'required_without:file|min:1|max:255|safe_url'
+            'name'        => ['required', 'min:1', 'max:255', 'string'],
+            'uploaded_to' => ['required', 'integer', 'exists:pages,id'],
+            'file'        => ['required_without:link', 'file'],
+            'link'        => ['required_without:file', 'min:1', 'max:255', 'safe_url'],
         ],
         'update' => [
-            'name' => 'min:1|max:255|string',
-            'uploaded_to' => 'integer|exists:pages,id',
-            'file' => 'file',
-            'link' => 'min:1|max:255|safe_url'
+            'name'        => ['min:1', 'max:255', 'string'],
+            'uploaded_to' => ['integer', 'exists:pages,id'],
+            'file'        => ['file'],
+            'link'        => ['min:1', 'max:255', 'safe_url'],
         ],
     ];
 
@@ -52,6 +52,9 @@ class AttachmentApiController extends ApiController
      * An uploaded_to value must be provided containing an ID of the page
      * that this upload will be related to.
      *
+     * If you're uploading a file the POST data should be provided via
+     * a multipart/form-data type request instead of JSON.
+     *
      * @throws ValidationException
      * @throws FileUploadException
      */
@@ -69,11 +72,14 @@ class AttachmentApiController extends ApiController
             $attachment = $this->attachmentService->saveNewUpload($uploadedFile, $page->id);
         } else {
             $attachment = $this->attachmentService->saveNewFromLink(
-                $requestData['name'], $requestData['link'], $page->id
+                $requestData['name'],
+                $requestData['link'],
+                $page->id
             );
         }
 
         $this->attachmentService->updateFile($attachment, $requestData);
+
         return response()->json($attachment);
     }
 
@@ -108,6 +114,8 @@ class AttachmentApiController extends ApiController
 
     /**
      * Update the details of a single attachment.
+     * As per the create endpoint, if a file is being provided as the attachment content
+     * the request should be formatted as a multipart/form-data request instead of JSON.
      *
      * @throws ValidationException
      * @throws FileUploadException
@@ -135,6 +143,7 @@ class AttachmentApiController extends ApiController
         }
 
         $this->attachmentService->updateFile($attachment, $requestData);
+
         return response()->json($attachment);
     }
 
@@ -153,5 +162,4 @@ class AttachmentApiController extends ApiController
 
         return response('', 204);
     }
-
 }