File size: 15,589 Bytes
b7560a4 a2a8341 b7560a4 70ebf00 a2a8341 70ebf00 a2a8341 70ebf00 b7560a4 70ebf00 b7560a4 a2a8341 b7560a4 70ebf00 b7560a4 70ebf00 b7560a4 9d621ef b7560a4 08a9a1b b7560a4 08a9a1b b7560a4 08a9a1b b7560a4 08a9a1b b7560a4 08a9a1b b7560a4 08a9a1b b7560a4 08a9a1b b7560a4 08a9a1b b7560a4 08a9a1b b7560a4 08a9a1b b7560a4 08a9a1b b7560a4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 |
import express from 'express';
import githubService from '../services/githubService.js';
import memoryStorageService from '../services/memoryStorageService.js';
const router = express.Router();
// 选择存储服务
const getStorageService = () => {
// 如果GitHub Token未配置,使用内存存储
if (!process.env.GITHUB_TOKEN) {
return memoryStorageService;
}
return githubService;
};
// 生成HTML页面来显示PPT幻灯片
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>
`;
};
// 公开访问PPT页面 - 返回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;
// 如果是GitHub服务,尝试所有仓库
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>
`);
}
// 返回HTML页面
const htmlContent = generateSlideHTML(pptData, slideIdx, pptData.title);
res.setHeader('Content-Type', 'text/html; charset=utf-8');
res.send(htmlContent);
} catch (error) {
next(error);
}
});
// API接口:获取PPT数据和指定幻灯片(JSON格式)
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;
// 如果是GitHub服务,尝试所有仓库
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' });
}
// 返回PPT数据和指定幻灯片
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);
}
});
// 获取完整PPT数据(只读模式)
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;
// 如果是GitHub服务,尝试所有仓库
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' });
}
// 返回只读版本的PPT数据
res.json({
...pptData,
isPublicView: true,
readOnly: true
});
} catch (error) {
next(error);
}
});
// 生成PPT分享链接
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}`);
// 验证PPT是否存在
const fileName = `${pptId}.json`;
const storageService = getStorageService();
let pptExists = false;
let pptData = null;
console.log(`Using storage service: ${storageService === githubService ? 'GitHub' : 'Memory'}`);
// 如果是GitHub服务,尝试所有仓库
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');
// 额外尝试:如果GitHub失败,检查memory storage作为fallback
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}`,
// 完整PPT分享链接
pptUrl: `${protocol}://${baseUrl}/api/public/ppt/${userId}/${pptId}`,
// 前端查看链接
viewUrl: `${protocol}://${baseUrl}/public/${userId}/${pptId}/${slideIndex}`,
// 添加PPT信息
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; |