Spaces:
Running
Running
File size: 6,926 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 |
import express from 'express';
import persistentImageLinkService from '../services/persistentImageLinkService.js';
import { authenticateToken } from '../middleware/auth.js';
const router = express.Router();
/**
* 生成PPT所有页面的持久化链接
* POST /api/persistent-links/generate-all
*/
router.post('/generate-all', authenticateToken, async (req, res, next) => {
try {
const { pptId, slides, options = {} } = req.body;
const userId = req.user.userId;
console.log(`🔗 Generate all persistent links: userId=${userId}, pptId=${pptId}, slides=${slides?.length}`);
// 验证参数
if (!pptId) {
return res.status(400).json({ error: 'PPT ID is required' });
}
if (!slides || !Array.isArray(slides) || slides.length === 0) {
return res.status(400).json({ error: 'Slides data is required' });
}
// 使用持久化链接服务批量生成链接
const results = await persistentImageLinkService.updateAllPersistentLinksWithFrontendExport(
userId,
pptId,
slides,
{
format: 'jpg',
quality: 0.9,
viewportSize: 1000,
viewportRatio: 0.5625,
...options
}
);
// 统计成功和失败的数量
const successCount = results.filter(r => r.success).length;
const failureCount = results.filter(r => !r.success).length;
console.log(`✅ Persistent links generation completed: ${successCount} success, ${failureCount} failed`);
// 返回结果
res.json({
success: true,
message: `成功生成 ${successCount} 个持久化链接${failureCount > 0 ? `,${failureCount} 个失败` : ''}`,
links: results.filter(r => r.success).map(r => ({
linkId: r.linkId,
url: r.url,
publicUrl: r.publicUrl,
pageIndex: r.pageIndex
})),
stats: {
total: slides.length,
success: successCount,
failure: failureCount
},
errors: results.filter(r => !r.success).map(r => ({
pageIndex: r.pageIndex,
error: r.error
}))
});
} catch (error) {
console.error('Generate all persistent links failed:', error);
next(error);
}
});
/**
* 获取PPT的所有持久化链接
* GET /api/persistent-links/:pptId
*/
router.get('/:pptId', authenticateToken, async (req, res, next) => {
try {
const { pptId } = req.params;
const userId = req.user.userId;
console.log(`🔍 Get persistent links: userId=${userId}, pptId=${pptId}`);
// 获取用户的所有持久化链接
const userLinks = await persistentImageLinkService.getUserLinks(userId);
// 过滤出指定PPT的链接
const pptLinks = userLinks.filter(link => link.pptId === pptId);
// 按页面索引排序
pptLinks.sort((a, b) => a.pageIndex - b.pageIndex);
res.json({
success: true,
links: pptLinks.map(link => ({
linkId: link.linkId,
url: link.url,
publicUrl: link.publicUrl,
pageIndex: link.pageIndex,
lastUpdated: link.lastUpdated,
hasImage: link.hasImage,
imageSize: link.imageSize,
format: link.format
}))
});
} catch (error) {
console.error('Get persistent links failed:', error);
next(error);
}
});
/**
* 更新单个页面的持久化链接
* POST /api/persistent-links/:pptId/:pageIndex/update
*/
router.post('/:pptId/:pageIndex/update', authenticateToken, async (req, res, next) => {
try {
const { pptId, pageIndex } = req.params;
const { slideData, options = {} } = req.body;
const userId = req.user.userId;
console.log(`🔄 Update persistent link: userId=${userId}, pptId=${pptId}, pageIndex=${pageIndex}`);
// 验证参数
if (!slideData) {
return res.status(400).json({ error: 'Slide data is required' });
}
const pageIdx = parseInt(pageIndex);
if (isNaN(pageIdx) || pageIdx < 0) {
return res.status(400).json({ error: 'Invalid page index' });
}
// 获取或创建持久化链接
const result = await persistentImageLinkService.getOrCreatePersistentLink(
userId,
pptId,
pageIdx,
slideData,
{
format: 'jpg',
quality: 0.9,
viewportSize: 1000,
viewportRatio: 0.5625,
...options
}
);
console.log(`✅ Persistent link updated: ${result.linkId}`);
res.json({
success: true,
linkId: result.linkId,
url: result.url,
publicUrl: result.publicUrl,
pageIndex: pageIdx
});
} catch (error) {
console.error('Update persistent link failed:', error);
next(error);
}
});
/**
* 删除持久化链接
* DELETE /api/persistent-links/:linkId
*/
router.delete('/:linkId', authenticateToken, async (req, res, next) => {
try {
const { linkId } = req.params;
const userId = req.user.userId;
console.log(`🗑️ Delete persistent link: userId=${userId}, linkId=${linkId}`);
// 验证链接所有权
const linkInfo = await persistentImageLinkService.getPersistentImage(linkId);
if (!linkInfo || linkInfo.userId !== userId) {
return res.status(404).json({ error: 'Persistent link not found or access denied' });
}
// 删除链接
await persistentImageLinkService.deletePersistentLink(linkId);
console.log(`✅ Persistent link deleted: ${linkId}`);
res.json({
success: true,
message: 'Persistent link deleted successfully'
});
} catch (error) {
console.error('Delete persistent link failed:', error);
next(error);
}
});
/**
* 获取持久化链接服务统计信息
* GET /api/persistent-links/stats
*/
router.get('/stats', authenticateToken, async (req, res, next) => {
try {
const userId = req.user.userId;
console.log(`📊 Get persistent links stats: userId=${userId}`);
// 获取用户的所有链接
const userLinks = await persistentImageLinkService.getUserLinks(userId);
// 计算统计信息
const stats = {
totalLinks: userLinks.length,
linksWithImages: userLinks.filter(link => link.hasImage).length,
totalImageSize: userLinks.reduce((sum, link) => sum + (link.imageSize || 0), 0),
lastUpdated: userLinks.length > 0 ?
Math.max(...userLinks.map(link => new Date(link.lastUpdated || 0).getTime())) : null,
pptCount: new Set(userLinks.map(link => link.pptId)).size
};
res.json({
success: true,
stats: {
...stats,
lastUpdated: stats.lastUpdated ? new Date(stats.lastUpdated).toISOString() : null,
averageImageSize: stats.linksWithImages > 0 ?
Math.round(stats.totalImageSize / stats.linksWithImages) : 0,
totalImageSizeMB: Math.round(stats.totalImageSize / 1024 / 1024 * 100) / 100
}
});
} catch (error) {
console.error('Get persistent links stats failed:', error);
next(error);
}
});
export default router; |