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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import express from 'express';
import { authenticateToken } from '../middleware/auth.js';
import persistentImageLinkService from '../services/persistentImageLinkService.js';
import githubService from '../services/githubService.js';

const router = express.Router();

// 添加路由级别的日志中间件
router.use((req, res, next) => {
  console.log(`Persistent Images Router - ${req.method} ${req.path}`);
  next();
});

/**
 * 获取或创建PPT页面的持久化链接
 * POST /api/persistent-images/create
 */
router.post('/create', authenticateToken, async (req, res, next) => {
  try {
    const userId = req.user.userId;
    const {
      pptId,
      pageIndex,
      slideId,
      slideData,
      options = {}
    } = req.body;

    // 验证必需参数 - 需要slideId或pageIndex其中之一
    if (!pptId || (slideId === undefined && pageIndex === undefined)) {
      return res.status(400).json({
        error: 'Missing required parameters: pptId and (slideId or pageIndex)'
      });
    }

    // 优先使用slideId,如果不存在则使用pageIndex
    const uniqueId = slideId || pageIndex;
    
    console.log(`🔗 Creating/getting persistent link for PPT ${pptId}, uniqueId ${uniqueId}`);

    // 获取或创建持久化链接
    const result = await persistentImageLinkService.getOrCreatePersistentLink(
      userId,
      pptId,
      uniqueId,
      slideData,
      options
    );

    res.json({
      success: true,
      message: 'Persistent link created/retrieved successfully',
      ...result
    });
  } catch (error) {
    console.error('❌ Failed to create persistent link:', error);
    next(error);
  }
});

/**
 * 批量创建PPT所有页面的持久化链接
 * POST /api/persistent-images/create-all
 */
router.post('/create-all', authenticateToken, async (req, res, next) => {
  try {
    const userId = req.user.userId;
    const {
      pptId,
      slides,
      options = {}
    } = req.body;

    // 验证必需参数
    if (!pptId || !slides || !Array.isArray(slides)) {
      return res.status(400).json({
        error: 'Missing required parameters: pptId, slides (array)'
      });
    }

    console.log(`🔗 Creating persistent links for ${slides.length} pages of PPT ${pptId}`);

    // 批量创建持久化链接
    const results = await persistentImageLinkService.updateAllPersistentLinks(
      userId,
      pptId,
      slides,
      options
    );

    // 统计结果
    const successCount = results.filter(r => r.success).length;
    const failureCount = results.length - successCount;

    console.log(`✅ Created ${successCount} persistent links, ${failureCount} failed`);

    // 转换结果格式以匹配前端期望的数据结构
    const links = results.filter(r => r.success).map(result => ({
      linkId: result.linkId,
      url: result.url,
      publicUrl: result.publicUrl,
      pageIndex: result.pageIndex,
      slideId: result.slideId,
      lastUpdated: result.lastUpdated
    }));

    res.json({
      success: true,
      message: `Created ${successCount} persistent links successfully`,
      totalPages: slides.length,
      successCount,
      failureCount,
      links,
      results
    });
  } catch (error) {
    console.error('❌ Failed to create persistent links:', error);
    next(error);
  }
});

/**
 * 更新PPT页面图片
 * PUT /api/persistent-images/:linkId
 */
router.put('/:linkId', authenticateToken, async (req, res, next) => {
  try {
    const { linkId } = req.params;
    const {
      slideData,
      options = {}
    } = req.body;

    // 验证必需参数
    if (!slideData) {
      return res.status(400).json({
        error: 'Missing required parameter: slideData'
      });
    }

    console.log(`🔄 Updating image for persistent link: ${linkId}`);

    // 更新图片
    await persistentImageLinkService.updatePageImage(linkId, slideData, options);

    res.json({
      success: true,
      message: 'Image updated successfully',
      linkId,
      url: `/api/persistent-images/${linkId}`
    });
  } catch (error) {
    console.error(`❌ Failed to update persistent link ${req.params.linkId}:`, error);
    next(error);
  }
});

/**
 * 更新持久化链接的图片内容(通过图片数据)
 * POST /api/persistent-images/update-image
 */
router.post('/update-image', authenticateToken, async (req, res, next) => {
  try {
    const {
      linkId,
      imageData,
      format = 'jpeg'
    } = req.body;

    // 验证必需参数
    if (!linkId || !imageData) {
      return res.status(400).json({
        error: 'Missing required parameters: linkId, imageData'
      });
    }

    console.log(`🔄 Updating image content for persistent link: ${linkId}`);

    // 将base64图片数据转换为Buffer
    let imageBuffer;
    try {
      // 移除data:image/jpeg;base64,前缀(如果存在)
      const base64Data = imageData.replace(/^data:image\/[a-z]+;base64,/, '');
      imageBuffer = Buffer.from(base64Data, 'base64');
    } catch (error) {
      return res.status(400).json({
        error: 'Invalid image data format'
      });
    }

    // 直接更新持久化链接的图片内容
    const result = await persistentImageLinkService.updateImageContent(linkId, imageBuffer, format);

    if (!result.success) {
      return res.status(404).json({
        error: 'Persistent link not found',
        linkId
      });
    }

    res.json({
      success: true,
      message: 'Image content updated successfully',
      linkId,
      url: `/api/persistent-images/${linkId}`,
      size: imageBuffer.length,
      format
    });
  } catch (error) {
    console.error(`❌ Failed to update image content for link ${req.body.linkId}:`, error);
    next(error);
  }
});

/**
 * 获取用户的所有持久化链接
 * GET /api/persistent-images/user/list
 */
router.get('/user/list', authenticateToken, async (req, res, next) => {
  try {
    const userId = req.user.userId;
    const { pptId } = req.query;

    console.log(`📋 Getting persistent links for user ${userId}${pptId ? `, PPT ${pptId}` : ''}`);

    const userLinks = persistentImageLinkService.getUserPersistentLinks(userId, pptId);

    res.json({
      success: true,
      userId,
      pptId: pptId || null,
      linkCount: userLinks.length,
      links: userLinks
    });
  } catch (error) {
    console.error(`❌ Failed to get user persistent links:`, error);
    next(error);
  }
});

/**
 * 获取服务统计信息
 * GET /api/persistent-images/stats
 */
router.get('/stats', authenticateToken, async (req, res, next) => {
  try {
    console.log('📊 Getting persistent image service stats');

    const stats = persistentImageLinkService.getStats();

    res.json({
      success: true,
      stats
    });
  } catch (error) {
    console.error('❌ Failed to get persistent image stats:', error);
    next(error);
  }
});

/**
 * 获取持久化链接的图片 (通过 pptId 和 slideId)
 * GET /api/persistent-images/:pptId/:slideId
 */
router.get('/:pptId/:slideId', async (req, res, next) => {
  try {
    const { pptId, slideId } = req.params;
    const { download } = req.query;

    // 验证pptId格式 (应该是UUID格式)
    const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
    if (!uuidRegex.test(pptId)) {
      return next(); // 不是UUID格式,继续到下一个路由
    }

    // 验证slideId格式 (不应该包含文件扩展名)
    if (slideId.includes('.')) {
      return next(); // 包含文件扩展名,可能是静态文件请求
    }

    console.log(`🖼️ Getting persistent image by pptId: ${pptId}, slideId: ${slideId}`);

    // 通过 pptId 和 slideId 查找对应的链接
    const linkInfo = await persistentImageLinkService.findLinkByPptAndSlide(pptId, slideId);
    
    if (!linkInfo) {
      return res.status(404).json({
        error: 'Persistent image not found for the specified PPT and slide',
        pptId,
        slideId
      });
    }

    // 获取图片
    const imageResult = await persistentImageLinkService.getPersistentImage(linkInfo.linkId);
    
    if (!imageResult.success) {
      return res.status(404).json({
        error: 'Persistent image not found',
        pptId,
        slideId,
        linkId: linkInfo.linkId
      });
    }

    const { data: imageBuffer, metadata } = imageResult;

    // 设置响应头
    const contentType = metadata.format === 'jpg' ? 'image/jpeg' : `image/${metadata.format}`;
    res.set({
      'Content-Type': contentType,
      'Content-Length': metadata.size,
      'Cache-Control': 'public, max-age=3600', // 缓存1小时
      'ETag': `"${linkInfo.linkId}"`,
      'Last-Modified': new Date(metadata.lastUpdated).toUTCString()
    });

    // 如果是下载请求,设置下载头
    if (download) {
      const fileName = `persistent-slide-${pptId}-${slideId}.${metadata.format}`;
      res.set('Content-Disposition', `attachment; filename="${fileName}"`);
    }

    // 检查条件请求
    const ifNoneMatch = req.get('If-None-Match');
    if (ifNoneMatch === `"${linkInfo.linkId}"`) {
      return res.status(304).end();
    }

    res.send(imageBuffer);
  } catch (error) {
    console.error(`❌ Failed to get persistent image ${req.params.pptId}/${req.params.slideId}:`, error);
    if (error.message.includes('not found')) {
      return res.status(404).json({
        error: 'Persistent image not found',
        pptId: req.params.pptId,
        slideId: req.params.slideId
      });
    }
    next(error);
  }
});

/**
 * 获取持久化链接的图片 (通过 pptId 或 linkId)
 * GET /api/persistent-images/:id
 */
router.get('/:id', async (req, res, next) => {
  try {
    const { id } = req.params;
    const { download } = req.query;

    console.log(`🖼️ Getting persistent image: ${id}`);

    // 检查是否是PPT ID格式(通常比linkId短,且可能包含特殊字符)
    // 如果是PPT ID,返回该PPT的第一页图片
    let imageResult;
    let isLinkId = false;
    
    try {
      // 首先尝试作为linkId获取图片
      imageResult = await persistentImageLinkService.getPersistentImage(id);
      isLinkId = true;
    } catch (error) {
      // 如果作为linkId失败,尝试作为PPT ID获取第一页图片
      console.log(`⚠️ Not found as linkId, trying as PPT ID: ${id}`);
      
      // 查找该PPT的第一页链接
      const links = [];
      for (const [linkId, linkInfo] of persistentImageLinkService.persistentLinks) {
        if (linkInfo.pptId === id && linkInfo.hasImage) {
          links.push({ linkId, pageIndex: linkInfo.pageIndex || 0 });
        }
      }
      
      if (links.length === 0) {
        return res.status(404).json({
          error: 'No persistent images found for PPT',
          pptId: id
        });
      }
      
      // 按页面索引排序,取第一页
      links.sort((a, b) => a.pageIndex - b.pageIndex);
      const firstPageLinkId = links[0].linkId;
      
      imageResult = await persistentImageLinkService.getPersistentImage(firstPageLinkId);
    }
    
    if (!imageResult.success) {
      return res.status(404).json({
        error: 'Persistent image not found',
        id
      });
    }

    const { data: imageBuffer, metadata } = imageResult;

    // 设置响应头
    const contentType = metadata.format === 'jpg' ? 'image/jpeg' : `image/${metadata.format}`;
    res.set({
      'Content-Type': contentType,
      'Content-Length': metadata.size,
      'Cache-Control': 'public, max-age=3600', // 缓存1小时
      'ETag': `"${id}"`,
      'Last-Modified': new Date(metadata.lastUpdated).toUTCString()
    });

    // 如果是下载请求,设置下载头
    if (download) {
      const fileName = isLinkId ? `persistent-slide-${id}.${metadata.format}` : `persistent-ppt-${id}.${metadata.format}`;
      res.set('Content-Disposition', `attachment; filename="${fileName}"`);
    }

    // 检查条件请求
    const ifNoneMatch = req.get('If-None-Match');
    if (ifNoneMatch === `"${id}"`) {
      return res.status(304).end();
    }

    res.send(imageBuffer);
  } catch (error) {
    console.error(`❌ Failed to get persistent image ${req.params.id}:`, error);
    if (error.message.includes('not found')) {
      return res.status(404).json({
        error: 'Persistent image not found',
        id: req.params.id
      });
    }
    next(error);
  }
});

/**
 * 删除持久化链接
 * DELETE /api/persistent-images/:linkId
 */
router.delete('/:linkId', authenticateToken, async (req, res, next) => {
  try {
    const { linkId } = req.params;

    console.log(`🗑️ Deleting persistent link: ${linkId}`);

    // 删除持久化链接
    const deleteResult = await persistentImageLinkService.deletePersistentLink(linkId);

    if (deleteResult) {
      res.json({
        success: true,
        message: 'Persistent link deleted successfully',
        linkId
      });
    } else {
      res.status(404).json({
        error: 'Persistent link not found',
        linkId
      });
    }
  } catch (error) {
    console.error(`❌ Failed to delete persistent link ${req.params.linkId}:`, error);
    next(error);
  }
});

/**
 * 从PPT数据同步创建持久化链接
 * POST /api/persistent-images/sync-from-ppt
 */
router.post('/sync-from-ppt', authenticateToken, async (req, res, next) => {
  try {
    const userId = req.user.userId;
    const { pptId } = req.body;

    // 验证必需参数
    if (!pptId) {
      return res.status(400).json({
        error: 'Missing required parameter: pptId'
      });
    }

    console.log(`🔄 Syncing persistent links from PPT data: ${pptId}`);

    // 从GitHub服务获取PPT数据
    const pptData = await githubService.getPPT(userId, pptId);
    
    if (!pptData || !pptData.slides) {
      return res.status(404).json({
        error: 'PPT not found or has no slides',
        pptId
      });
    }

    // 创建持久化链接(不生成图片,只创建链接)
    const results = [];
    for (let pageIndex = 0; pageIndex < pptData.slides.length; pageIndex++) {
      try {
        const result = await persistentImageLinkService.getOrCreatePersistentLink(
          userId,
          pptId,
          pageIndex,
          null, // 不提供slideData,只创建链接
          {}
        );
        
        results.push({
          pageIndex,
          success: true,
          ...result
        });
      } catch (error) {
        console.error(`❌ Failed to sync persistent link for page ${pageIndex}:`, error);
        results.push({
          pageIndex,
          success: false,
          error: error.message
        });
      }
    }

    const successCount = results.filter(r => r.success).length;
    const failureCount = results.length - successCount;

    console.log(`✅ Synced ${successCount} persistent links, ${failureCount} failed`);

    res.json({
      success: true,
      message: `Synced ${successCount} persistent links from PPT data`,
      pptId,
      totalPages: pptData.slides.length,
      successCount,
      failureCount,
      results
    });
  } catch (error) {
    console.error('❌ Failed to sync persistent links from PPT:', error);
    next(error);
  }
});

export default router;