+ return $copyChapter;
+ }
+
+ /**
+ * Clone the given book.
+ * Clones all child chapters & pages.
+ */
+ public function cloneBook(Book $original, string $newName): Book
+ {
+ $bookDetails = $original->getAttributes();
+ $bookDetails['name'] = $newName;
+ $bookDetails['tags'] = $this->entityTagsToInputArray($original);
+
+ $copyBook = $this->bookRepo->create($bookDetails);
+
+ $directChildren = $original->getDirectChildren();
+ foreach ($directChildren as $child) {
+ if ($child instanceof Chapter && userCan('chapter-create', $copyBook)) {
+ $this->cloneChapter($child, $copyBook, $child->name);
+ }
+
+ if ($child instanceof Page && !$child->draft && userCan('page-create', $copyBook)) {
+ $this->clonePage($child, $copyBook, $child->name);
+ }
+ }
+
+ if ($original->cover) {
+ try {
+ $tmpImgFile = tmpfile();
+ $uploadedFile = $this->imageToUploadedFile($original->cover, $tmpImgFile);
+ $this->bookRepo->updateCoverImage($copyBook, $uploadedFile, false);
+ } catch (\Exception $exception) {
+ }
+ }
+
+ return $copyBook;
+ }
+
+ /**
+ * Convert an image instance to an UploadedFile instance to mimic
+ * a file being uploaded.
+ */
+ protected function imageToUploadedFile(Image $image, &$tmpFile): ?UploadedFile
+ {
+ $imgData = $this->imageService->getImageData($image);
+ $tmpImgFilePath = stream_get_meta_data($tmpFile)['uri'];
+ file_put_contents($tmpImgFilePath, $imgData);
+
+ return new UploadedFile($tmpImgFilePath, basename($image->path));