CatPtain commited on
Commit
7b673de
·
verified ·
1 Parent(s): 613c920

Upload screenshotService.js

Browse files
backend/src/services/screenshotService.js CHANGED
@@ -1,6 +1,4 @@
1
  import puppeteer from 'puppeteer';
2
- // 添加Playwright作为备用截图引擎
3
- import { chromium } from 'playwright';
4
 
5
  class ScreenshotService {
6
  constructor() {
@@ -13,6 +11,27 @@ class ScreenshotService {
13
  this.isClosing = false;
14
  this.isHuggingFaceSpace = process.env.SPACE_ID || process.env.HF_SPACE_ID;
15
  this.preferredEngine = 'puppeteer'; // 优先使用的截图引擎
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  }
17
 
18
  async initBrowser() {
@@ -142,6 +161,13 @@ class ScreenshotService {
142
  }
143
 
144
  async initPlaywrightBrowser() {
 
 
 
 
 
 
 
145
  if (this.playwrightBrowser) {
146
  try {
147
  const context = await this.playwrightBrowser.newContext();
@@ -460,6 +486,12 @@ class ScreenshotService {
460
  }
461
 
462
  async generateScreenshotWithPlaywright(htmlContent, options = {}) {
 
 
 
 
 
 
463
  try {
464
  console.log('🎭 开始Playwright截图生成...');
465
 
@@ -523,26 +555,44 @@ class ScreenshotService {
523
  console.log('✅ Puppeteer截图生成成功');
524
  return screenshot;
525
  } catch (puppeteerError) {
526
- console.warn('⚠️ Puppeteer截图失败,尝试Playwright:', puppeteerError.message);
527
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
528
  try {
 
529
  const screenshot = await this.generateScreenshotWithPlaywright(htmlContent, options);
530
  console.log('✅ Playwright截图生成成功');
531
- this.preferredEngine = 'playwright';
532
  return screenshot;
533
  } catch (playwrightError) {
534
- console.warn('⚠️ Playwright截图也失败,使用fallback方法:', playwrightError.message);
 
 
 
 
 
 
 
 
535
  }
536
- }
537
- } else {
538
- try {
539
- console.log('🎭 尝试使用Playwright生成截图');
540
- const screenshot = await this.generateScreenshotWithPlaywright(htmlContent, options);
541
- console.log('✅ Playwright截图生成成功');
542
- return screenshot;
543
- } catch (playwrightError) {
544
- console.warn('⚠️ Playwright截图失败,尝试Puppeteer:', playwrightError.message);
545
-
546
  try {
547
  const screenshot = await this.generateScreenshotWithPuppeteer(htmlContent, options);
548
  console.log('✅ Puppeteer截图生成成功');
@@ -553,6 +603,7 @@ class ScreenshotService {
553
  }
554
  }
555
 
 
556
  const dimensions = this.extractPPTDimensions(htmlContent);
557
  const fallbackImage = this.generateFallbackImage(
558
  dimensions.width,
@@ -564,8 +615,17 @@ class ScreenshotService {
564
  return fallbackImage;
565
  }
566
 
567
- setPreferredEngine(engine) {
568
  if (['puppeteer', 'playwright'].includes(engine)) {
 
 
 
 
 
 
 
 
 
569
  this.preferredEngine = engine;
570
  console.log(`截图引擎偏好设置为: ${engine}`);
571
  }
 
1
  import puppeteer from 'puppeteer';
 
 
2
 
3
  class ScreenshotService {
4
  constructor() {
 
11
  this.isClosing = false;
12
  this.isHuggingFaceSpace = process.env.SPACE_ID || process.env.HF_SPACE_ID;
13
  this.preferredEngine = 'puppeteer'; // 优先使用的截图引擎
14
+ this.isPlaywrightAvailable = false; // 初始为false,运行时检查
15
+ this.chromium = null; // Playwright chromium实例
16
+ }
17
+
18
+ // 动态检查和加载Playwright
19
+ async initPlaywright() {
20
+ if (this.chromium) return this.chromium; // 已经加载过
21
+
22
+ try {
23
+ console.log('🎭 尝试动态加载Playwright...');
24
+ const playwrightModule = await import('playwright');
25
+ this.chromium = playwrightModule.chromium;
26
+ this.isPlaywrightAvailable = true;
27
+ console.log('✅ Playwright动态加载成功,可作为截图备用方案');
28
+ return this.chromium;
29
+ } catch (error) {
30
+ console.warn('⚠️ Playwright动态加载失败,将仅使用Puppeteer进行截图:', error.message);
31
+ this.isPlaywrightAvailable = false;
32
+ this.chromium = null;
33
+ return null;
34
+ }
35
  }
36
 
37
  async initBrowser() {
 
161
  }
162
 
163
  async initPlaywrightBrowser() {
164
+ // 先尝试加载Playwright
165
+ const chromium = await this.initPlaywright();
166
+ if (!chromium) {
167
+ console.warn('Playwright不可用,跳过初始化');
168
+ return null;
169
+ }
170
+
171
  if (this.playwrightBrowser) {
172
  try {
173
  const context = await this.playwrightBrowser.newContext();
 
486
  }
487
 
488
  async generateScreenshotWithPlaywright(htmlContent, options = {}) {
489
+ // 先尝试加载Playwright
490
+ const chromium = await this.initPlaywright();
491
+ if (!chromium) {
492
+ throw new Error('Playwright不可用');
493
+ }
494
+
495
  try {
496
  console.log('🎭 开始Playwright截图生成...');
497
 
 
555
  console.log('✅ Puppeteer截图生成成功');
556
  return screenshot;
557
  } catch (puppeteerError) {
558
+ console.warn('⚠️ Puppeteer截图失败:', puppeteerError.message);
559
 
560
+ // 动态检查Playwright是否可用
561
+ if (await this.initPlaywright()) {
562
+ console.log('🎭 尝试使用Playwright作为备用方案...');
563
+ try {
564
+ const screenshot = await this.generateScreenshotWithPlaywright(htmlContent, options);
565
+ console.log('✅ Playwright截图生成成功');
566
+ this.preferredEngine = 'playwright';
567
+ return screenshot;
568
+ } catch (playwrightError) {
569
+ console.warn('⚠️ Playwright截图也失败,使用fallback方法:', playwrightError.message);
570
+ }
571
+ } else {
572
+ console.warn('⚠️ Playwright不可用,直接使用fallback方法');
573
+ }
574
+ }
575
+ } else {
576
+ // 如果偏好Playwright,先动态检查是否可用
577
+ if (await this.initPlaywright()) {
578
  try {
579
+ console.log('🎭 尝试使用Playwright生成截图');
580
  const screenshot = await this.generateScreenshotWithPlaywright(htmlContent, options);
581
  console.log('✅ Playwright截图生成成功');
 
582
  return screenshot;
583
  } catch (playwrightError) {
584
+ console.warn('⚠️ Playwright截图失败,尝试Puppeteer:', playwrightError.message);
585
+
586
+ try {
587
+ const screenshot = await this.generateScreenshotWithPuppeteer(htmlContent, options);
588
+ console.log('✅ Puppeteer截图生成成功');
589
+ return screenshot;
590
+ } catch (puppeteerError) {
591
+ console.warn('⚠️ Puppeteer截图也失败,使用fallback方法:', puppeteerError.message);
592
+ }
593
  }
594
+ } else {
595
+ console.warn('⚠️ Playwright不可用,直接使用Puppeteer');
 
 
 
 
 
 
 
 
596
  try {
597
  const screenshot = await this.generateScreenshotWithPuppeteer(htmlContent, options);
598
  console.log('✅ Puppeteer截图生成成功');
 
603
  }
604
  }
605
 
606
+ // 如果所有截图引擎都���败,生成fallback图片
607
  const dimensions = this.extractPPTDimensions(htmlContent);
608
  const fallbackImage = this.generateFallbackImage(
609
  dimensions.width,
 
615
  return fallbackImage;
616
  }
617
 
618
+ async setPreferredEngine(engine) {
619
  if (['puppeteer', 'playwright'].includes(engine)) {
620
+ // 检查所选引擎是否可用
621
+ if (engine === 'playwright') {
622
+ const chromium = await this.initPlaywright();
623
+ if (!chromium) {
624
+ console.warn('Playwright不可用,保持使用Puppeteer');
625
+ return;
626
+ }
627
+ }
628
+
629
  this.preferredEngine = engine;
630
  console.log(`截图引擎偏好设置为: ${engine}`);
631
  }