1
0

Added tools to repair and check last modification time

This commit is contained in:
Richard Dern 2024-04-23 11:58:07 +02:00
parent e09fc2faa1
commit 5799ec3281

View File

@ -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
*/