Upload githubService.js
Browse files
backend/src/services/githubService.js
CHANGED
@@ -159,35 +159,93 @@ class GitHubService {
|
|
159 |
console.log(`Successfully saved to GitHub: ${response.data.commit.sha}`);
|
160 |
return response.data;
|
161 |
} catch (error) {
|
162 |
-
console.error(`GitHub save failed
|
163 |
|
164 |
-
//
|
165 |
if (error.response?.status === 404) {
|
|
|
|
|
166 |
try {
|
167 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
168 |
|
169 |
-
//
|
170 |
-
|
171 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
172 |
|
173 |
-
|
174 |
-
|
175 |
-
{
|
176 |
-
message: `Create user directory for ${userId}`,
|
177 |
-
content: readmeContent,
|
178 |
-
branch: 'main'
|
179 |
-
},
|
180 |
-
{
|
181 |
headers: {
|
182 |
'Authorization': `token ${this.token}`,
|
183 |
'Accept': 'application/vnd.github.v3+json'
|
184 |
}
|
185 |
-
}
|
186 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
187 |
|
188 |
-
|
|
|
189 |
|
190 |
// 重试保存PPT文件
|
|
|
191 |
const retryResponse = await axios.put(
|
192 |
`${this.apiUrl}/repos/${owner}/${repo}/contents/${path}`,
|
193 |
payload,
|
@@ -203,11 +261,26 @@ class GitHubService {
|
|
203 |
return retryResponse.data;
|
204 |
|
205 |
} catch (retryError) {
|
206 |
-
console.error(`
|
207 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
208 |
}
|
|
|
|
|
209 |
} else {
|
210 |
-
throw new Error(`GitHub API error: ${error.response?.data?.message || error.message}`);
|
211 |
}
|
212 |
}
|
213 |
}
|
|
|
159 |
console.log(`Successfully saved to GitHub: ${response.data.commit.sha}`);
|
160 |
return response.data;
|
161 |
} catch (error) {
|
162 |
+
console.error(`GitHub save failed:`, error.response?.data || error.message);
|
163 |
|
164 |
+
// 详细的错误处理
|
165 |
if (error.response?.status === 404) {
|
166 |
+
console.log('404 error - attempting to create directory structure...');
|
167 |
+
|
168 |
try {
|
169 |
+
// 方法1: 先检查仓库是否存在和可访问
|
170 |
+
const repoCheckResponse = await axios.get(`${this.apiUrl}/repos/${owner}/${repo}`, {
|
171 |
+
headers: {
|
172 |
+
'Authorization': `token ${this.token}`,
|
173 |
+
'Accept': 'application/vnd.github.v3+json'
|
174 |
+
}
|
175 |
+
});
|
176 |
+
console.log(`Repository exists: ${repoCheckResponse.data.full_name}`);
|
177 |
|
178 |
+
// 方法2: 检查users目录是否存在
|
179 |
+
try {
|
180 |
+
await axios.get(`${this.apiUrl}/repos/${owner}/${repo}/contents/users`, {
|
181 |
+
headers: {
|
182 |
+
'Authorization': `token ${this.token}`,
|
183 |
+
'Accept': 'application/vnd.github.v3+json'
|
184 |
+
}
|
185 |
+
});
|
186 |
+
console.log('Users directory exists');
|
187 |
+
} catch (usersDirError) {
|
188 |
+
console.log('Users directory does not exist, creating...');
|
189 |
+
|
190 |
+
// 创建users目录的README
|
191 |
+
const usersReadmePath = 'users/README.md';
|
192 |
+
const usersReadmeContent = Buffer.from('# Users Directory\n\nThis directory contains user-specific PPT files.\n').toString('base64');
|
193 |
+
|
194 |
+
await axios.put(
|
195 |
+
`${this.apiUrl}/repos/${owner}/${repo}/contents/${usersReadmePath}`,
|
196 |
+
{
|
197 |
+
message: 'Create users directory',
|
198 |
+
content: usersReadmeContent,
|
199 |
+
branch: 'main'
|
200 |
+
},
|
201 |
+
{
|
202 |
+
headers: {
|
203 |
+
'Authorization': `token ${this.token}`,
|
204 |
+
'Accept': 'application/vnd.github.v3+json'
|
205 |
+
}
|
206 |
+
}
|
207 |
+
);
|
208 |
+
console.log('Users directory created');
|
209 |
+
}
|
210 |
|
211 |
+
// 方法3: 检查用户目录是否存在
|
212 |
+
try {
|
213 |
+
await axios.get(`${this.apiUrl}/repos/${owner}/${repo}/contents/users/${userId}`, {
|
|
|
|
|
|
|
|
|
|
|
214 |
headers: {
|
215 |
'Authorization': `token ${this.token}`,
|
216 |
'Accept': 'application/vnd.github.v3+json'
|
217 |
}
|
218 |
+
});
|
219 |
+
console.log(`User directory exists: users/${userId}`);
|
220 |
+
} catch (userDirError) {
|
221 |
+
console.log(`User directory does not exist, creating: users/${userId}`);
|
222 |
+
|
223 |
+
// 创建用户目录的README
|
224 |
+
const userReadmePath = `users/${userId}/README.md`;
|
225 |
+
const userReadmeContent = Buffer.from(`# PPT Files for User ${userId}\n\nThis directory contains PPT files for user ${userId}.\n`).toString('base64');
|
226 |
+
|
227 |
+
await axios.put(
|
228 |
+
`${this.apiUrl}/repos/${owner}/${repo}/contents/${userReadmePath}`,
|
229 |
+
{
|
230 |
+
message: `Create user directory for ${userId}`,
|
231 |
+
content: userReadmeContent,
|
232 |
+
branch: 'main'
|
233 |
+
},
|
234 |
+
{
|
235 |
+
headers: {
|
236 |
+
'Authorization': `token ${this.token}`,
|
237 |
+
'Accept': 'application/vnd.github.v3+json'
|
238 |
+
}
|
239 |
+
}
|
240 |
+
);
|
241 |
+
console.log(`User directory created: users/${userId}`);
|
242 |
+
}
|
243 |
|
244 |
+
// 等待一小会儿让GitHub同步
|
245 |
+
await new Promise(resolve => setTimeout(resolve, 1000));
|
246 |
|
247 |
// 重试保存PPT文件
|
248 |
+
console.log('Retrying PPT file save...');
|
249 |
const retryResponse = await axios.put(
|
250 |
`${this.apiUrl}/repos/${owner}/${repo}/contents/${path}`,
|
251 |
payload,
|
|
|
261 |
return retryResponse.data;
|
262 |
|
263 |
} catch (retryError) {
|
264 |
+
console.error(`Comprehensive retry also failed:`, retryError.response?.data || retryError.message);
|
265 |
+
|
266 |
+
// 如果GitHub彻底失败,fallback到内存存储
|
267 |
+
console.log('GitHub save completely failed, falling back to memory storage...');
|
268 |
+
try {
|
269 |
+
const memoryResult = await memoryStorageService.saveFile(userId, fileName, data);
|
270 |
+
console.log('Successfully saved to memory storage as fallback');
|
271 |
+
return {
|
272 |
+
...memoryResult,
|
273 |
+
warning: 'Saved to temporary memory storage due to GitHub issues'
|
274 |
+
};
|
275 |
+
} catch (memoryError) {
|
276 |
+
console.error('Memory storage fallback also failed:', memoryError.message);
|
277 |
+
throw new Error(`All storage methods failed. GitHub: ${retryError.message}, Memory: ${memoryError.message}`);
|
278 |
+
}
|
279 |
}
|
280 |
+
} else if (error.response?.status === 403) {
|
281 |
+
throw new Error(`GitHub permission denied. Check if the token has 'repo' permissions: ${error.response.data.message}`);
|
282 |
} else {
|
283 |
+
throw new Error(`GitHub API error (${error.response?.status}): ${error.response?.data?.message || error.message}`);
|
284 |
}
|
285 |
}
|
286 |
}
|