X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/919660678bec2b94eaa84ac60d0313f5ef07dfb7..refs/pull/2333/head:/app/Uploads/AttachmentService.php diff --git a/app/Uploads/AttachmentService.php b/app/Uploads/AttachmentService.php index 26e341111..02220771a 100644 --- a/app/Uploads/AttachmentService.php +++ b/app/Uploads/AttachmentService.php @@ -1,9 +1,8 @@ getClientOriginalName(); - $attachmentPath = $this->putFileInStorage($attachmentName, $uploadedFile); + $attachmentPath = $this->putFileInStorage($uploadedFile); $largestExistingOrder = Attachment::where('uploaded_to', '=', $page_id)->max('order'); $attachment = Attachment::forceCreate([ @@ -77,7 +76,7 @@ class AttachmentService extends UploadService } $attachmentName = $uploadedFile->getClientOriginalName(); - $attachmentPath = $this->putFileInStorage($attachmentName, $uploadedFile); + $attachmentPath = $this->putFileInStorage($uploadedFile); $attachment->name = $attachmentName; $attachment->path = $attachmentPath; @@ -110,14 +109,14 @@ class AttachmentService extends UploadService } /** - * Updates the file ordering for a listing of attached files. - * @param array $attachmentList - * @param $pageId + * Updates the ordering for a listing of attached files. */ - public function updateFileOrderWithinPage($attachmentList, $pageId) + public function updateFileOrderWithinPage(array $attachmentOrder, string $pageId) { - foreach ($attachmentList as $index => $attachment) { - Attachment::where('uploaded_to', '=', $pageId)->where('id', '=', $attachment['id'])->update(['order' => $index]); + foreach ($attachmentOrder as $index => $attachmentId) { + Attachment::query()->where('uploaded_to', '=', $pageId) + ->where('id', '=', $attachmentId) + ->update(['order' => $index]); } } @@ -176,21 +175,20 @@ class AttachmentService extends UploadService /** * Store a file in storage with the given filename - * @param $attachmentName * @param UploadedFile $uploadedFile * @return string * @throws FileUploadException */ - protected function putFileInStorage($attachmentName, UploadedFile $uploadedFile) + protected function putFileInStorage(UploadedFile $uploadedFile) { $attachmentData = file_get_contents($uploadedFile->getRealPath()); $storage = $this->getStorage(); $basePath = 'uploads/files/' . Date('Y-m-M') . '/'; - $uploadFileName = $attachmentName; + $uploadFileName = Str::random(16) . '.' . $uploadedFile->getClientOriginalExtension(); while ($storage->exists($basePath . $uploadFileName)) { - $uploadFileName = str_random(3) . $uploadFileName; + $uploadFileName = Str::random(3) . $uploadFileName; } $attachmentPath = $basePath . $uploadFileName;