|
import express from 'express';
|
|
import githubService from '../services/githubService.js';
|
|
import memoryStorageService from '../services/memoryStorageService.js';
|
|
import screenshotService from '../services/screenshotService.js';
|
|
|
|
const router = express.Router();
|
|
|
|
|
|
const getStorageService = () => {
|
|
|
|
if (!process.env.GITHUB_TOKEN) {
|
|
return memoryStorageService;
|
|
}
|
|
return githubService;
|
|
};
|
|
|
|
|
|
const generateSlideHTML = (pptData, slideIndex, title) => {
|
|
const slide = pptData.slides[slideIndex];
|
|
const theme = pptData.theme || {};
|
|
|
|
|
|
const calculatePptDimensions = (slide) => {
|
|
|
|
if (pptData.slideSize && pptData.slideSize.width && pptData.slideSize.height) {
|
|
const result = {
|
|
width: Math.ceil(pptData.slideSize.width),
|
|
height: Math.ceil(pptData.slideSize.height)
|
|
};
|
|
console.log(`使用PPT预设尺寸: ${result.width}x${result.height}`);
|
|
return result;
|
|
}
|
|
|
|
|
|
if (slide.width && slide.height) {
|
|
const result = {
|
|
width: Math.ceil(slide.width),
|
|
height: Math.ceil(slide.height)
|
|
};
|
|
console.log(`使用slide尺寸: ${result.width}x${result.height}`);
|
|
return result;
|
|
}
|
|
|
|
|
|
if (!slide.elements || slide.elements.length === 0) {
|
|
|
|
const result = { width: 960, height: 720 };
|
|
console.log(`使用默认尺寸: ${result.width}x${result.height}`);
|
|
return result;
|
|
}
|
|
|
|
let maxRight = 0;
|
|
let maxBottom = 0;
|
|
let minLeft = Infinity;
|
|
let minTop = Infinity;
|
|
|
|
|
|
slide.elements.forEach(element => {
|
|
const left = element.left || 0;
|
|
const top = element.top || 0;
|
|
const width = element.width || 0;
|
|
const height = element.height || 0;
|
|
|
|
const elementRight = left + width;
|
|
const elementBottom = top + height;
|
|
|
|
maxRight = Math.max(maxRight, elementRight);
|
|
maxBottom = Math.max(maxBottom, elementBottom);
|
|
minLeft = Math.min(minLeft, left);
|
|
minTop = Math.min(minTop, top);
|
|
});
|
|
|
|
|
|
if (minLeft === Infinity) minLeft = 0;
|
|
if (minTop === Infinity) minTop = 0;
|
|
|
|
|
|
const contentWidth = maxRight - minLeft;
|
|
const contentHeight = maxBottom - minTop;
|
|
|
|
|
|
const paddingX = Math.max(40, Math.abs(minLeft));
|
|
const paddingY = Math.max(40, Math.abs(minTop));
|
|
|
|
|
|
let finalWidth = Math.max(contentWidth + paddingX * 2, 800);
|
|
let finalHeight = Math.max(contentHeight + paddingY * 2, 600);
|
|
|
|
|
|
finalWidth = Math.ceil(finalWidth / 2) * 2;
|
|
finalHeight = Math.ceil(finalHeight / 2) * 2;
|
|
|
|
const result = { width: finalWidth, height: finalHeight };
|
|
console.log(`根据元素计算尺寸: ${result.width}x${result.height}`);
|
|
|
|
return result;
|
|
};
|
|
|
|
const pptDimensions = calculatePptDimensions(slide);
|
|
|
|
|
|
const renderElements = (elements) => {
|
|
if (!elements || elements.length === 0) return '';
|
|
|
|
return elements.map(element => {
|
|
const style = `
|
|
position: absolute;
|
|
left: ${element.left}px;
|
|
top: ${element.top}px;
|
|
width: ${element.width}px;
|
|
height: ${element.height}px;
|
|
transform: rotate(${element.rotate || 0}deg);
|
|
z-index: ${element.zIndex || 1};
|
|
`;
|
|
|
|
switch (element.type) {
|
|
case 'text':
|
|
return `
|
|
<div style="${style}
|
|
font-size: ${element.fontSize || 14}px;
|
|
font-family: ${element.fontName || 'Arial'};
|
|
color: ${element.defaultColor || '#000'};
|
|
font-weight: ${element.bold ? 'bold' : 'normal'};
|
|
font-style: ${element.italic ? 'italic' : 'normal'};
|
|
text-decoration: ${element.underline ? 'underline' : 'none'};
|
|
text-align: ${element.align || 'left'};
|
|
line-height: ${element.lineHeight || 1.2};
|
|
padding: 10px;
|
|
word-wrap: break-word;
|
|
overflow: hidden;
|
|
">
|
|
${element.content || ''}
|
|
</div>
|
|
`;
|
|
case 'image':
|
|
return `
|
|
<div style="${style}">
|
|
<img src="${element.src}" alt="" style="width: 100%; height: 100%; object-fit: ${element.objectFit || 'contain'};" />
|
|
</div>
|
|
`;
|
|
case 'shape':
|
|
const shapeStyle = element.fill ? `background-color: ${element.fill};` : '';
|
|
const borderStyle = element.outline ? `border: ${element.outline.width || 1}px ${element.outline.style || 'solid'} ${element.outline.color || '#000'};` : '';
|
|
return `
|
|
<div style="${style} ${shapeStyle} ${borderStyle}"></div>
|
|
`;
|
|
default:
|
|
return `<div style="${style}"></div>`;
|
|
}
|
|
}).join('');
|
|
};
|
|
|
|
return `
|
|
<!DOCTYPE html>
|
|
<html lang="zh-CN">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
|
|
<title>${title} - 第${slideIndex + 1}页</title>
|
|
<style>
|
|
/* 完全重置所有默认样式 */
|
|
*, *::before, *::after {
|
|
margin: 0 !important;
|
|
padding: 0 !important;
|
|
border: 0 !important;
|
|
outline: 0 !important;
|
|
box-sizing: border-box !important;
|
|
}
|
|
|
|
/* HTML - 填满整个窗口 */
|
|
html {
|
|
width: 100vw !important;
|
|
height: 100vh !important;
|
|
background: #000000 !important;
|
|
overflow: hidden !important;
|
|
position: fixed !important;
|
|
top: 0 !important;
|
|
left: 0 !important;
|
|
}
|
|
|
|
/* Body - 填满整个窗口 */
|
|
body {
|
|
width: 100vw !important;
|
|
height: 100vh !important;
|
|
background: #000000 !important;
|
|
overflow: hidden !important;
|
|
font-family: 'Microsoft YaHei', Arial, sans-serif !important;
|
|
position: fixed !important;
|
|
top: 0 !important;
|
|
left: 0 !important;
|
|
display: flex !important;
|
|
align-items: center !important;
|
|
justify-content: center !important;
|
|
}
|
|
|
|
/* PPT容器 - 响应式缩放,保持宽高比,填满窗口 */
|
|
.slide-container {
|
|
width: ${pptDimensions.width}px !important;
|
|
height: ${pptDimensions.height}px !important;
|
|
background-color: ${slide.background?.color || theme.backgroundColor || '#ffffff'} !important;
|
|
position: relative !important;
|
|
overflow: hidden !important;
|
|
transform-origin: center center !important;
|
|
}
|
|
|
|
/* 背景图片处理 */
|
|
${slide.background?.type === 'image' ? `
|
|
.slide-container::before {
|
|
content: '';
|
|
position: absolute !important;
|
|
top: 0 !important;
|
|
left: 0 !important;
|
|
width: 100% !important;
|
|
height: 100% !important;
|
|
background-image: url('${slide.background.image}') !important;
|
|
background-size: ${slide.background.imageSize || 'cover'} !important;
|
|
background-position: center !important;
|
|
background-repeat: no-repeat !important;
|
|
z-index: 0 !important;
|
|
}
|
|
` : ''}
|
|
|
|
/* 隐藏所有滚动条 */
|
|
::-webkit-scrollbar {
|
|
display: none !important;
|
|
}
|
|
|
|
html {
|
|
scrollbar-width: none !important;
|
|
-ms-overflow-style: none !important;
|
|
}
|
|
|
|
/* 禁用用户交互 */
|
|
* {
|
|
-webkit-user-select: none !important;
|
|
-moz-user-select: none !important;
|
|
-ms-user-select: none !important;
|
|
user-select: none !important;
|
|
-webkit-user-drag: none !important;
|
|
-khtml-user-drag: none !important;
|
|
-moz-user-drag: none !important;
|
|
-o-user-drag: none !important;
|
|
user-drag: none !important;
|
|
}
|
|
|
|
/* 禁用缩放 */
|
|
body {
|
|
zoom: 1 !important;
|
|
-webkit-text-size-adjust: 100% !important;
|
|
-ms-text-size-adjust: 100% !important;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="slide-container" id="slideContainer">
|
|
${renderElements(slide.elements)}
|
|
</div>
|
|
|
|
<script>
|
|
// PPT尺寸信息
|
|
window.PPT_DIMENSIONS = {
|
|
width: ${pptDimensions.width},
|
|
height: ${pptDimensions.height}
|
|
};
|
|
|
|
console.log('PPT页面初始化 - 响应式填充模式:', window.PPT_DIMENSIONS);
|
|
|
|
// 响应式缩放函数 - PPT内容自动填满窗口
|
|
function resizePPTToFitWindow() {
|
|
const container = document.getElementById('slideContainer');
|
|
if (!container) return;
|
|
|
|
const windowWidth = window.innerWidth;
|
|
const windowHeight = window.innerHeight;
|
|
const pptWidth = ${pptDimensions.width};
|
|
const pptHeight = ${pptDimensions.height};
|
|
|
|
// 计算缩放比例,使PPT完全填满窗口
|
|
const scaleX = windowWidth / pptWidth;
|
|
const scaleY = windowHeight / pptHeight;
|
|
|
|
// 使用较大的缩放比例,确保PPT内容填满窗口(可能会有部分内容被裁切)
|
|
const scale = Math.max(scaleX, scaleY);
|
|
|
|
// 应用缩放变换
|
|
container.style.transform = \`scale(\${scale})\`;
|
|
|
|
// 如果需要居中显示(当一个方向填满,另一个方向可能会超出)
|
|
const scaledWidth = pptWidth * scale;
|
|
const scaledHeight = pptHeight * scale;
|
|
|
|
// 计算偏移,使内容居中
|
|
const offsetX = (windowWidth - scaledWidth) / 2 / scale;
|
|
const offsetY = (windowHeight - scaledHeight) / 2 / scale;
|
|
|
|
container.style.left = offsetX + 'px';
|
|
container.style.top = offsetY + 'px';
|
|
|
|
console.log(\`窗口尺寸: \${windowWidth}x\${windowHeight}, PPT尺寸: \${pptWidth}x\${pptHeight}, 缩放比例: \${scale.toFixed(3)}, 偏移: \${offsetX.toFixed(1)}, \${offsetY.toFixed(1)}\`);
|
|
}
|
|
|
|
// 页面加载完成后初始化
|
|
window.addEventListener('load', function() {
|
|
const html = document.documentElement;
|
|
const body = document.body;
|
|
const container = document.getElementById('slideContainer');
|
|
|
|
console.log('页面加载完成,开始响应式缩放');
|
|
|
|
// 确保页面元素填满窗口
|
|
html.style.width = '100vw';
|
|
html.style.height = '100vh';
|
|
html.style.background = '#000000';
|
|
html.style.overflow = 'hidden';
|
|
|
|
body.style.width = '100vw';
|
|
body.style.height = '100vh';
|
|
body.style.background = '#000000';
|
|
body.style.overflow = 'hidden';
|
|
body.style.display = 'flex';
|
|
body.style.alignItems = 'center';
|
|
body.style.justifyContent = 'center';
|
|
body.style.margin = '0';
|
|
body.style.padding = '0';
|
|
|
|
// 初始化PPT容器
|
|
if (container) {
|
|
container.style.width = '${pptDimensions.width}px';
|
|
container.style.height = '${pptDimensions.height}px';
|
|
container.style.position = 'relative';
|
|
container.style.overflow = 'hidden';
|
|
container.style.transformOrigin = 'center center';
|
|
}
|
|
|
|
// 执行初始缩放
|
|
resizePPTToFitWindow();
|
|
|
|
// 禁用各种用户交互
|
|
document.addEventListener('wheel', function(e) {
|
|
if (e.ctrlKey) e.preventDefault();
|
|
}, { passive: false });
|
|
|
|
document.addEventListener('keydown', function(e) {
|
|
if ((e.ctrlKey && (e.key === '+' || e.key === '-' || e.key === '0')) || e.key === 'F11') {
|
|
e.preventDefault();
|
|
}
|
|
}, false);
|
|
|
|
document.addEventListener('contextmenu', function(e) {
|
|
e.preventDefault();
|
|
}, false);
|
|
|
|
// 禁用触摸缩放
|
|
let lastTouchEnd = 0;
|
|
document.addEventListener('touchend', function(e) {
|
|
const now = new Date().getTime();
|
|
if (now - lastTouchEnd <= 300) {
|
|
e.preventDefault();
|
|
}
|
|
lastTouchEnd = now;
|
|
}, false);
|
|
|
|
document.addEventListener('touchstart', function(e) {
|
|
if (e.touches.length > 1) {
|
|
e.preventDefault();
|
|
}
|
|
}, { passive: false });
|
|
|
|
console.log('响应式PPT页面初始化完成');
|
|
});
|
|
|
|
// 监听窗口大小变化,实时调整PPT大小
|
|
window.addEventListener('resize', function() {
|
|
console.log('窗口大小变化,重新调整PPT尺寸');
|
|
resizePPTToFitWindow();
|
|
});
|
|
|
|
// 监听屏幕方向变化(移动设备)
|
|
window.addEventListener('orientationchange', function() {
|
|
console.log('屏幕方向变化,重新调整PPT尺寸');
|
|
setTimeout(resizePPTToFitWindow, 100); // 延迟一点时间等待方向变化完成
|
|
});
|
|
</script>
|
|
</body>
|
|
</html>
|
|
`;
|
|
};
|
|
|
|
|
|
router.get('/view/:userId/:pptId/:slideIndex?', async (req, res, next) => {
|
|
try {
|
|
const { userId, pptId, slideIndex = 0 } = req.params;
|
|
const querySlideIndex = req.query.slide ? parseInt(req.query.slide) : parseInt(slideIndex);
|
|
const fileName = `${pptId}.json`;
|
|
const storageService = getStorageService();
|
|
|
|
let pptData = null;
|
|
|
|
|
|
if (storageService === githubService && storageService.repositories) {
|
|
for (let i = 0; i < storageService.repositories.length; i++) {
|
|
try {
|
|
const result = await storageService.getFile(userId, fileName, i);
|
|
if (result) {
|
|
pptData = result.content;
|
|
break;
|
|
}
|
|
} catch (error) {
|
|
continue;
|
|
}
|
|
}
|
|
} else {
|
|
|
|
const result = await storageService.getFile(userId, fileName);
|
|
if (result) {
|
|
pptData = result.content;
|
|
}
|
|
}
|
|
|
|
if (!pptData) {
|
|
return res.status(404).send(`
|
|
<!DOCTYPE html>
|
|
<html lang="zh-CN">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>PPT未找到</title>
|
|
<style>
|
|
body { font-family: Arial, sans-serif; text-align: center; padding: 50px; }
|
|
.error { color: #e74c3c; font-size: 18px; }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="error">
|
|
<h2>PPT未找到</h2>
|
|
<p>请检查链接是否正确</p>
|
|
</div>
|
|
</body>
|
|
</html>
|
|
`);
|
|
}
|
|
|
|
const slideIdx = querySlideIndex;
|
|
if (slideIdx >= pptData.slides.length || slideIdx < 0) {
|
|
return res.status(404).send(`
|
|
<!DOCTYPE html>
|
|
<html lang="zh-CN">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>幻灯片未找到</title>
|
|
<style>
|
|
body { font-family: Arial, sans-serif; text-align: center; padding: 50px; }
|
|
.error { color: #e74c3c; font-size: 18px; }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="error">
|
|
<h2>幻灯片未找到</h2>
|
|
<p>请检查幻灯片索引是否正确</p>
|
|
</div>
|
|
</body>
|
|
</html>
|
|
`);
|
|
}
|
|
|
|
|
|
const htmlContent = generateSlideHTML(pptData, slideIdx, pptData.title);
|
|
res.setHeader('Content-Type', 'text/html; charset=utf-8');
|
|
res.send(htmlContent);
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
|
|
|
|
router.get('/api-view/:userId/:pptId/:slideIndex?', async (req, res, next) => {
|
|
try {
|
|
const { userId, pptId, slideIndex = 0 } = req.params;
|
|
const fileName = `${pptId}.json`;
|
|
const storageService = getStorageService();
|
|
|
|
let pptData = null;
|
|
|
|
|
|
if (storageService === githubService && storageService.repositories) {
|
|
for (let i = 0; i < storageService.repositories.length; i++) {
|
|
try {
|
|
const result = await storageService.getFile(userId, fileName, i);
|
|
if (result) {
|
|
pptData = result.content;
|
|
break;
|
|
}
|
|
} catch (error) {
|
|
continue;
|
|
}
|
|
}
|
|
} else {
|
|
|
|
const result = await storageService.getFile(userId, fileName);
|
|
if (result) {
|
|
pptData = result.content;
|
|
}
|
|
}
|
|
|
|
if (!pptData) {
|
|
return res.status(404).json({ error: 'PPT not found' });
|
|
}
|
|
|
|
const slideIdx = parseInt(slideIndex);
|
|
if (slideIdx >= pptData.slides.length || slideIdx < 0) {
|
|
return res.status(404).json({ error: 'Slide not found' });
|
|
}
|
|
|
|
|
|
res.json({
|
|
id: pptData.id,
|
|
title: pptData.title,
|
|
theme: pptData.theme,
|
|
currentSlide: pptData.slides[slideIdx],
|
|
slideIndex: slideIdx,
|
|
totalSlides: pptData.slides.length,
|
|
isPublicView: true
|
|
});
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
|
|
|
|
router.get('/ppt/:userId/:pptId', async (req, res, next) => {
|
|
try {
|
|
const { userId, pptId } = req.params;
|
|
const fileName = `${pptId}.json`;
|
|
const storageService = getStorageService();
|
|
|
|
let pptData = null;
|
|
|
|
|
|
if (storageService === githubService && storageService.repositories) {
|
|
for (let i = 0; i < storageService.repositories.length; i++) {
|
|
try {
|
|
const result = await storageService.getFile(userId, fileName, i);
|
|
if (result) {
|
|
pptData = result.content;
|
|
break;
|
|
}
|
|
} catch (error) {
|
|
continue;
|
|
}
|
|
}
|
|
} else {
|
|
|
|
const result = await storageService.getFile(userId, fileName);
|
|
if (result) {
|
|
pptData = result.content;
|
|
}
|
|
}
|
|
|
|
if (!pptData) {
|
|
return res.status(404).json({ error: 'PPT not found' });
|
|
}
|
|
|
|
|
|
res.json({
|
|
...pptData,
|
|
isPublicView: true,
|
|
readOnly: true
|
|
});
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
|
|
|
|
router.post('/generate-share-link', async (req, res, next) => {
|
|
try {
|
|
const { userId, pptId, slideIndex = 0 } = req.body;
|
|
|
|
if (!userId || !pptId) {
|
|
return res.status(400).json({ error: 'User ID and PPT ID are required' });
|
|
}
|
|
|
|
console.log(`Generating share link for PPT: ${pptId}, User: ${userId}`);
|
|
|
|
|
|
const fileName = `${pptId}.json`;
|
|
const storageService = getStorageService();
|
|
let pptExists = false;
|
|
let pptData = null;
|
|
|
|
console.log(`Using storage service: ${storageService === githubService ? 'GitHub' : 'Memory'}`);
|
|
|
|
|
|
if (storageService === githubService && storageService.repositories) {
|
|
console.log(`Checking ${storageService.repositories.length} GitHub repositories...`);
|
|
|
|
for (let i = 0; i < storageService.repositories.length; i++) {
|
|
try {
|
|
console.log(`Checking repository ${i}: ${storageService.repositories[i]}`);
|
|
const result = await storageService.getFile(userId, fileName, i);
|
|
if (result) {
|
|
pptExists = true;
|
|
pptData = result.content;
|
|
console.log(`PPT found in repository ${i}`);
|
|
break;
|
|
}
|
|
} catch (error) {
|
|
console.log(`PPT not found in repository ${i}: ${error.message}`);
|
|
continue;
|
|
}
|
|
}
|
|
} else {
|
|
|
|
console.log('Checking memory storage...');
|
|
try {
|
|
const result = await storageService.getFile(userId, fileName);
|
|
if (result) {
|
|
pptExists = true;
|
|
pptData = result.content;
|
|
console.log('PPT found in memory storage');
|
|
}
|
|
} catch (error) {
|
|
console.log(`PPT not found in memory storage: ${error.message}`);
|
|
}
|
|
}
|
|
|
|
if (!pptExists) {
|
|
console.log('PPT not found in any storage location');
|
|
|
|
|
|
if (storageService === githubService) {
|
|
console.log('GitHub lookup failed, trying memory storage fallback...');
|
|
try {
|
|
const memoryResult = await memoryStorageService.getFile(userId, fileName);
|
|
if (memoryResult) {
|
|
pptExists = true;
|
|
pptData = memoryResult.content;
|
|
console.log('PPT found in memory storage fallback');
|
|
}
|
|
} catch (memoryError) {
|
|
console.log(`Memory storage fallback also failed: ${memoryError.message}`);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!pptExists) {
|
|
return res.status(404).json({
|
|
error: 'PPT not found',
|
|
details: `PPT ${pptId} not found for user ${userId}`,
|
|
searchedLocations: storageService === githubService ? 'GitHub repositories and memory storage' : 'Memory storage only'
|
|
});
|
|
}
|
|
|
|
const baseUrl = process.env.PUBLIC_URL || req.get('host');
|
|
const protocol = process.env.NODE_ENV === 'production' ? 'https' : req.protocol;
|
|
|
|
const shareLinks = {
|
|
|
|
slideUrl: `${protocol}://${baseUrl}/api/public/view/${userId}/${pptId}/${slideIndex}`,
|
|
|
|
pptUrl: `${protocol}://${baseUrl}/api/public/ppt/${userId}/${pptId}`,
|
|
|
|
viewUrl: `${protocol}://${baseUrl}/public/${userId}/${pptId}/${slideIndex}`,
|
|
|
|
screenshotUrl: `${protocol}://${baseUrl}/api/public/screenshot/${userId}/${pptId}/${slideIndex}`,
|
|
|
|
pptInfo: {
|
|
id: pptId,
|
|
title: pptData?.title || 'Unknown Title',
|
|
slideCount: pptData?.slides?.length || 0
|
|
}
|
|
};
|
|
|
|
console.log('Share links generated successfully:', shareLinks);
|
|
res.json(shareLinks);
|
|
} catch (error) {
|
|
console.error('Share link generation error:', error);
|
|
next(error);
|
|
}
|
|
});
|
|
|
|
|
|
router.get('/screenshot/:userId/:pptId/:slideIndex?', async (req, res, next) => {
|
|
try {
|
|
console.log('Screenshot request received:', req.params);
|
|
|
|
const { userId, pptId, slideIndex = 0 } = req.params;
|
|
const slideIdx = parseInt(slideIndex);
|
|
const fileName = `${pptId}.json`;
|
|
const storageService = getStorageService();
|
|
|
|
console.log(`Generating screenshot for: ${userId}/${pptId}/${slideIdx}`);
|
|
|
|
let pptData = null;
|
|
|
|
|
|
if (storageService === githubService && storageService.repositories) {
|
|
console.log('Checking GitHub repositories...');
|
|
for (let i = 0; i < storageService.repositories.length; i++) {
|
|
try {
|
|
const result = await storageService.getFile(userId, fileName, i);
|
|
if (result) {
|
|
pptData = result.content;
|
|
console.log(`PPT data found in repository ${i}`);
|
|
break;
|
|
}
|
|
} catch (error) {
|
|
continue;
|
|
}
|
|
}
|
|
} else {
|
|
console.log('Checking memory storage...');
|
|
const result = await storageService.getFile(userId, fileName);
|
|
if (result) {
|
|
pptData = result.content;
|
|
console.log('PPT data found in memory storage');
|
|
}
|
|
}
|
|
|
|
|
|
if (!pptData && storageService === githubService) {
|
|
console.log('Trying memory storage fallback...');
|
|
try {
|
|
const memoryResult = await memoryStorageService.getFile(userId, fileName);
|
|
if (memoryResult) {
|
|
pptData = memoryResult.content;
|
|
console.log('PPT data found in memory storage fallback');
|
|
}
|
|
} catch (memoryError) {
|
|
console.log('Memory storage fallback failed:', memoryError.message);
|
|
}
|
|
}
|
|
|
|
if (!pptData) {
|
|
console.log('PPT not found');
|
|
return res.status(404).json({ error: 'PPT not found' });
|
|
}
|
|
|
|
if (slideIdx >= pptData.slides.length || slideIdx < 0) {
|
|
console.log('Slide index out of bounds');
|
|
return res.status(404).json({ error: 'Slide not found' });
|
|
}
|
|
|
|
console.log('Generating HTML content...');
|
|
|
|
const htmlContent = generateSlideHTML(pptData, slideIdx, pptData.title);
|
|
|
|
console.log('Calling screenshot service...');
|
|
|
|
const screenshot = await screenshotService.generateScreenshot(htmlContent);
|
|
|
|
console.log('Screenshot generated, sending response...');
|
|
|
|
|
|
res.setHeader('Content-Type', 'image/jpeg');
|
|
res.setHeader('Cache-Control', 'public, max-age=60');
|
|
res.setHeader('Content-Disposition', `inline; filename="${pptData.title}-${slideIdx + 1}.jpg"`);
|
|
res.send(screenshot);
|
|
} catch (error) {
|
|
console.error('Screenshot route error:', error);
|
|
console.error('Stack trace:', error.stack);
|
|
|
|
|
|
try {
|
|
res.status(500).setHeader('Content-Type', 'application/json');
|
|
res.json({
|
|
error: 'Screenshot generation failed',
|
|
message: error.message,
|
|
details: process.env.NODE_ENV === 'development' ? error.stack : undefined
|
|
});
|
|
} catch (responseError) {
|
|
console.error('Error sending error response:', responseError);
|
|
|
|
next(error);
|
|
}
|
|
}
|
|
});
|
|
|
|
export default router; |