fix: use pdf previews for documents

This commit is contained in:
2026-05-19 08:44:35 +08:00
parent 7e5a6a3f39
commit 63f2827cc9
14 changed files with 399 additions and 345 deletions

View File

@@ -4,9 +4,8 @@ namespace Tests\Feature;
use App\Models\Document;
use App\Models\User;
use App\Services\DocumentPreviewService;
use App\Services\DocumentPdfPreviewService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;
@@ -14,56 +13,79 @@ class DocumentPreviewServiceTest extends TestCase
{
use RefreshDatabase;
protected DocumentPreviewService $previewService;
protected DocumentPdfPreviewService $previewService;
protected function setUp(): void
{
parent::setUp();
$this->previewService = new DocumentPreviewService();
$this->previewService = new DocumentPdfPreviewService();
Storage::fake('local');
Storage::fake('previews');
config(['scout.driver' => 'null']);
}
public function test_可以检查文档是否支持预览(): void
public function test_可以检查文档是否支持_pdf_预览(): void
{
$user = User::factory()->create();
// 创建一个 .docx 文档
$document = Document::factory()->create([
'file_name' => 'test.docx',
'uploaded_by' => $user->id,
]);
$this->assertTrue($this->previewService->canPreview($document));
// 创建一个 .doc 文档
$document2 = Document::factory()->create([
'file_name' => 'test.doc',
'uploaded_by' => $user->id,
]);
$this->assertTrue($this->previewService->canPreview($document2));
// 创建一个不支持的格式
$document3 = Document::factory()->create([
'conversion_status' => 'completed',
'file_path' => 'documents/test.pdf',
'file_name' => 'test.pdf',
'mime_type' => 'application/pdf',
'uploaded_by' => $user->id,
]);
$this->assertFalse($this->previewService->canPreview($document3));
Storage::disk('local')->put($document->file_path, '%PDF-1.4 test');
$this->assertTrue($this->previewService->canPreview($document));
$document2 = Document::factory()->create([
'conversion_status' => 'pending',
'file_path' => 'documents/pending.pdf',
'file_name' => 'pending.pdf',
'mime_type' => 'application/pdf',
'uploaded_by' => $user->id,
]);
Storage::disk('local')->put($document2->file_path, '%PDF-1.4 test');
$this->assertFalse($this->previewService->canPreview($document2));
}
public function test_文档不存在时抛出异常(): void
{
$user = User::factory()->create();
$document = Document::factory()->create([
'file_path' => 'documents/2024/01/01/nonexistent.docx',
'file_name' => 'nonexistent.docx',
'conversion_status' => 'completed',
'file_path' => 'documents/2024/01/01/nonexistent.pdf',
'file_name' => 'nonexistent.pdf',
'mime_type' => 'application/pdf',
'uploaded_by' => $user->id,
]);
$this->expectException(\Exception::class);
$this->expectExceptionMessage('文档文件不存在');
$this->previewService->convertToHtml($document);
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('文档尚未完成转换或原文件不存在');
$this->previewService->getPreviewPath($document);
}
public function test_非_pdf_文件在缺少_libreoffice_时给出明确错误(): void
{
$user = User::factory()->create();
$document = Document::factory()->create([
'conversion_status' => 'completed',
'file_path' => 'documents/test.docx',
'file_name' => 'test.docx',
'mime_type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'uploaded_by' => $user->id,
]);
Storage::disk('local')->put($document->file_path, 'fake-docx-content');
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('LibreOffice');
$this->previewService->getPreviewPath($document);
}
}

View File

@@ -4,51 +4,66 @@ use App\Models\Document;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Storage;
use Spatie\Permission\Models\Permission;
uses(RefreshDatabase::class);
beforeEach(function () {
Storage::fake('local');
Storage::fake('previews');
config(['scout.driver' => 'null']);
Permission::findOrCreate('document.view', 'web');
});
test('用户可以预览已转换文档', function () {
test('用户可以打开已转换文档的 PDF 预览页', function () {
$user = User::factory()->create();
$user->givePermissionTo('document.view');
$document = Document::factory()->create([
'conversion_status' => 'completed',
'markdown_path' => 'markdown/test.md',
'file_path' => 'documents/test.pdf',
'file_name' => 'test.pdf',
'mime_type' => 'application/pdf',
]);
Storage::disk('local')->put($document->markdown_path, '# 测试标题\n\n这是测试内容。');
Storage::disk('local')->put($document->file_path, '%PDF-1.4 test');
$response = $this->actingAs($user)->get(route('documents.preview', $document));
$response->assertStatus(200);
$response->assertSee($document->title);
$response->assertSee('测试标题');
$response->assertSee(route('documents.preview-pdf', $document));
$response->assertSee('PDF 预览', false);
});
test('预览页面正确处理 Markdown 内容为空的情况', function () {
test('预览页面正确处理 PDF 预览不可用的情况', function () {
$user = User::factory()->create();
$user->givePermissionTo('document.view');
$document = Document::factory()->create([
'conversion_status' => 'completed',
'markdown_path' => null,
'file_path' => 'documents/missing.pdf',
'file_name' => 'missing.pdf',
'mime_type' => 'application/pdf',
]);
$response = $this->actingAs($user)->get(route('documents.preview', $document));
$response->assertStatus(200);
$response->assertSee('Markdown 内容为空');
$response->assertSee('PDF 预览暂不可用');
$response->assertSee('下载原始文档');
});
test('预览页面显示下载按钮', function () {
$user = User::factory()->create();
$user->givePermissionTo('document.view');
$document = Document::factory()->create([
'conversion_status' => 'completed',
'markdown_path' => 'markdown/test.md',
'file_path' => 'documents/test.pdf',
'file_name' => 'test.pdf',
'mime_type' => 'application/pdf',
]);
Storage::disk('local')->put($document->markdown_path, '# 测试');
Storage::disk('local')->put($document->file_path, '%PDF-1.4 test');
$response = $this->actingAs($user)->get(route('documents.preview', $document));
@@ -56,3 +71,21 @@ test('预览页面显示下载按钮', function () {
$response->assertSee('下载原文档');
$response->assertSee(route('documents.download', $document));
});
test('PDF 原文件直接以内联 PDF 响应预览', function () {
$user = User::factory()->create();
$user->givePermissionTo('document.view');
$document = Document::factory()->create([
'conversion_status' => 'completed',
'file_path' => 'documents/test.pdf',
'file_name' => 'test.pdf',
'mime_type' => 'application/pdf',
]);
Storage::disk('local')->put($document->file_path, '%PDF-1.4 test');
$response = $this->actingAs($user)->get(route('documents.preview-pdf', $document));
$response->assertStatus(200);
$response->assertHeader('content-type', 'application/pdf');
});