refactor: 重构知识库文件上传和处理, 支持 pdf

This commit is contained in:
2026-03-23 16:30:13 +08:00
parent 89af7c17f1
commit 63ea2686e1
17 changed files with 905 additions and 1782 deletions

View File

@@ -40,13 +40,13 @@ class DocumentResource extends Resource
public static function getEloquentQuery(): Builder
{
$query = parent::getEloquentQuery();
// 应用 accessibleBy 作用域,确保用户只能看到有权限的文档
$user = auth()->user();
if ($user) {
$query->accessibleBy($user);
}
return $query;
}
@@ -60,27 +60,32 @@ class DocumentResource extends Resource
->maxLength(255)
->placeholder('请输入文档标题')
->columnSpanFull(),
Forms\Components\Textarea::make('description')
->label('文档描述')
->rows(3)
->maxLength(65535)
->placeholder('请输入文档描述(可选)')
->columnSpanFull(),
Forms\Components\FileUpload::make('file')
->label('文档文件')
->required()
->acceptedFileTypes(['application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'])
->acceptedFileTypes(config('documents.supported_formats.mime_types', [
'application/msword',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'application/pdf',
'application/vnd.ms-powerpoint',
'application/vnd.openxmlformats-officedocument.presentationml.presentation',
]))
->maxSize(51200) // 50MB
->disk('local')
->directory('documents/' . date('Y/m/d'))
->visibility('private')
->downloadable()
->preserveFilenames() // 保留原始文件名
->helperText('仅支持 .doc 和 .docx 格式,最大 50MB')
->helperText('支持 .docx/.pptx/.xlsx/.pdf 格式,最大 50MB')
->columnSpanFull(),
Forms\Components\Select::make('type')
->label('文档类型')
->required()
@@ -90,18 +95,19 @@ class DocumentResource extends Resource
])
->default('global')
->reactive()
->afterStateUpdated(fn ($state, callable $set) =>
->afterStateUpdated(
fn($state, callable $set) =>
$state === 'global' ? $set('group_id', null) : null
)
->helperText('全局知识库所有用户可见,专用知识库仅指定分组可见'),
Forms\Components\Select::make('group_id')
->label('所属分组')
->relationship('group', 'name')
->searchable()
->preload()
->required(fn (Forms\Get $get): bool => $get('type') === 'dedicated')
->visible(fn (Forms\Get $get): bool => $get('type') === 'dedicated')
->required(fn(Forms\Get $get): bool => $get('type') === 'dedicated')
->visible(fn(Forms\Get $get): bool => $get('type') === 'dedicated')
->helperText('专用知识库必须选择所属分组'),
]);
}
@@ -122,51 +128,51 @@ class DocumentResource extends Resource
}
return null;
}),
Tables\Columns\TextColumn::make('type')
->label('文档类型')
->badge()
->color(fn (string $state): string => match ($state) {
->color(fn(string $state): string => match ($state) {
'global' => 'success',
'dedicated' => 'warning',
})
->formatStateUsing(fn (string $state): string => match ($state) {
->formatStateUsing(fn(string $state): string => match ($state) {
'global' => '全局知识库',
'dedicated' => '专用知识库',
default => $state,
})
->sortable(),
Tables\Columns\TextColumn::make('group.name')
->label('所属分组')
->searchable()
->sortable()
->placeholder('—')
->toggleable(),
Tables\Columns\TextColumn::make('uploader.name')
->label('上传者')
->searchable()
->sortable()
->toggleable(),
Tables\Columns\TextColumn::make('file_size')
->label('文件大小')
->formatStateUsing(fn ($state): string => self::formatFileSize($state))
->formatStateUsing(fn($state): string => self::formatFileSize($state))
->sortable()
->toggleable(),
Tables\Columns\TextColumn::make('conversion_status')
->label('转换状态')
->badge()
->color(fn (?string $state): string => match ($state) {
->color(fn(?string $state): string => match ($state) {
'completed' => 'success',
'processing' => 'info',
'pending' => 'warning',
'failed' => 'danger',
default => 'gray',
})
->formatStateUsing(fn (?string $state): string => match ($state) {
->formatStateUsing(fn(?string $state): string => match ($state) {
'completed' => '已完成',
'processing' => '转换中',
'pending' => '等待转换',
@@ -175,13 +181,13 @@ class DocumentResource extends Resource
})
->sortable()
->toggleable(),
Tables\Columns\TextColumn::make('created_at')
->label('上传时间')
->dateTime('Y年m月d日 H:i')
->sortable()
->toggleable(),
Tables\Columns\TextColumn::make('updated_at')
->label('更新时间')
->dateTime('Y年m月d日 H:i')
@@ -196,21 +202,21 @@ class DocumentResource extends Resource
'dedicated' => '专用知识库',
])
->placeholder('全部类型'),
Tables\Filters\SelectFilter::make('group_id')
->label('所属分组')
->relationship('group', 'name')
->searchable()
->preload()
->placeholder('全部分组'),
Tables\Filters\SelectFilter::make('uploaded_by')
->label('上传者')
->relationship('uploader', 'name')
->searchable()
->preload()
->placeholder('全部上传者'),
Tables\Filters\SelectFilter::make('conversion_status')
->label('转换状态')
->options([
@@ -226,32 +232,29 @@ class DocumentResource extends Resource
->label('重试转换')
->icon('heroicon-o-arrow-path')
->color('warning')
->visible(fn (Document $record): bool =>
->visible(
fn(Document $record): bool =>
in_array($record->conversion_status, ['failed', 'processing', 'pending'])
)
->requiresConfirmation()
->modalHeading('重试文档转换')
->modalDescription(fn (Document $record): string =>
'确定要重新转换文档 "' . $record->title . '" 吗?' .
"\n\n当前状态:" . match($record->conversion_status) {
'failed' => '转换失败',
'processing' => '转换中(可能卡住)',
'pending' => '等待转换',
default => $record->conversion_status,
} .
($record->conversion_error ? "\n\n错误信息:" . $record->conversion_error : '')
->modalDescription(
fn(Document $record): string =>
'确定要重新转换文档 "' . $record->title . '" 吗?' .
"\n\n当前状态:" . match ($record->conversion_status) {
'failed' => '转换失败',
'processing' => '转换中(可能卡住)',
'pending' => '等待转换',
default => $record->conversion_status,
} .
($record->conversion_error ? "\n\n错误信息:" . $record->conversion_error : '')
)
->modalSubmitActionLabel('确认重试')
->action(function (Document $record) {
try {
// 重置转换状态
$record->conversion_status = 'pending';
$record->conversion_error = null;
$record->save();
// 重新派发转换任务
\App\Jobs\ConvertDocumentToMarkdown::dispatch($record);
app(\App\Services\DocumentConversionService::class)
->queueConversion($record);
\Filament\Notifications\Notification::make()
->success()
->title('重试成功')
@@ -269,11 +272,13 @@ class DocumentResource extends Resource
->label('查看错误')
->icon('heroicon-o-exclamation-triangle')
->color('danger')
->visible(fn (Document $record): bool =>
->visible(
fn(Document $record): bool =>
$record->conversion_status === 'failed' && !empty($record->conversion_error)
)
->modalHeading('转换错误详情')
->modalContent(fn (Document $record): \Illuminate\Contracts\View\View =>
->modalContent(
fn(Document $record): \Illuminate\Contracts\View\View =>
view('filament.modals.conversion-error', [
'document' => $record,
'error' => $record->conversion_error,
@@ -285,12 +290,13 @@ class DocumentResource extends Resource
->label('预览 Markdown')
->icon('heroicon-o-eye')
->color('info')
->visible(fn (Document $record): bool => $record->conversion_status === 'completed')
->url(fn (Document $record): string => route('documents.preview', $record))
->visible(fn(Document $record): bool => $record->conversion_status === 'completed')
->url(fn(Document $record): string => route('documents.preview', $record))
->openUrlInNewTab()
->tooltip(fn (Document $record): ?string =>
$record->conversion_status !== 'completed'
? '文档尚未完成转换'
->tooltip(
fn(Document $record): ?string =>
$record->conversion_status !== 'completed'
? '文档尚未完成转换'
: null
),
Tables\Actions\Action::make('download')
@@ -300,11 +306,11 @@ class DocumentResource extends Resource
->action(function (Document $record) {
$documentService = app(\App\Services\DocumentService::class);
$user = auth()->user();
try {
// 记录下载日志
$documentService->logDownload($record, $user);
// 返回文件下载响应
return $documentService->downloadDocument($record, $user);
} catch (\Exception $e) {
@@ -313,7 +319,7 @@ class DocumentResource extends Resource
->title('下载失败')
->body($e->getMessage())
->send();
return null;
}
}),
@@ -341,13 +347,13 @@ class DocumentResource extends Resource
if ($bytes === null) {
return '—';
}
$units = ['B', 'KB', 'MB', 'GB'];
$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
$pow = min($pow, count($units) - 1);
$bytes /= (1 << (10 * $pow));
return round($bytes, 2) . ' ' . $units[$pow];
}

View File

@@ -3,8 +3,6 @@
namespace App\Filament\Resources\DocumentResource\Pages;
use App\Filament\Resources\DocumentResource;
use App\Services\DocumentService;
use Filament\Actions;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\CreateRecord;
use Illuminate\Support\Facades\Auth;
@@ -16,37 +14,28 @@ class CreateDocument extends CreateRecord
protected function mutateFormDataBeforeCreate(array $data): array
{
// 设置上传者为当前用户
$data['uploaded_by'] = Auth::id();
// 如果是全局文档,确保 group_id 为 null
if ($data['type'] === 'global') {
$data['group_id'] = null;
}
// 处理文件上传
if (isset($data['file'])) {
$filePath = $data['file'];
// 获取原始文件名(由于使用了 preserveFilenames()basename 就是原始文件名)
$originalFileName = basename($filePath);
// 保存文件信息
$data['file_path'] = $filePath;
$data['file_name'] = $originalFileName; // 保存原始文件名
$data['file_name'] = basename($filePath);
$data['file_size'] = Storage::disk('local')->size($filePath);
$data['mime_type'] = Storage::disk('local')->mimeType($filePath);
// 移除临时的 file 字段
unset($data['file']);
}
return $data;
}
protected function afterCreate(): void
{
// 文档创建后,触发转换任务
$conversionService = app(\App\Services\DocumentConversionService::class);
$conversionService->queueConversion($this->record);
}

View File

@@ -6,12 +6,15 @@ use App\Filament\Resources\DocumentResource;
use Filament\Actions;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\EditRecord;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
class EditDocument extends EditRecord
{
protected static string $resource = DocumentResource::class;
private ?string $previousFilePath = null;
protected function getHeaderActions(): array
{
return [
@@ -24,60 +27,55 @@ class EditDocument extends EditRecord
protected function mutateFormDataBeforeFill(array $data): array
{
// 将文件路径设置到 file 字段以便显示
$this->previousFilePath = $data['file_path'] ?? null;
if (isset($data['file_path'])) {
$data['file'] = $data['file_path'];
}
return $data;
}
protected function mutateFormDataBeforeSave(array $data): array
{
// 如果是全局文档,确保 group_id 为 null
if ($data['type'] === 'global') {
$data['group_id'] = null;
}
// 处理文件更新
if (isset($data['file']) && $data['file'] !== $this->record->file_path) {
$filePath = $data['file'];
// 删除旧的 Word 文件
if ($this->record->file_path && Storage::disk('local')->exists($this->record->file_path)) {
Storage::disk('local')->delete($this->record->file_path);
$currentFile = $data['file'] ?? null;
// 检测文件是否变更:与填充时记录的原始路径比较
if ($currentFile && $currentFile !== $this->previousFilePath) {
// 删除旧文件
if ($this->previousFilePath && Storage::disk('local')->exists($this->previousFilePath)) {
Storage::disk('local')->delete($this->previousFilePath);
}
// 删除旧的 Markdown 文件
if ($this->record->markdown_path && Storage::disk('markdown')->exists($this->record->markdown_path)) {
Storage::disk('markdown')->delete($this->record->markdown_path);
}
// 获取原始文件名(由于使用了 preserveFilenames()basename 就是原始文件名)
$originalFileName = basename($filePath);
// 更新文件信息
$data['file_path'] = $filePath;
$data['file_name'] = $originalFileName; // 保存原始文件名
$data['file_size'] = Storage::disk('local')->size($filePath);
$data['mime_type'] = Storage::disk('local')->mimeType($filePath);
// 重置转换状态,准备重新转换
$data['file_path'] = $currentFile;
$data['file_name'] = basename($currentFile);
$data['file_size'] = Storage::disk('local')->size($currentFile);
$data['mime_type'] = Storage::disk('local')->mimeType($currentFile);
// 重置转换状态,触发重新转换
$data['conversion_status'] = 'pending';
$data['markdown_path'] = null;
$data['markdown_preview'] = null;
$data['conversion_error'] = null;
}
// 移除临时的 file 字段
unset($data['file']);
return $data;
}
protected function afterSave(): void
{
// 如果文档的转换状态是 pending说明文件已更新需要触发重新转换
// 刷新模型以获取最新数据库状态
$this->record->refresh();
if ($this->record->conversion_status === 'pending') {
$conversionService = app(\App\Services\DocumentConversionService::class);
$conversionService->queueConversion($this->record);

View File

@@ -34,21 +34,15 @@ class ViewDocument extends ViewRecord
->modalSubmitActionLabel('确认重试')
->action(function () {
try {
// 重置转换状态
$this->record->conversion_status = 'pending';
$this->record->conversion_error = null;
$this->record->save();
// 重新派发转换任务
\App\Jobs\ConvertDocumentToMarkdown::dispatch($this->record);
app(\App\Services\DocumentConversionService::class)
->queueConversion($this->record);
Notification::make()
->success()
->title('重试成功')
->body('文档转换任务已重新加入队列,请稍后查看转换结果。')
->send();
// 刷新页面数据
$this->refreshFormData([
'conversion_status',
'conversion_error',