CatPtain's picture
Upload 10 files
b7560a4 verified
raw
history blame
6.6 kB
import express from 'express';
import { v4 as uuidv4 } from 'uuid';
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;
};
// 获取用户的PPT列表
router.get('/list', async (req, res, next) => {
try {
const userId = req.user.userId;
const storageService = getStorageService();
const pptList = await storageService.getUserPPTList(userId);
res.json(pptList);
} catch (error) {
next(error);
}
});
// 获取指定PPT数据
router.get('/:pptId', async (req, res, next) => {
try {
const userId = req.user.userId;
const { 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' });
}
res.json(pptData);
} catch (error) {
next(error);
}
});
// 保存PPT数据
router.post('/save', async (req, res, next) => {
try {
const userId = req.user.userId;
const { pptId, title, slides, theme } = req.body;
if (!pptId || !slides) {
return res.status(400).json({ error: 'PPT ID and slides are required' });
}
const fileName = `${pptId}.json`;
const pptData = {
id: pptId,
title: title || '未命名演示文稿',
slides: slides,
theme: theme || {},
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString()
};
const storageService = getStorageService();
// 如果是GitHub服务,保存到第一个仓库
if (storageService === githubService) {
await storageService.saveFile(userId, fileName, pptData, 0);
} else {
// 内存存储服务
await storageService.saveFile(userId, fileName, pptData);
}
res.json({ message: 'PPT saved successfully', pptId });
} catch (error) {
next(error);
}
});
// 创建新PPT
router.post('/create', async (req, res, next) => {
try {
const userId = req.user.userId;
const { title } = req.body;
const pptId = uuidv4();
const fileName = `${pptId}.json`;
// 创建默认PPT数据
const defaultSlide = {
id: uuidv4(),
elements: [],
background: {
type: 'solid',
color: '#ffffff'
}
};
const pptData = {
id: pptId,
title: title || '新建演示文稿',
slides: [defaultSlide],
theme: {},
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString()
};
const storageService = getStorageService();
// 保存新创建的PPT
if (storageService === githubService) {
await storageService.saveFile(userId, fileName, pptData, 0);
} else {
await storageService.saveFile(userId, fileName, pptData);
}
res.json({
message: 'PPT created successfully',
pptId,
ppt: pptData
});
} catch (error) {
next(error);
}
});
// 删除PPT
router.delete('/:pptId', async (req, res, next) => {
try {
const userId = req.user.userId;
const { pptId } = req.params;
const fileName = `${pptId}.json`;
const storageService = getStorageService();
// 如果是GitHub服务,从所有仓库中删除
if (storageService === githubService && storageService.repositories) {
for (let i = 0; i < storageService.repositories.length; i++) {
try {
await storageService.deleteFile(userId, fileName, i);
} catch (error) {
// 继续尝试其他仓库
continue;
}
}
} else {
// 内存存储服务
await storageService.deleteFile(userId, fileName);
}
res.json({ message: 'PPT deleted successfully' });
} catch (error) {
next(error);
}
});
// 复制PPT
router.post('/:pptId/copy', async (req, res, next) => {
try {
const userId = req.user.userId;
const { pptId } = req.params;
const { title } = req.body;
const sourceFileName = `${pptId}.json`;
const storageService = getStorageService();
// 获取源PPT数据
let sourcePPT = null;
if (storageService === githubService && storageService.repositories) {
for (let i = 0; i < storageService.repositories.length; i++) {
try {
const result = await storageService.getFile(userId, sourceFileName, i);
if (result) {
sourcePPT = result.content;
break;
}
} catch (error) {
continue;
}
}
} else {
const result = await storageService.getFile(userId, sourceFileName);
if (result) {
sourcePPT = result.content;
}
}
if (!sourcePPT) {
return res.status(404).json({ error: 'Source PPT not found' });
}
// 创建新的PPT ID和数据
const newPptId = uuidv4();
const newFileName = `${newPptId}.json`;
const newPPTData = {
...sourcePPT,
id: newPptId,
title: title || `${sourcePPT.title} - 副本`,
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString()
};
// 保存复制的PPT
if (storageService === githubService) {
await storageService.saveFile(userId, newFileName, newPPTData, 0);
} else {
await storageService.saveFile(userId, newFileName, newPPTData);
}
res.json({
message: 'PPT copied successfully',
pptId: newPptId,
ppt: newPPTData
});
} catch (error) {
next(error);
}
});
export default router;