X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/20e093a7a1f5bc56936c9da4456e7646c52f71ef..refs/pull/3333/head:/app/Entities/Tools/Cloner.php diff --git a/app/Entities/Tools/Cloner.php b/app/Entities/Tools/Cloner.php index d74f2f195..b4923b90a 100644 --- a/app/Entities/Tools/Cloner.php +++ b/app/Entities/Tools/Cloner.php @@ -7,12 +7,15 @@ use BookStack\Entities\Models\Book; use BookStack\Entities\Models\Chapter; use BookStack\Entities\Models\Entity; use BookStack\Entities\Models\Page; +use BookStack\Entities\Repos\BookRepo; use BookStack\Entities\Repos\ChapterRepo; use BookStack\Entities\Repos\PageRepo; +use BookStack\Uploads\Image; +use BookStack\Uploads\ImageService; +use Illuminate\Http\UploadedFile; class Cloner { - /** * @var PageRepo */ @@ -23,10 +26,22 @@ class Cloner */ protected $chapterRepo; - public function __construct(PageRepo $pageRepo, ChapterRepo $chapterRepo) + /** + * @var BookRepo + */ + protected $bookRepo; + + /** + * @var ImageService + */ + protected $imageService; + + public function __construct(PageRepo $pageRepo, ChapterRepo $chapterRepo, BookRepo $bookRepo, ImageService $imageService) { $this->pageRepo = $pageRepo; $this->chapterRepo = $chapterRepo; + $this->bookRepo = $bookRepo; + $this->imageService = $imageService; } /** @@ -66,6 +81,54 @@ class Cloner 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)); + } + /** * Convert the tags on the given entity to the raw format * that's used for incoming request data. @@ -81,5 +144,4 @@ class Cloner return $tags; } - -} \ No newline at end of file +}