feat: use xpier media library for uploads and markdown embeds
Store media:ID in post bodies instead of CDN URLs, and keep sablog attach tokens as a fallback. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -6,8 +6,10 @@ namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Spatie\MediaLibrary\HasMedia;
|
||||
use Xpier\FilamentMediaLibrary\Models\MediaLibrary;
|
||||
use Spatie\MediaLibrary\InteractsWithMedia;
|
||||
|
||||
class Category extends Model implements HasMedia
|
||||
@@ -21,6 +23,7 @@ class Category extends Model implements HasMedia
|
||||
'display_order',
|
||||
'post_count',
|
||||
'description',
|
||||
'cover_media_id',
|
||||
];
|
||||
|
||||
public function posts(): HasMany
|
||||
@@ -28,6 +31,21 @@ class Category extends Model implements HasMedia
|
||||
return $this->hasMany(Post::class);
|
||||
}
|
||||
|
||||
public function coverMedia(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(MediaLibrary::class, 'cover_media_id');
|
||||
}
|
||||
|
||||
public function coverImageUrl(): ?string
|
||||
{
|
||||
$libraryUrl = $this->coverMedia?->url;
|
||||
if (filled($libraryUrl)) {
|
||||
return (string) $libraryUrl;
|
||||
}
|
||||
|
||||
return $this->getFirstMedia('cover')?->getUrl();
|
||||
}
|
||||
|
||||
public function getUrlAttribute(): string
|
||||
{
|
||||
return route('category.show', $this->slug ?? $this->id);
|
||||
|
||||
+26
-2
@@ -11,6 +11,7 @@ use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Spatie\MediaLibrary\HasMedia;
|
||||
use Xpier\FilamentMediaLibrary\Models\MediaLibrary;
|
||||
use Spatie\MediaLibrary\InteractsWithMedia;
|
||||
use Spatie\MediaLibrary\MediaCollections\Models\Media;
|
||||
use Spatie\Tags\HasTags;
|
||||
@@ -30,6 +31,7 @@ class Post extends Model implements HasMedia
|
||||
'content',
|
||||
'content_format',
|
||||
'keywords',
|
||||
'cover_media_id',
|
||||
'status',
|
||||
'is_sticky',
|
||||
'close_comment',
|
||||
@@ -57,6 +59,26 @@ class Post extends Model implements HasMedia
|
||||
return $this->belongsTo(User::class, 'user_id');
|
||||
}
|
||||
|
||||
public function coverMedia(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(MediaLibrary::class, 'cover_media_id');
|
||||
}
|
||||
|
||||
public function coverImageUrl(?string $conversion = 'card'): ?string
|
||||
{
|
||||
$libraryUrl = $this->coverMedia?->url;
|
||||
if (filled($libraryUrl)) {
|
||||
return (string) $libraryUrl;
|
||||
}
|
||||
|
||||
$cover = $this->getFirstMedia('cover');
|
||||
if ($cover === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $cover->getUrl($conversion) ?: $cover->getUrl();
|
||||
}
|
||||
|
||||
public function comments(): HasMany
|
||||
{
|
||||
return $this->hasMany(Comment::class);
|
||||
@@ -133,8 +155,10 @@ class Post extends Model implements HasMedia
|
||||
|
||||
$text = strip_tags($this->content);
|
||||
// 付费块不进入摘要,避免付费内容泄漏到列表页/SEO meta
|
||||
$text = preg_replace('/\[paid\].*?\[\/paid\]/s', '', $text);
|
||||
$text = preg_replace('/\[[^\]]*\]/', '', $text);
|
||||
$text = preg_replace('/\[paid\].*?\[\/paid\]/s', '', $text) ?? $text;
|
||||
$text = preg_replace('/!\[([^\]]*)\]\((?:media|attach):\d+\)/i', '$1', $text) ?? $text;
|
||||
$text = preg_replace('/\[([^\]]*)\]\((?:media|attach):\d+\)/i', '$1', $text) ?? $text;
|
||||
$text = preg_replace('/\[[^\]]*\]/', '', $text) ?? $text;
|
||||
|
||||
return mb_substr($text, 0, 200);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user