From 5799ec328129aad6196a9fc195067c31e0ed7e05 Mon Sep 17 00:00:00 2001 From: Richard Dern Date: Tue, 23 Apr 2024 11:58:07 +0200 Subject: [PATCH] Added tools to repair and check last modification time --- app/Classes/Bundle.php | 50 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/app/Classes/Bundle.php b/app/Classes/Bundle.php index 0940134..608dfb6 100644 --- a/app/Classes/Bundle.php +++ b/app/Classes/Bundle.php @@ -7,6 +7,7 @@ use App\Classes\Traits\ManagesMetadata; use App\Contracts\Bundles; use App\Services\BundleRenderers\Facades\BundleRenderer; +use Carbon\Carbon; use Exception; use Illuminate\Filesystem\FilesystemAdapter; use Illuminate\Support\Str; @@ -111,6 +112,7 @@ public function save(): void public function repair(): void { $this->load(); + $this->repairCover(); $this->lintMarkdown(); $this->repairAttachments(); $this->save(); @@ -140,6 +142,13 @@ public function render() return $renderer->render(); } + public function renderCard() + { + $renderer = BundleRenderer::getBundleRendererFor($this); + + return $renderer->renderCard(); + } + /** * Return a list of bundles in the specified path */ @@ -167,6 +176,47 @@ public static function findBundles(FilesystemAdapter $disk, ?string $path = '/', } } + /** + * Return the most recent modification date of this bundle and all + * sub-bundles + */ + public function lastModified(): ?Carbon + { + $dates = $this->disk + ->listContents($this->getPath(), true) + ->map(fn (StorageAttributes $attributes) => $attributes->lastModified()) + ->toArray(); + + $latestTime = 0; + + foreach ($dates as $date) { + if ($date > $latestTime) { + $latestTime = $date; + } + } + + return $latestTime == 0 ? null : Carbon::parse($latestTime); + } + + private function repairCover() + { + $cover = $this->metadata()->get('cover'); + + if (empty($cover)) { + return; + } + + if (is_array($cover)) { + $path = $cover['url']; + + foreach ($this->attachments(AttachmentsManager::Images)->manager()->get('files') as $ref => $data) { + if (Str::endsWith($data['filename'], $path)) { + $this->metadata()->set('cover', $ref); + } + } + } + } + /** * Return a normalized representation of bundle's path */