Upload public.js
Browse files- backend/src/routes/public.js +34 -4
backend/src/routes/public.js
CHANGED
|
@@ -467,20 +467,26 @@ router.post('/generate-share-link', async (req, res, next) => {
|
|
| 467 |
// 截图功能 - 返回JPEG图片
|
| 468 |
router.get('/screenshot/:userId/:pptId/:slideIndex?', async (req, res, next) => {
|
| 469 |
try {
|
|
|
|
|
|
|
| 470 |
const { userId, pptId, slideIndex = 0 } = req.params;
|
| 471 |
const slideIdx = parseInt(slideIndex);
|
| 472 |
const fileName = `${pptId}.json`;
|
| 473 |
const storageService = getStorageService();
|
| 474 |
|
|
|
|
|
|
|
| 475 |
let pptData = null;
|
| 476 |
|
| 477 |
// 获取PPT数据(复用现有逻辑)
|
| 478 |
if (storageService === githubService && storageService.repositories) {
|
|
|
|
| 479 |
for (let i = 0; i < storageService.repositories.length; i++) {
|
| 480 |
try {
|
| 481 |
const result = await storageService.getFile(userId, fileName, i);
|
| 482 |
if (result) {
|
| 483 |
pptData = result.content;
|
|
|
|
| 484 |
break;
|
| 485 |
}
|
| 486 |
} catch (error) {
|
|
@@ -488,46 +494,70 @@ router.get('/screenshot/:userId/:pptId/:slideIndex?', async (req, res, next) =>
|
|
| 488 |
}
|
| 489 |
}
|
| 490 |
} else {
|
|
|
|
| 491 |
const result = await storageService.getFile(userId, fileName);
|
| 492 |
if (result) {
|
| 493 |
pptData = result.content;
|
|
|
|
| 494 |
}
|
| 495 |
}
|
| 496 |
|
| 497 |
// 如果GitHub失败,尝试内存存储fallback
|
| 498 |
if (!pptData && storageService === githubService) {
|
|
|
|
| 499 |
try {
|
| 500 |
const memoryResult = await memoryStorageService.getFile(userId, fileName);
|
| 501 |
if (memoryResult) {
|
| 502 |
pptData = memoryResult.content;
|
|
|
|
| 503 |
}
|
| 504 |
} catch (memoryError) {
|
| 505 |
-
|
| 506 |
}
|
| 507 |
}
|
| 508 |
|
| 509 |
if (!pptData) {
|
|
|
|
| 510 |
return res.status(404).json({ error: 'PPT not found' });
|
| 511 |
}
|
| 512 |
|
| 513 |
if (slideIdx >= pptData.slides.length || slideIdx < 0) {
|
|
|
|
| 514 |
return res.status(404).json({ error: 'Slide not found' });
|
| 515 |
}
|
| 516 |
|
|
|
|
| 517 |
// 生成HTML内容(复用现有函数)
|
| 518 |
const htmlContent = generateSlideHTML(pptData, slideIdx, pptData.title);
|
| 519 |
|
|
|
|
| 520 |
// 生成截图
|
| 521 |
const screenshot = await screenshotService.generateScreenshot(htmlContent);
|
| 522 |
|
|
|
|
|
|
|
| 523 |
// 返回图片
|
| 524 |
res.setHeader('Content-Type', 'image/jpeg');
|
| 525 |
-
res.setHeader('Cache-Control', 'public, max-age=
|
| 526 |
res.setHeader('Content-Disposition', `inline; filename="${pptData.title}-${slideIdx + 1}.jpg"`);
|
| 527 |
res.send(screenshot);
|
| 528 |
} catch (error) {
|
| 529 |
-
console.error('Screenshot
|
| 530 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 531 |
}
|
| 532 |
});
|
| 533 |
|
|
|
|
| 467 |
// 截图功能 - 返回JPEG图片
|
| 468 |
router.get('/screenshot/:userId/:pptId/:slideIndex?', async (req, res, next) => {
|
| 469 |
try {
|
| 470 |
+
console.log('Screenshot request received:', req.params);
|
| 471 |
+
|
| 472 |
const { userId, pptId, slideIndex = 0 } = req.params;
|
| 473 |
const slideIdx = parseInt(slideIndex);
|
| 474 |
const fileName = `${pptId}.json`;
|
| 475 |
const storageService = getStorageService();
|
| 476 |
|
| 477 |
+
console.log(`Generating screenshot for: ${userId}/${pptId}/${slideIdx}`);
|
| 478 |
+
|
| 479 |
let pptData = null;
|
| 480 |
|
| 481 |
// 获取PPT数据(复用现有逻辑)
|
| 482 |
if (storageService === githubService && storageService.repositories) {
|
| 483 |
+
console.log('Checking GitHub repositories...');
|
| 484 |
for (let i = 0; i < storageService.repositories.length; i++) {
|
| 485 |
try {
|
| 486 |
const result = await storageService.getFile(userId, fileName, i);
|
| 487 |
if (result) {
|
| 488 |
pptData = result.content;
|
| 489 |
+
console.log(`PPT data found in repository ${i}`);
|
| 490 |
break;
|
| 491 |
}
|
| 492 |
} catch (error) {
|
|
|
|
| 494 |
}
|
| 495 |
}
|
| 496 |
} else {
|
| 497 |
+
console.log('Checking memory storage...');
|
| 498 |
const result = await storageService.getFile(userId, fileName);
|
| 499 |
if (result) {
|
| 500 |
pptData = result.content;
|
| 501 |
+
console.log('PPT data found in memory storage');
|
| 502 |
}
|
| 503 |
}
|
| 504 |
|
| 505 |
// 如果GitHub失败,尝试内存存储fallback
|
| 506 |
if (!pptData && storageService === githubService) {
|
| 507 |
+
console.log('Trying memory storage fallback...');
|
| 508 |
try {
|
| 509 |
const memoryResult = await memoryStorageService.getFile(userId, fileName);
|
| 510 |
if (memoryResult) {
|
| 511 |
pptData = memoryResult.content;
|
| 512 |
+
console.log('PPT data found in memory storage fallback');
|
| 513 |
}
|
| 514 |
} catch (memoryError) {
|
| 515 |
+
console.log('Memory storage fallback failed:', memoryError.message);
|
| 516 |
}
|
| 517 |
}
|
| 518 |
|
| 519 |
if (!pptData) {
|
| 520 |
+
console.log('PPT not found');
|
| 521 |
return res.status(404).json({ error: 'PPT not found' });
|
| 522 |
}
|
| 523 |
|
| 524 |
if (slideIdx >= pptData.slides.length || slideIdx < 0) {
|
| 525 |
+
console.log('Slide index out of bounds');
|
| 526 |
return res.status(404).json({ error: 'Slide not found' });
|
| 527 |
}
|
| 528 |
|
| 529 |
+
console.log('Generating HTML content...');
|
| 530 |
// 生成HTML内容(复用现有函数)
|
| 531 |
const htmlContent = generateSlideHTML(pptData, slideIdx, pptData.title);
|
| 532 |
|
| 533 |
+
console.log('Calling screenshot service...');
|
| 534 |
// 生成截图
|
| 535 |
const screenshot = await screenshotService.generateScreenshot(htmlContent);
|
| 536 |
|
| 537 |
+
console.log('Screenshot generated, sending response...');
|
| 538 |
+
|
| 539 |
// 返回图片
|
| 540 |
res.setHeader('Content-Type', 'image/jpeg');
|
| 541 |
+
res.setHeader('Cache-Control', 'public, max-age=60'); // 1分钟缓存,减少服务器压力
|
| 542 |
res.setHeader('Content-Disposition', `inline; filename="${pptData.title}-${slideIdx + 1}.jpg"`);
|
| 543 |
res.send(screenshot);
|
| 544 |
} catch (error) {
|
| 545 |
+
console.error('Screenshot route error:', error);
|
| 546 |
+
console.error('Stack trace:', error.stack);
|
| 547 |
+
|
| 548 |
+
// 返回一个简单的错误响应而不是抛出异常
|
| 549 |
+
try {
|
| 550 |
+
res.status(500).setHeader('Content-Type', 'application/json');
|
| 551 |
+
res.json({
|
| 552 |
+
error: 'Screenshot generation failed',
|
| 553 |
+
message: error.message,
|
| 554 |
+
details: process.env.NODE_ENV === 'development' ? error.stack : undefined
|
| 555 |
+
});
|
| 556 |
+
} catch (responseError) {
|
| 557 |
+
console.error('Error sending error response:', responseError);
|
| 558 |
+
// 如果连错误响应都发送失败,调用next
|
| 559 |
+
next(error);
|
| 560 |
+
}
|
| 561 |
}
|
| 562 |
});
|
| 563 |
|