refactor: 重构知识库文件上传和处理, 支持 pdf
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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',
|
||||
|
||||
Reference in New Issue
Block a user