use Exception;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Http\Request;
+use Illuminate\Support\Facades\Storage;
use Illuminate\Support\MessageBag;
use Illuminate\Validation\ValidationException;
class AttachmentController extends Controller
{
- protected $attachmentService;
- protected $pageRepo;
+ protected AttachmentService $attachmentService;
+ protected PageRepo $pageRepo;
/**
* AttachmentController constructor.
public function upload(Request $request)
{
$this->validate($request, [
- 'uploaded_to' => 'required|integer|exists:pages,id',
- 'file' => 'required|file',
+ 'uploaded_to' => ['required', 'integer', 'exists:pages,id'],
+ 'file' => array_merge(['required'], $this->attachmentService->getFileValidationRules()),
]);
$pageId = $request->get('uploaded_to');
public function uploadUpdate(Request $request, $attachmentId)
{
$this->validate($request, [
- 'file' => 'required|file',
+ 'file' => array_merge(['required'], $this->attachmentService->getFileValidationRules()),
]);
+ /** @var Attachment $attachment */
$attachment = Attachment::query()->findOrFail($attachmentId);
$this->checkOwnablePermission('view', $attachment->page);
$this->checkOwnablePermission('page-update', $attachment->page);
/**
* Get the update form for an attachment.
- *
- * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function getUpdateForm(string $attachmentId)
{
+ /** @var Attachment $attachment */
$attachment = Attachment::query()->findOrFail($attachmentId);
$this->checkOwnablePermission('page-update', $attachment->page);
try {
$this->validate($request, [
- 'attachment_edit_name' => 'required|string|min:1|max:255',
- 'attachment_edit_url' => 'string|min:1|max:255|safe_url',
+ 'attachment_edit_name' => ['required', 'string', 'min:1', 'max:255'],
+ 'attachment_edit_url' => ['string', 'min:1', 'max:255', 'safe_url'],
]);
} catch (ValidationException $exception) {
return response()->view('attachments.manager-edit-form', array_merge($request->only(['attachment_edit_name', 'attachment_edit_url']), [
try {
$this->validate($request, [
- 'attachment_link_uploaded_to' => 'required|integer|exists:pages,id',
- 'attachment_link_name' => 'required|string|min:1|max:255',
- 'attachment_link_url' => 'required|string|min:1|max:255|safe_url',
+ 'attachment_link_uploaded_to' => ['required', 'integer', 'exists:pages,id'],
+ 'attachment_link_name' => ['required', 'string', 'min:1', 'max:255'],
+ 'attachment_link_url' => ['required', 'string', 'min:1', 'max:255', 'safe_url'],
]);
} catch (ValidationException $exception) {
return response()->view('attachments.manager-link-form', array_merge($request->only(['attachment_link_name', 'attachment_link_url']), [
/**
* Get the attachments for a specific page.
+ *
+ * @throws NotFoundException
*/
public function listForPage(int $pageId)
{
public function sortForPage(Request $request, int $pageId)
{
$this->validate($request, [
- 'order' => 'required|array',
+ 'order' => ['required', 'array'],
]);
$page = $this->pageRepo->getById($pageId);
$this->checkOwnablePermission('page-update', $page);
}
$fileName = $attachment->getFileName();
- $attachmentContents = $this->attachmentService->getAttachmentFromStorage($attachment);
+ $attachmentStream = $this->attachmentService->streamAttachmentFromStorage($attachment);
if ($request->get('open') === 'true') {
- return $this->inlineDownloadResponse($attachmentContents, $fileName);
+ return $this->streamedInlineDownloadResponse($attachmentStream, $fileName);
}
- return $this->downloadResponse($attachmentContents, $fileName);
+ return $this->streamedDownloadResponse($attachmentStream, $fileName);
}
/**