protected $rules = [
'create' => [
- 'name' => 'required|min:1|max:255|string',
+ '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'
+ 'file' => 'required_without:link|file',
+ 'link' => 'required_without:file|min:1|max:255|safe_url',
],
'update' => [
- 'name' => 'min:1|max:255|string',
+ 'name' => 'min:1|max:255|string',
'uploaded_to' => 'integer|exists:pages,id',
- 'file' => 'link|file',
- 'link' => 'file|min:1|max:255|safe_url'
+ 'file' => 'file',
+ 'link' => 'min:1|max:255|safe_url',
],
];
* 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
*/
$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);
}
public function read(string $id)
{
/** @var Attachment $attachment */
- $attachment = Attachment::visible()->findOrFail($id);
+ $attachment = Attachment::visible()
+ ->with(['createdBy', 'updatedBy'])
+ ->findOrFail($id);
$attachment->setAttribute('links', [
'html' => $attachment->htmlLink(),
/**
* 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
if ($request->hasFile('file')) {
$uploadedFile = $request->file('file');
- $attachment = $this->attachmentService->saveUpdatedUpload($uploadedFile, $page->id);
+ $attachment = $this->attachmentService->saveUpdatedUpload($uploadedFile, $attachment);
}
$this->attachmentService->updateFile($attachment, $requestData);
+
return response()->json($attachment);
}
return response('', 204);
}
-
}