|
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();
|
|
|
|
|
|
router.use((req, res, next) => {
|
|
console.log(`PPT Router - ${req.method} ${req.path} - Body:`, Object.keys(req.body || {}));
|
|
next();
|
|
});
|
|
|
|
|
|
const getStorageService = () => {
|
|
|
|
if (!process.env.GITHUB_TOKEN) {
|
|
return memoryStorageService;
|
|
}
|
|
return githubService;
|
|
};
|
|
|
|
|
|
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);
|
|
}
|
|
});
|
|
|
|
|
|
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;
|
|
|
|
|
|
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);
|
|
}
|
|
});
|
|
|
|
|
|
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();
|
|
|
|
|
|
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);
|
|
}
|
|
});
|
|
|
|
|
|
router.post('/create', async (req, res, next) => {
|
|
try {
|
|
const userId = req.user.userId;
|
|
const { title } = req.body;
|
|
|
|
if (!title) {
|
|
return res.status(400).json({ error: 'Title is required' });
|
|
}
|
|
|
|
const pptId = uuidv4();
|
|
const now = new Date().toISOString();
|
|
|
|
const pptData = {
|
|
id: pptId,
|
|
title,
|
|
theme: {
|
|
backgroundColor: '#ffffff',
|
|
themeColor: '#d14424',
|
|
fontColor: '#333333',
|
|
fontName: 'Microsoft YaHei'
|
|
},
|
|
slides: [
|
|
{
|
|
id: uuidv4(),
|
|
elements: [
|
|
{
|
|
type: 'text',
|
|
id: uuidv4(),
|
|
left: 150,
|
|
top: 200,
|
|
width: 600,
|
|
height: 100,
|
|
content: title,
|
|
fontSize: 32,
|
|
fontName: 'Microsoft YaHei',
|
|
defaultColor: '#333333',
|
|
bold: true,
|
|
align: 'center'
|
|
}
|
|
],
|
|
background: {
|
|
type: 'solid',
|
|
color: '#ffffff'
|
|
}
|
|
}
|
|
],
|
|
createdAt: now,
|
|
updatedAt: now
|
|
};
|
|
|
|
const storageService = getStorageService();
|
|
const fileName = `${pptId}.json`;
|
|
|
|
console.log(`Creating PPT for user ${userId}, using storage: ${storageService === githubService ? 'GitHub' : 'Memory'}`);
|
|
|
|
try {
|
|
await storageService.saveFile(userId, fileName, pptData);
|
|
console.log(`PPT created successfully: ${pptId}`);
|
|
res.json({ success: true, pptId, pptData });
|
|
} catch (saveError) {
|
|
console.error('Error saving PPT:', saveError.message);
|
|
|
|
|
|
if (storageService === githubService) {
|
|
console.log('GitHub save failed, falling back to memory storage');
|
|
try {
|
|
await memoryStorageService.saveFile(userId, fileName, pptData);
|
|
console.log(`PPT saved to memory storage: ${pptId}`);
|
|
res.json({
|
|
success: true,
|
|
pptId,
|
|
pptData,
|
|
warning: 'Saved to temporary storage due to GitHub connection issue'
|
|
});
|
|
} catch (memoryError) {
|
|
console.error('Memory storage also failed:', memoryError.message);
|
|
throw new Error('Failed to save PPT to any storage');
|
|
}
|
|
} else {
|
|
throw saveError;
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error('PPT creation error:', error);
|
|
next(error);
|
|
}
|
|
});
|
|
|
|
|
|
router.delete('/:pptId', async (req, res, next) => {
|
|
try {
|
|
const userId = req.user.userId;
|
|
const { pptId } = req.params;
|
|
const fileName = `${pptId}.json`;
|
|
const storageService = getStorageService();
|
|
|
|
|
|
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);
|
|
}
|
|
});
|
|
|
|
|
|
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();
|
|
|
|
|
|
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' });
|
|
}
|
|
|
|
|
|
const newPptId = uuidv4();
|
|
const newFileName = `${newPptId}.json`;
|
|
const newPPTData = {
|
|
...sourcePPT,
|
|
id: newPptId,
|
|
title: title || `${sourcePPT.title} - 副本`,
|
|
createdAt: new Date().toISOString(),
|
|
updatedAt: new Date().toISOString()
|
|
};
|
|
|
|
|
|
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; |