Upload public.js
Browse files- backend/src/routes/public.js +70 -0
backend/src/routes/public.js
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import express from 'express';
|
2 |
import githubService from '../services/githubService.js';
|
3 |
import memoryStorageService from '../services/memoryStorageService.js';
|
|
|
4 |
|
5 |
const router = express.Router();
|
6 |
|
@@ -445,6 +446,8 @@ router.post('/generate-share-link', async (req, res, next) => {
|
|
445 |
pptUrl: `${protocol}://${baseUrl}/api/public/ppt/${userId}/${pptId}`,
|
446 |
// 前端查看链接
|
447 |
viewUrl: `${protocol}://${baseUrl}/public/${userId}/${pptId}/${slideIndex}`,
|
|
|
|
|
448 |
// 添加PPT信息
|
449 |
pptInfo: {
|
450 |
id: pptId,
|
@@ -461,4 +464,71 @@ router.post('/generate-share-link', async (req, res, next) => {
|
|
461 |
}
|
462 |
});
|
463 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
464 |
export default router;
|
|
|
1 |
import express from 'express';
|
2 |
import githubService from '../services/githubService.js';
|
3 |
import memoryStorageService from '../services/memoryStorageService.js';
|
4 |
+
import screenshotService from '../services/screenshotService.js';
|
5 |
|
6 |
const router = express.Router();
|
7 |
|
|
|
446 |
pptUrl: `${protocol}://${baseUrl}/api/public/ppt/${userId}/${pptId}`,
|
447 |
// 前端查看链接
|
448 |
viewUrl: `${protocol}://${baseUrl}/public/${userId}/${pptId}/${slideIndex}`,
|
449 |
+
// 新增:截图链接
|
450 |
+
screenshotUrl: `${protocol}://${baseUrl}/api/public/screenshot/${userId}/${pptId}/${slideIndex}`,
|
451 |
// 添加PPT信息
|
452 |
pptInfo: {
|
453 |
id: pptId,
|
|
|
464 |
}
|
465 |
});
|
466 |
|
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) {
|
487 |
+
continue;
|
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=300'); // 5分钟缓存
|
526 |
+
res.setHeader('Content-Disposition', `inline; filename="${pptData.title}-${slideIdx + 1}.jpg"`);
|
527 |
+
res.send(screenshot);
|
528 |
+
} catch (error) {
|
529 |
+
console.error('Screenshot generation error:', error);
|
530 |
+
next(error);
|
531 |
+
}
|
532 |
+
});
|
533 |
+
|
534 |
export default router;
|