|
import express from 'express';
|
|
import githubService from '../services/githubService.js';
|
|
import memoryStorageService from '../services/memoryStorageService.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 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">
|
|
<title>${title} - 第${slideIndex + 1}页</title>
|
|
<style>
|
|
* {
|
|
margin: 0;
|
|
padding: 0;
|
|
box-sizing: border-box;
|
|
}
|
|
|
|
html, body {
|
|
width: 100%;
|
|
height: 100%;
|
|
overflow: hidden;
|
|
font-family: 'Microsoft YaHei', Arial, sans-serif;
|
|
background-color: #000;
|
|
}
|
|
|
|
.viewport-container {
|
|
width: 100vw;
|
|
height: 100vh;
|
|
display: flex;
|
|
justify-content: center;
|
|
align-items: center;
|
|
background-color: #000;
|
|
}
|
|
|
|
.slide-container {
|
|
width: 1000px;
|
|
height: 750px;
|
|
background-color: ${slide.background?.color || theme.backgroundColor || '#ffffff'};
|
|
position: relative;
|
|
transform-origin: center center;
|
|
overflow: hidden;
|
|
}
|
|
|
|
${slide.background?.type === 'image' ? `
|
|
.slide-container::before {
|
|
content: '';
|
|
position: absolute;
|
|
top: 0;
|
|
left: 0;
|
|
right: 0;
|
|
bottom: 0;
|
|
background-image: url('${slide.background.image}');
|
|
background-size: ${slide.background.imageSize || 'cover'};
|
|
background-position: center;
|
|
background-repeat: no-repeat;
|
|
z-index: 0;
|
|
}
|
|
` : ''}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="viewport-container">
|
|
<div class="slide-container" id="slideContainer">
|
|
${renderElements(slide.elements)}
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
function resizeSlide() {
|
|
const container = document.getElementById('slideContainer');
|
|
const viewport = container.parentElement;
|
|
|
|
// PPT标准尺寸 (4:3 比例)
|
|
const slideWidth = 1000;
|
|
const slideHeight = 750;
|
|
|
|
// 窗口尺寸
|
|
const windowWidth = viewport.clientWidth;
|
|
const windowHeight = viewport.clientHeight;
|
|
|
|
// 计算缩放比例,使PPT填满窗口
|
|
const scaleX = windowWidth / slideWidth;
|
|
const scaleY = windowHeight / slideHeight;
|
|
const scale = Math.max(scaleX, scaleY); // 使用较大的缩放比例以填满窗口
|
|
|
|
// 应用缩放
|
|
container.style.transform = \`scale(\${scale})\`;
|
|
|
|
console.log(\`Window: \${windowWidth}x\${windowHeight}, Scale: \${scale}\`);
|
|
}
|
|
|
|
// 页面加载时调整大小
|
|
window.addEventListener('load', resizeSlide);
|
|
|
|
// 窗口大小改变时重新调整
|
|
window.addEventListener('resize', resizeSlide);
|
|
</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}`,
|
|
|
|
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);
|
|
}
|
|
});
|
|
|
|
export default router; |