feat: use xpier media library for uploads and markdown embeds
Store media:ID in article 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:
@@ -36,12 +36,14 @@ class ContentRenderer
|
||||
if ($format === ContentFormat::MARKDOWN) {
|
||||
$content = $this->attachEmbed->legacyToMarkdownReference($content, $articleId);
|
||||
$content = $this->attachEmbed->expandMarkdownAttachProtocol($content);
|
||||
$content = app(\App\Domain\Media\MarkdownMediaEmbed::class)->expandMarkdown($content);
|
||||
$html = $this->markdownToHtml($content);
|
||||
} else {
|
||||
$html = $content;
|
||||
}
|
||||
|
||||
$html = $this->attachEmbed->hydrateHtml($html, $articleId);
|
||||
$html = app(\App\Domain\Media\MarkdownMediaEmbed::class)->hydrateHtml($html);
|
||||
$html = Purifier::clean($html, 'article');
|
||||
|
||||
return $this->applyTocAnchors($html);
|
||||
|
||||
@@ -117,6 +117,17 @@ class ArticleCoverService
|
||||
{
|
||||
$content = (string) $article->content;
|
||||
|
||||
if (preg_match('/!\[[^\]]*\]\(media:(\d+)\)/i', $content, $matches) === 1) {
|
||||
$media = app(MarkdownMediaEmbed::class)->find((int) $matches[1]);
|
||||
if ($media !== null && app(MarkdownMediaEmbed::class)->isImage($media) && filled($media->url)) {
|
||||
return [
|
||||
'disk' => 'url',
|
||||
'path' => (string) $media->url,
|
||||
'source' => self::SOURCE_CONTENT_IMAGE,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
if (preg_match('/!\[[^\]]*\]\(attach:(\d+)\)/i', $content, $matches) === 1
|
||||
|| preg_match('/\[attach=(\d+)\]/i', $content, $matches) === 1) {
|
||||
$attachment = Attachment::query()->find((int) $matches[1]);
|
||||
|
||||
@@ -0,0 +1,131 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Domain\Media;
|
||||
|
||||
use Xpier\FilamentMediaLibrary\Models\MediaLibrary;
|
||||
|
||||
/**
|
||||
* Markdown/HTML body must store library references, never CDN URLs.
|
||||
*
|
||||
* Markdown:  or [file.pdf](media:12)
|
||||
* HTML: [media=12]
|
||||
*
|
||||
* Legacy sablog tokens ([attach=ID] / attach:ID) stay on the Attachment table.
|
||||
*/
|
||||
class MarkdownMediaEmbed
|
||||
{
|
||||
public const PROTOCOL = 'media:';
|
||||
|
||||
public function snippet(MediaLibrary $media, string $format = 'markdown'): string
|
||||
{
|
||||
$id = (int) $media->getKey();
|
||||
$isImage = $this->isImage($media);
|
||||
|
||||
if ($format === 'html') {
|
||||
return '[media='.$id.']';
|
||||
}
|
||||
|
||||
$label = $this->escapeMdLabel($this->label($media, $isImage));
|
||||
|
||||
return $isImage
|
||||
? ''
|
||||
: '['.$label.']('.self::PROTOCOL.$id.')';
|
||||
}
|
||||
|
||||
public function expandMarkdown(string $markdown): string
|
||||
{
|
||||
return preg_replace_callback(
|
||||
'/\]\(media:(\d+)\)/i',
|
||||
function (array $matches): string {
|
||||
$url = $this->publicUrl((int) $matches[1]);
|
||||
|
||||
return $url !== null ? ']('.$url.')' : $matches[0];
|
||||
},
|
||||
$markdown
|
||||
) ?? $markdown;
|
||||
}
|
||||
|
||||
public function hydrateHtml(string $html): string
|
||||
{
|
||||
$html = preg_replace_callback(
|
||||
'/\b(src|href)=([\'"])media:(\d+)\2/i',
|
||||
function (array $matches): string {
|
||||
$url = $this->publicUrl((int) $matches[3]);
|
||||
if ($url === null) {
|
||||
return $matches[0];
|
||||
}
|
||||
|
||||
return $matches[1].'='.$matches[2].$url.$matches[2];
|
||||
},
|
||||
$html
|
||||
) ?? $html;
|
||||
|
||||
return preg_replace_callback(
|
||||
'/\[media=(\d+)\]/i',
|
||||
function (array $matches): string {
|
||||
$id = (int) $matches[1];
|
||||
$media = $this->find($id);
|
||||
$url = $this->publicUrl($id);
|
||||
|
||||
if ($url === null) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if ($media && $this->isImage($media)) {
|
||||
$alt = e($this->label($media, true));
|
||||
|
||||
return '<img src="'.e($url).'" alt="'.$alt.'" />';
|
||||
}
|
||||
|
||||
$label = e($media ? $this->label($media, false) : 'file-'.$id);
|
||||
|
||||
return '<a href="'.e($url).'">'.$label.'</a>';
|
||||
},
|
||||
$html
|
||||
) ?? $html;
|
||||
}
|
||||
|
||||
public function find(int $id): ?MediaLibrary
|
||||
{
|
||||
try {
|
||||
return MediaLibrary::query()->find($id);
|
||||
} catch (\Throwable) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public function publicUrl(int $id): ?string
|
||||
{
|
||||
$media = $this->find($id);
|
||||
$url = $media?->url;
|
||||
|
||||
return filled($url) ? (string) $url : null;
|
||||
}
|
||||
|
||||
public function isImage(MediaLibrary $media): bool
|
||||
{
|
||||
if ($media->type === MediaLibrary::TYPE_IMAGE) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return is_string($media->mime_type) && str_starts_with($media->mime_type, 'image/');
|
||||
}
|
||||
|
||||
protected function label(MediaLibrary $media, bool $isImage): string
|
||||
{
|
||||
if (filled($media->alt_text)) {
|
||||
return (string) $media->alt_text;
|
||||
}
|
||||
|
||||
$name = (string) ($media->original_name ?: basename((string) $media->path) ?: 'media-'.$media->getKey());
|
||||
|
||||
return $isImage ? (pathinfo($name, PATHINFO_FILENAME) ?: $name) : $name;
|
||||
}
|
||||
|
||||
protected function escapeMdLabel(string $label): string
|
||||
{
|
||||
return str_replace(['[', ']', '!'], ['\\[', '\\]', '\\!'], $label);
|
||||
}
|
||||
}
|
||||
@@ -5,15 +5,22 @@ declare(strict_types=1);
|
||||
namespace App\Filament\Resources\Articles\Schemas;
|
||||
|
||||
use App\Domain\Blog\ContentFormat;
|
||||
use App\Domain\Media\MarkdownMediaEmbed;
|
||||
use App\Domain\Plugin\Hook;
|
||||
use App\Settings\GeneralSettings;
|
||||
use Filament\Actions\Action;
|
||||
use Filament\Forms\Components\DateTimePicker;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Components\Textarea;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Forms\Components\Toggle;
|
||||
use Filament\Notifications\Notification;
|
||||
use Filament\Schemas\Components\Actions;
|
||||
use Filament\Schemas\Components\Utilities\Get;
|
||||
use Filament\Schemas\Components\Utilities\Set;
|
||||
use Filament\Schemas\Schema;
|
||||
use Xpier\FilamentMediaLibrary\Components\MediaPicker;
|
||||
use Xpier\FilamentMediaLibrary\Models\MediaLibrary;
|
||||
|
||||
class ArticleForm
|
||||
{
|
||||
@@ -57,6 +64,37 @@ class ArticleForm
|
||||
->required()
|
||||
->rows(18)
|
||||
->columnSpanFull(),
|
||||
MediaPicker::make('insert_media_id')
|
||||
->label(__('admin.fields.insert_media'))
|
||||
->module('articles')
|
||||
->mediaType(null)
|
||||
->dehydrated(false)
|
||||
->helperText(__('admin.helpers.insert_media'))
|
||||
->columnSpanFull(),
|
||||
Actions::make([
|
||||
Action::make('insertSelectedMedia')
|
||||
->label(__('admin.actions.insert_media_into_content'))
|
||||
->action(function (Get $get, Set $set): void {
|
||||
$mediaId = $get('insert_media_id');
|
||||
$media = is_numeric($mediaId)
|
||||
? MediaLibrary::query()->find((int) $mediaId)
|
||||
: null;
|
||||
|
||||
if (! $media instanceof MediaLibrary) {
|
||||
Notification::make()->title(__('admin.notifications.pick_media_first'))->warning()->send();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$snippet = app(MarkdownMediaEmbed::class)->snippet(
|
||||
$media,
|
||||
(string) ($get('content_format') ?: ContentFormat::MARKDOWN)
|
||||
);
|
||||
$set('content', rtrim((string) $get('content'))."\n\n".$snippet."\n");
|
||||
$set('insert_media_id', null);
|
||||
Notification::make()->title(__('admin.notifications.media_inserted'))->success()->send();
|
||||
}),
|
||||
])->columnSpanFull(),
|
||||
TextInput::make('description')
|
||||
->label(__('admin.fields.description')),
|
||||
TextInput::make('keywords')
|
||||
@@ -82,6 +120,12 @@ class ArticleForm
|
||||
->label(__('admin.fields.cover_path'))
|
||||
->helperText(__('admin.helpers.cover_path'))
|
||||
->columnSpanFull(),
|
||||
MediaPicker::make('cover_media_id')
|
||||
->label(__('admin.fields.cover_media'))
|
||||
->module('articles')
|
||||
->mediaType('image')
|
||||
->relationship('coverMedia')
|
||||
->columnSpanFull(),
|
||||
TextInput::make('cover_source')
|
||||
->label(__('admin.fields.cover_source'))
|
||||
->disabled()
|
||||
|
||||
@@ -31,6 +31,11 @@ class AttachmentResource extends Resource
|
||||
return 'attachments';
|
||||
}
|
||||
|
||||
public static function shouldRegisterNavigation(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
protected static function modelKey(): string
|
||||
{
|
||||
return 'attachment';
|
||||
|
||||
@@ -13,6 +13,7 @@ use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Xpier\FilamentMediaLibrary\Models\MediaLibrary;
|
||||
|
||||
class Article extends Model
|
||||
{
|
||||
@@ -40,6 +41,7 @@ class Article extends Model
|
||||
'cover_path',
|
||||
'cover_source',
|
||||
'cover_status',
|
||||
'cover_media_id',
|
||||
'cover_generated_at',
|
||||
];
|
||||
|
||||
@@ -60,11 +62,20 @@ class Article extends Model
|
||||
|
||||
public function hasCover(): bool
|
||||
{
|
||||
if ($this->cover_media_id) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return $this->cover_status === 'ready' && filled($this->cover_path);
|
||||
}
|
||||
|
||||
public function coverUrl(): ?string
|
||||
{
|
||||
$libraryUrl = $this->coverMedia?->url;
|
||||
if (filled($libraryUrl)) {
|
||||
return (string) $libraryUrl;
|
||||
}
|
||||
|
||||
return app(ArticleCoverService::class)->publicUrl($this);
|
||||
}
|
||||
|
||||
@@ -123,6 +134,11 @@ class Article extends Model
|
||||
return $this->hasMany(Attachment::class);
|
||||
}
|
||||
|
||||
public function coverMedia(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(MediaLibrary::class, 'cover_media_id');
|
||||
}
|
||||
|
||||
public function scopeVisible(Builder $query): Builder
|
||||
{
|
||||
return $query->where('visible', true);
|
||||
|
||||
+8
-1
@@ -6,6 +6,8 @@ namespace App\Models;
|
||||
|
||||
use App\Support\LegacyPassword;
|
||||
use Database\Factories\UserFactory;
|
||||
use Filament\Models\Contracts\FilamentUser;
|
||||
use Filament\Panel;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
@@ -13,11 +15,16 @@ use Illuminate\Notifications\Notifiable;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Spatie\Permission\Traits\HasRoles;
|
||||
|
||||
class User extends Authenticatable
|
||||
class User extends Authenticatable implements FilamentUser
|
||||
{
|
||||
/** @use HasFactory<UserFactory> */
|
||||
use HasFactory, HasRoles, Notifiable;
|
||||
|
||||
public function canAccessPanel(Panel $panel): bool
|
||||
{
|
||||
return $this->hasRole(['admin', 'editor']);
|
||||
}
|
||||
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'username',
|
||||
|
||||
@@ -46,6 +46,10 @@ class AppServiceProvider extends ServiceProvider
|
||||
|
||||
public function boot(): void
|
||||
{
|
||||
config([
|
||||
'filament-media-library.navigation_group' => __('admin.groups.content'),
|
||||
]);
|
||||
|
||||
Auth::provider('larablog', function ($app, array $config) {
|
||||
return new LaraBlogUserProvider($app['hash'], $config['model'] ?? User::class);
|
||||
});
|
||||
|
||||
@@ -26,6 +26,7 @@ use Illuminate\Session\Middleware\StartSession;
|
||||
use Illuminate\Support\Facades\Blade;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\View\Middleware\ShareErrorsFromSession;
|
||||
use Xpier\FilamentMediaLibrary\Filament\MediaLibraryPlugin;
|
||||
|
||||
class AdminPanelProvider extends PanelProvider
|
||||
{
|
||||
@@ -58,6 +59,10 @@ class AdminPanelProvider extends PanelProvider
|
||||
->widgets([
|
||||
AccountWidget::class,
|
||||
])
|
||||
->plugins([
|
||||
MediaLibraryPlugin::make()
|
||||
->navigationGroup((string) (__('admin.groups.content') ?: '内容')),
|
||||
])
|
||||
->renderHook(
|
||||
PanelsRenderHook::GLOBAL_SEARCH_AFTER,
|
||||
fn (): string => Blade::render('@livewire(\'admin.clear-cache-button\')'),
|
||||
|
||||
Reference in New Issue
Block a user