Spaces:
Running
Running
File size: 20,137 Bytes
28e1dba |
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 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 |
import express from 'express';
import persistentImageLinkService from '../services/persistentImageLinkService.js';
import githubService from '../services/githubService.js';
import huggingfaceStorageService from '../services/huggingfaceStorageService.js';
import { authenticateToken } from '../middleware/auth.js';
const router = express.Router();
/**
* 接收前端生成的图片数据并创建持久化链接
* POST /api/export/ppt-to-image
*/
router.post('/ppt-to-image', authenticateToken, async (req, res, next) => {
try {
const { pptId, slideIndex = 0, imageData, format = 'jpeg', metadata = {} } = req.body;
const userId = req.user.userId;
console.log(`🖼️ Export PPT to image request: userId=${userId}, pptId=${pptId}, slideIndex=${slideIndex}`);
// 验证参数
if (!pptId) {
return res.status(400).json({ error: 'PPT ID is required' });
}
if (!imageData) {
return res.status(400).json({ error: 'Image data is required' });
}
// 验证图片数据格式
if (!imageData.startsWith('data:image/')) {
return res.status(400).json({ error: 'Invalid image data format' });
}
// 获取PPT数据以验证权限
let pptData;
try {
pptData = await huggingfaceStorageService.getPPTData(userId, pptId);
} catch (error) {
console.log('Huggingface storage failed, trying GitHub...');
try {
pptData = await githubService.getPPTData(userId, pptId);
} catch (githubError) {
console.error('Both storage methods failed:', { huggingface: error.message, github: githubError.message });
return res.status(404).json({ error: 'PPT not found' });
}
}
// 验证幻灯片索引
if (!pptData.slides || slideIndex >= pptData.slides.length) {
return res.status(400).json({ error: 'Invalid slide index' });
}
// 将图片数据转换为Buffer并存储到内存
const base64Data = imageData.split(',')[1];
const imageBuffer = Buffer.from(base64Data, 'base64');
// 存储图片到内存中供公开访问
try {
await huggingfaceStorageService.storeImage(userId, pptId, slideIndex, imageBuffer, {
format: format === 'jpeg' ? 'jpg' : format,
updateExisting: true
});
console.log(`✅ Image stored in memory: ${userId}/${pptId}/${slideIndex}`);
} catch (storeError) {
console.warn(`⚠️ Failed to store image in memory: ${storeError.message}`);
}
// 创建持久化链接
const linkData = {
pptId,
slideIndex,
imageData,
format,
metadata: {
...metadata,
exportedAt: new Date().toISOString(),
slideTitle: pptData.slides[slideIndex]?.title || `第 ${slideIndex + 1} 页`,
exportMethod: 'frontend'
}
};
const result = await persistentImageLinkService.createLink(userId, linkData);
console.log(`✅ PPT exported successfully: ${result.linkId}`);
res.json({
success: true,
linkId: result.linkId,
imageUrl: result.imageUrl,
downloadUrl: result.downloadUrl,
metadata: result.metadata
});
} catch (error) {
console.error('Export PPT to image failed:', error);
next(error);
}
});
/**
* 接收前端批量生成的图片数据并创建持久化链接
* POST /api/export/ppt-to-images-batch
*/
router.post('/ppt-to-images-batch', authenticateToken, async (req, res, next) => {
try {
const { pptId, imagesData, format = 'jpeg', metadata = {} } = req.body;
const userId = req.user.userId;
console.log(`🖼️ Batch export PPT to images: userId=${userId}, pptId=${pptId}`);
// 验证参数
if (!pptId) {
return res.status(400).json({ error: 'PPT ID is required' });
}
if (!imagesData || !Array.isArray(imagesData) || imagesData.length === 0) {
return res.status(400).json({ error: 'Images data array is required' });
}
// 获取PPT数据以验证权限
let pptData;
try {
pptData = await huggingfaceStorageService.getPPTData(userId, pptId);
} catch (error) {
console.log('Huggingface storage failed, trying GitHub...');
try {
pptData = await githubService.getPPTData(userId, pptId);
} catch (githubError) {
console.error('Both storage methods failed:', { huggingface: error.message, github: githubError.message });
return res.status(404).json({ error: 'PPT not found' });
}
}
if (!pptData.slides || pptData.slides.length === 0) {
return res.status(400).json({ error: 'No slides found in PPT' });
}
// 批量创建持久化链接
const results = [];
const errors = [];
const exportTime = new Date().toISOString();
for (const imageItem of imagesData) {
try {
const { slideIndex, imageData } = imageItem;
// 验证图片数据
if (!imageData || !imageData.startsWith('data:image/')) {
errors.push({ slideIndex, error: 'Invalid image data format' });
continue;
}
// 验证幻灯片索引
if (slideIndex >= pptData.slides.length) {
errors.push({ slideIndex, error: 'Invalid slide index' });
continue;
}
// 将图片数据转换为Buffer并存储到内存
const base64Data = imageData.split(',')[1];
const imageBuffer = Buffer.from(base64Data, 'base64');
// 存储图片到内存中供公开访问
try {
await huggingfaceStorageService.storeImage(userId, pptId, slideIndex, imageBuffer, {
format: format === 'jpeg' ? 'jpg' : format,
updateExisting: true
});
console.log(`✅ Batch image stored in memory: ${userId}/${pptId}/${slideIndex}`);
} catch (storeError) {
console.warn(`⚠️ Failed to store batch image in memory: ${storeError.message}`);
}
const linkData = {
pptId,
slideIndex,
imageData,
format,
metadata: {
...metadata,
exportedAt: exportTime,
slideTitle: pptData.slides[slideIndex]?.title || `第 ${slideIndex + 1} 页`,
exportMethod: 'frontend',
batchExport: true
}
};
const result = await persistentImageLinkService.createLink(userId, linkData);
results.push({
slideIndex,
linkId: result.linkId,
imageUrl: result.imageUrl,
downloadUrl: result.downloadUrl,
metadata: result.metadata
});
} catch (error) {
console.error(`Failed to create link for slide ${imageItem.slideIndex}:`, error);
errors.push({ slideIndex: imageItem.slideIndex, error: error.message });
}
}
console.log(`✅ Batch export completed: ${results.length}/${imagesData.length} successful`);
res.json({
success: true,
totalSlides: imagesData.length,
successCount: results.length,
errorCount: errors.length,
results,
errors
});
} catch (error) {
console.error('Batch export PPT to images failed:', error);
next(error);
}
});
/**
* 接收前端生成的当前幻灯片图片数据并创建持久化链接
* POST /api/export/current-slide
*/
router.post('/current-slide', authenticateToken, async (req, res, next) => {
try {
const { pptId, slideIndex, imageData, format = 'jpeg', metadata = {} } = req.body;
const userId = req.user.userId;
console.log(`🖼️ Export current slide: userId=${userId}, pptId=${pptId}, slideIndex=${slideIndex}`);
// 验证参数
if (!pptId || slideIndex === undefined || !imageData) {
return res.status(400).json({ error: 'PPT ID, slide index and image data are required' });
}
// 验证图片数据格式
if (!imageData.startsWith('data:image/')) {
return res.status(400).json({ error: 'Invalid image data format' });
}
// 获取PPT数据以验证权限
let pptData;
try {
pptData = await huggingfaceStorageService.getPPTData(userId, pptId);
} catch (error) {
console.log('Huggingface storage failed, trying GitHub...');
try {
pptData = await githubService.getPPTData(userId, pptId);
} catch (githubError) {
console.error('Both storage methods failed:', { huggingface: error.message, github: githubError.message });
return res.status(404).json({ error: 'PPT not found' });
}
}
// 验证幻灯片索引
if (!pptData.slides || slideIndex >= pptData.slides.length) {
return res.status(400).json({ error: 'Invalid slide index' });
}
// 将图片数据转换为Buffer并存储到内存
const base64Data = imageData.split(',')[1];
const imageBuffer = Buffer.from(base64Data, 'base64');
// 存储图片到内存中供公开访问
try {
await huggingfaceStorageService.storeImage(userId, pptId, slideIndex, imageBuffer, {
format: format === 'jpeg' ? 'jpg' : format,
updateExisting: true
});
console.log(`✅ Current slide image stored in memory: ${userId}/${pptId}/${slideIndex}`);
} catch (storeError) {
console.warn(`⚠️ Failed to store current slide image in memory: ${storeError.message}`);
}
// 创建持久化链接
const linkData = {
pptId,
slideIndex,
imageData,
format,
metadata: {
...metadata,
exportedAt: new Date().toISOString(),
slideTitle: pptData.slides[slideIndex]?.title || `第 ${slideIndex + 1} 页`,
exportMethod: 'frontend',
isCurrentSlide: true
}
};
const result = await persistentImageLinkService.createLink(userId, linkData);
console.log(`✅ Current slide exported successfully: ${result.linkId}`);
res.json({
success: true,
linkId: result.linkId,
imageUrl: result.imageUrl,
downloadUrl: result.downloadUrl,
metadata: result.metadata
});
} catch (error) {
console.error('Export current slide failed:', error);
next(error);
}
});
/**
* 获取导出历史记录
* GET /api/export/history
*/
router.get('/history', authenticateToken, async (req, res, next) => {
try {
const userId = req.user.id;
const { page = 1, limit = 20, pptId } = req.query;
console.log(`📋 Get export history: userId=${userId}, page=${page}, limit=${limit}`);
// 获取用户的持久化链接列表
const links = await persistentImageLinkService.getUserLinks(userId, {
page: parseInt(page),
limit: parseInt(limit),
pptId
});
// 过滤出导出相关的链接(包含导出元数据)
const exportHistory = links.links.filter(link =>
link.metadata && (link.metadata.exportedAt || link.metadata.isCurrentSlide || link.metadata.batchExport)
);
res.json({
success: true,
total: exportHistory.length,
page: parseInt(page),
limit: parseInt(limit),
history: exportHistory.map(link => ({
linkId: link.linkId,
pptId: link.pptId,
slideIndex: link.slideIndex,
imageUrl: link.imageUrl,
downloadUrl: link.downloadUrl,
createdAt: link.createdAt,
expiresAt: link.expiresAt,
metadata: link.metadata
}))
});
} catch (error) {
console.error('Get export history failed:', error);
next(error);
}
});
/**
* 删除导出记录
* DELETE /api/export/:linkId
*/
router.delete('/:linkId', authenticateToken, async (req, res, next) => {
try {
const { linkId } = req.params;
const userId = req.user.id;
console.log(`🗑️ Delete export record: userId=${userId}, linkId=${linkId}`);
// 删除持久化链接
const result = await persistentImageLinkService.deleteLink(linkId, userId);
if (!result.success) {
return res.status(404).json({ error: 'Export record not found or access denied' });
}
console.log(`✅ Export record deleted: ${linkId}`);
res.json({
success: true,
message: 'Export record deleted successfully'
});
} catch (error) {
console.error('Delete export record failed:', error);
next(error);
}
});
/**
* 前端导出功能复刻 - 单个幻灯片导出
* POST /api/export/frontend-replicate-single
*/
router.post('/frontend-replicate-single', authenticateToken, async (req, res, next) => {
try {
const { pptId, slideIndex = 0, format = 'jpeg', quality = 1, width = 1600, height = 900, ignoreWebfont = true, backgroundColor = '#ffffff', pixelRatio = 1 } = req.body;
const userId = req.user.userId;
console.log(`🖼️ Frontend replicate single slide: userId=${userId}, pptId=${pptId}, slideIndex=${slideIndex}`);
// 验证参数
if (!pptId) {
return res.status(400).json({ error: 'PPT ID is required' });
}
// 获取PPT数据以验证权限
let pptData;
try {
pptData = await huggingfaceStorageService.getPPTData(userId, pptId);
} catch (error) {
console.log('Huggingface storage failed, trying GitHub...');
try {
pptData = await githubService.getPPTData(userId, pptId);
} catch (githubError) {
console.error('Both storage methods failed:', { huggingface: error.message, github: githubError.message });
return res.status(404).json({ error: 'PPT not found' });
}
}
// 验证幻灯片索引
if (!pptData.slides || slideIndex >= pptData.slides.length) {
return res.status(400).json({ error: 'Invalid slide index' });
}
// 返回指导信息,因为实际的图片生成需要在前端完成
const response = {
success: false,
message: 'Frontend rendering required',
instruction: {
method: 'frontend-rendering',
description: 'Please use frontend html-to-image library to generate the image data, then call /api/export/ppt-to-image to create persistent link',
steps: [
'1. Use html-to-image library in frontend to capture slide DOM',
'2. Convert captured image to base64 data URL',
'3. Call /api/export/ppt-to-image with the image data',
'4. Receive persistent link for the generated image'
],
alternativeApi: '/api/export/ppt-to-image',
requiredParams: {
pptId,
slideIndex,
imageData: 'data:image/jpeg;base64,...',
format,
metadata: {
width,
height,
quality,
backgroundColor,
pixelRatio,
ignoreWebfont,
exportMethod: 'frontend-replication'
}
}
},
slideInfo: {
title: pptData.slides[slideIndex]?.title || `第 ${slideIndex + 1} 页`,
slideCount: pptData.slides.length
}
};
res.json(response);
} catch (error) {
console.error('Frontend replicate single slide failed:', error);
next(error);
}
});
/**
* 前端导出功能复刻 - 批量导出
* POST /api/export/frontend-replicate-batch
*/
router.post('/frontend-replicate-batch', authenticateToken, async (req, res, next) => {
try {
const { pptId, rangeType = 'all', range, currentSlideIndex = 0, format = 'jpeg', quality = 1, width = 1600, height = 900 } = req.body;
const userId = req.user.userId;
console.log(`🖼️ Frontend replicate batch export: userId=${userId}, pptId=${pptId}, rangeType=${rangeType}`);
// 验证参数
if (!pptId) {
return res.status(400).json({ error: 'PPT ID is required' });
}
// 获取PPT数据以验证权限
let pptData;
try {
pptData = await huggingfaceStorageService.getPPTData(userId, pptId);
} catch (error) {
console.log('Huggingface storage failed, trying GitHub...');
try {
pptData = await githubService.getPPTData(userId, pptId);
} catch (githubError) {
console.error('Both storage methods failed:', { huggingface: error.message, github: githubError.message });
return res.status(404).json({ error: 'PPT not found' });
}
}
if (!pptData.slides || pptData.slides.length === 0) {
return res.status(400).json({ error: 'No slides found in PPT' });
}
// 确定要导出的幻灯片范围
let slideIndices = [];
switch (rangeType) {
case 'all':
slideIndices = Array.from({ length: pptData.slides.length }, (_, i) => i);
break;
case 'current':
if (currentSlideIndex >= 0 && currentSlideIndex < pptData.slides.length) {
slideIndices = [currentSlideIndex];
}
break;
case 'custom':
if (Array.isArray(range) && range.length === 2) {
const [start, end] = range;
for (let i = Math.max(0, start); i <= Math.min(end, pptData.slides.length - 1); i++) {
slideIndices.push(i);
}
}
break;
}
if (slideIndices.length === 0) {
return res.status(400).json({ error: 'No valid slides to export' });
}
// 返回批量导出指导信息
const response = {
success: false,
message: 'Frontend rendering required for batch export',
instruction: {
method: 'frontend-batch-rendering',
description: 'Please use frontend to generate all slide images, then call /api/export/ppt-to-images-batch',
slideIndices,
totalSlides: slideIndices.length,
steps: [
'1. Loop through each slide index in slideIndices array',
'2. For each slide, use html-to-image library to capture DOM',
'3. Collect all image data in the required format',
'4. Call /api/export/ppt-to-images-batch with all image data',
'5. Receive persistent links for all generated images'
],
alternativeApi: '/api/export/ppt-to-images-batch',
requiredFormat: {
pptId,
imagesData: [
{
slideIndex: 'number',
imageData: 'data:image/jpeg;base64,...'
}
],
format,
metadata: {
width,
height,
quality,
exportMethod: 'frontend-batch-replication',
rangeType,
range: rangeType === 'custom' ? range : undefined
}
}
},
pptInfo: {
title: pptData.title || '未命名演示文稿',
slideCount: pptData.slides.length,
exportRange: {
type: rangeType,
indices: slideIndices,
total: slideIndices.length
}
}
};
res.json(response);
} catch (error) {
console.error('Frontend replicate batch export failed:', error);
next(error);
}
});
/**
* 前端导出服务健康检查
* GET /api/export/health
*/
router.get('/health', async (req, res, next) => {
try {
console.log('🔍 Checking frontend export service health...');
// 检查持久化链接服务状态
const linkServiceStatus = await persistentImageLinkService.healthCheck();
// 检查存储服务状态
const storageStatus = await huggingfaceStorageService.healthCheck();
const isHealthy = linkServiceStatus.status === 'healthy' && storageStatus.status === 'healthy';
const healthStatus = {
status: isHealthy ? 'healthy' : 'degraded',
timestamp: new Date().toISOString(),
services: {
persistentImageLink: linkServiceStatus,
storage: storageStatus
},
exportMethod: 'frontend-based',
browserDependencies: 'removed'
};
if (isHealthy) {
res.json({
success: true,
service: 'Frontend Export Service',
...healthStatus
});
} else {
res.status(503).json({
success: false,
service: 'Frontend Export Service',
...healthStatus
});
}
} catch (error) {
console.error('Health check failed:', error);
res.status(503).json({
success: false,
status: 'error',
service: 'Frontend Export Service',
error: error.message,
timestamp: new Date().toISOString()
});
}
});
export default router; |