Initial Commit
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\Post;
|
||||
|
||||
class PostMediaCacher extends MediaCacher
|
||||
{
|
||||
public function cache(Post $post): void
|
||||
{
|
||||
$dirty = false;
|
||||
|
||||
if ($resolved = $this->resolveUrl($post->image_url, "posts/images/{$post->id}")) {
|
||||
$post->image_url = $resolved;
|
||||
$dirty = true;
|
||||
}
|
||||
|
||||
if ($resolved = $this->resolveUrl($post->video_url, "posts/videos/{$post->id}")) {
|
||||
$post->video_url = $resolved;
|
||||
$dirty = true;
|
||||
}
|
||||
|
||||
if ($carousel = $post->carousel_images) {
|
||||
$changed = false;
|
||||
|
||||
foreach ($carousel as $i => &$item) {
|
||||
if (! empty($item['url']) && $resolved = $this->resolveUrl($item['url'], "posts/carousel_images/{$post->id}/{$i}")) {
|
||||
$item['url'] = $resolved;
|
||||
$changed = true;
|
||||
}
|
||||
|
||||
if (! empty($item['video_url']) && $resolved = $this->resolveUrl($item['video_url'], "posts/carousel_images/{$post->id}/{$i}-video")) {
|
||||
$item['video_url'] = $resolved;
|
||||
$changed = true;
|
||||
}
|
||||
}
|
||||
unset($item);
|
||||
|
||||
if ($changed) {
|
||||
$post->carousel_images = $carousel;
|
||||
$dirty = true;
|
||||
}
|
||||
}
|
||||
|
||||
if ($dirty) {
|
||||
$post->saveQuietly();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user