Upload ppt.js
Browse files- backend/src/routes/ppt.js +68 -26
backend/src/routes/ppt.js
CHANGED
|
@@ -108,44 +108,86 @@ router.post('/create', async (req, res, next) => {
|
|
| 108 |
try {
|
| 109 |
const userId = req.user.userId;
|
| 110 |
const { title } = req.body;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
const pptId = uuidv4();
|
| 113 |
-
const
|
| 114 |
|
| 115 |
-
// 创建默认PPT数据
|
| 116 |
-
const defaultSlide = {
|
| 117 |
-
id: uuidv4(),
|
| 118 |
-
elements: [],
|
| 119 |
-
background: {
|
| 120 |
-
type: 'solid',
|
| 121 |
-
color: '#ffffff'
|
| 122 |
-
}
|
| 123 |
-
};
|
| 124 |
-
|
| 125 |
const pptData = {
|
| 126 |
id: pptId,
|
| 127 |
-
title
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
};
|
| 133 |
|
| 134 |
const storageService = getStorageService();
|
|
|
|
| 135 |
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
} else {
|
| 140 |
await storageService.saveFile(userId, fileName, pptData);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
}
|
| 142 |
-
|
| 143 |
-
res.json({
|
| 144 |
-
message: 'PPT created successfully',
|
| 145 |
-
pptId,
|
| 146 |
-
ppt: pptData
|
| 147 |
-
});
|
| 148 |
} catch (error) {
|
|
|
|
| 149 |
next(error);
|
| 150 |
}
|
| 151 |
});
|
|
|
|
| 108 |
try {
|
| 109 |
const userId = req.user.userId;
|
| 110 |
const { title } = req.body;
|
| 111 |
+
|
| 112 |
+
if (!title) {
|
| 113 |
+
return res.status(400).json({ error: 'Title is required' });
|
| 114 |
+
}
|
| 115 |
|
| 116 |
const pptId = uuidv4();
|
| 117 |
+
const now = new Date().toISOString();
|
| 118 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
const pptData = {
|
| 120 |
id: pptId,
|
| 121 |
+
title,
|
| 122 |
+
theme: {
|
| 123 |
+
backgroundColor: '#ffffff',
|
| 124 |
+
themeColor: '#d14424',
|
| 125 |
+
fontColor: '#333333',
|
| 126 |
+
fontName: 'Microsoft YaHei'
|
| 127 |
+
},
|
| 128 |
+
slides: [
|
| 129 |
+
{
|
| 130 |
+
id: uuidv4(),
|
| 131 |
+
elements: [
|
| 132 |
+
{
|
| 133 |
+
type: 'text',
|
| 134 |
+
id: uuidv4(),
|
| 135 |
+
left: 150,
|
| 136 |
+
top: 200,
|
| 137 |
+
width: 600,
|
| 138 |
+
height: 100,
|
| 139 |
+
content: title,
|
| 140 |
+
fontSize: 32,
|
| 141 |
+
fontName: 'Microsoft YaHei',
|
| 142 |
+
defaultColor: '#333333',
|
| 143 |
+
bold: true,
|
| 144 |
+
align: 'center'
|
| 145 |
+
}
|
| 146 |
+
],
|
| 147 |
+
background: {
|
| 148 |
+
type: 'solid',
|
| 149 |
+
color: '#ffffff'
|
| 150 |
+
}
|
| 151 |
+
}
|
| 152 |
+
],
|
| 153 |
+
createdAt: now,
|
| 154 |
+
updatedAt: now
|
| 155 |
};
|
| 156 |
|
| 157 |
const storageService = getStorageService();
|
| 158 |
+
const fileName = `${pptId}.json`;
|
| 159 |
|
| 160 |
+
console.log(`Creating PPT for user ${userId}, using storage: ${storageService === githubService ? 'GitHub' : 'Memory'}`);
|
| 161 |
+
|
| 162 |
+
try {
|
|
|
|
| 163 |
await storageService.saveFile(userId, fileName, pptData);
|
| 164 |
+
console.log(`PPT created successfully: ${pptId}`);
|
| 165 |
+
res.json({ success: true, pptId, pptData });
|
| 166 |
+
} catch (saveError) {
|
| 167 |
+
console.error('Error saving PPT:', saveError.message);
|
| 168 |
+
|
| 169 |
+
// 如果GitHub保存失败,尝试使用内存存储作为fallback
|
| 170 |
+
if (storageService === githubService) {
|
| 171 |
+
console.log('GitHub save failed, falling back to memory storage');
|
| 172 |
+
try {
|
| 173 |
+
await memoryStorageService.saveFile(userId, fileName, pptData);
|
| 174 |
+
console.log(`PPT saved to memory storage: ${pptId}`);
|
| 175 |
+
res.json({
|
| 176 |
+
success: true,
|
| 177 |
+
pptId,
|
| 178 |
+
pptData,
|
| 179 |
+
warning: 'Saved to temporary storage due to GitHub connection issue'
|
| 180 |
+
});
|
| 181 |
+
} catch (memoryError) {
|
| 182 |
+
console.error('Memory storage also failed:', memoryError.message);
|
| 183 |
+
throw new Error('Failed to save PPT to any storage');
|
| 184 |
+
}
|
| 185 |
+
} else {
|
| 186 |
+
throw saveError;
|
| 187 |
+
}
|
| 188 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
} catch (error) {
|
| 190 |
+
console.error('PPT creation error:', error);
|
| 191 |
next(error);
|
| 192 |
}
|
| 193 |
});
|