Upload githubService.js
Browse files
backend/src/services/githubService.js
CHANGED
@@ -116,21 +116,24 @@ class GitHubService {
|
|
116 |
const { owner, repo } = this.parseRepoUrl(repoUrl);
|
117 |
const path = `users/${userId}/${fileName}`;
|
118 |
|
|
|
|
|
119 |
// 先尝试获取现有文件的SHA
|
120 |
let sha = null;
|
121 |
try {
|
122 |
const existing = await this.getFile(userId, fileName, repoIndex);
|
123 |
if (existing) {
|
124 |
sha = existing.sha;
|
|
|
125 |
}
|
126 |
} catch (error) {
|
127 |
-
|
128 |
}
|
129 |
|
130 |
const content = Buffer.from(JSON.stringify(data, null, 2)).toString('base64');
|
131 |
|
132 |
const payload = {
|
133 |
-
message:
|
134 |
content: content,
|
135 |
branch: 'main'
|
136 |
};
|
@@ -139,18 +142,74 @@ class GitHubService {
|
|
139 |
payload.sha = sha;
|
140 |
}
|
141 |
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
|
|
|
|
|
|
|
|
149 |
}
|
150 |
-
|
151 |
-
);
|
152 |
|
153 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
154 |
}
|
155 |
|
156 |
// 获取用户的所有PPT列表
|
|
|
116 |
const { owner, repo } = this.parseRepoUrl(repoUrl);
|
117 |
const path = `users/${userId}/${fileName}`;
|
118 |
|
119 |
+
console.log(`Attempting to save file: ${path} to repo: ${owner}/${repo}`);
|
120 |
+
|
121 |
// 先尝试获取现有文件的SHA
|
122 |
let sha = null;
|
123 |
try {
|
124 |
const existing = await this.getFile(userId, fileName, repoIndex);
|
125 |
if (existing) {
|
126 |
sha = existing.sha;
|
127 |
+
console.log(`Found existing file with SHA: ${sha}`);
|
128 |
}
|
129 |
} catch (error) {
|
130 |
+
console.log(`No existing file found: ${error.message}`);
|
131 |
}
|
132 |
|
133 |
const content = Buffer.from(JSON.stringify(data, null, 2)).toString('base64');
|
134 |
|
135 |
const payload = {
|
136 |
+
message: `${sha ? 'Update' : 'Create'} ${fileName} for user ${userId}`,
|
137 |
content: content,
|
138 |
branch: 'main'
|
139 |
};
|
|
|
142 |
payload.sha = sha;
|
143 |
}
|
144 |
|
145 |
+
try {
|
146 |
+
console.log(`Saving to GitHub: ${this.apiUrl}/repos/${owner}/${repo}/contents/${path}`);
|
147 |
+
|
148 |
+
const response = await axios.put(
|
149 |
+
`${this.apiUrl}/repos/${owner}/${repo}/contents/${path}`,
|
150 |
+
payload,
|
151 |
+
{
|
152 |
+
headers: {
|
153 |
+
'Authorization': `token ${this.token}`,
|
154 |
+
'Accept': 'application/vnd.github.v3+json'
|
155 |
+
}
|
156 |
}
|
157 |
+
);
|
|
|
158 |
|
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.message}`);
|
163 |
+
|
164 |
+
// 如果是404错误,可能是目录不存在,尝试创建
|
165 |
+
if (error.response?.status === 404) {
|
166 |
+
try {
|
167 |
+
console.log('Attempting to create directory structure...');
|
168 |
+
|
169 |
+
// 先创建用户目录的README
|
170 |
+
const readmePath = `users/${userId}/README.md`;
|
171 |
+
const readmeContent = Buffer.from(`# PPT Files for User ${userId}\n\nThis directory contains PPT files for user ${userId}.\n`).toString('base64');
|
172 |
+
|
173 |
+
await axios.put(
|
174 |
+
`${this.apiUrl}/repos/${owner}/${repo}/contents/${readmePath}`,
|
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 |
+
console.log('User directory created, retrying PPT save...');
|
189 |
+
|
190 |
+
// 重试保存PPT文件
|
191 |
+
const retryResponse = await axios.put(
|
192 |
+
`${this.apiUrl}/repos/${owner}/${repo}/contents/${path}`,
|
193 |
+
payload,
|
194 |
+
{
|
195 |
+
headers: {
|
196 |
+
'Authorization': `token ${this.token}`,
|
197 |
+
'Accept': 'application/vnd.github.v3+json'
|
198 |
+
}
|
199 |
+
}
|
200 |
+
);
|
201 |
+
|
202 |
+
console.log(`Successfully saved to GitHub after retry: ${retryResponse.data.commit.sha}`);
|
203 |
+
return retryResponse.data;
|
204 |
+
|
205 |
+
} catch (retryError) {
|
206 |
+
console.error(`Retry also failed: ${retryError.message}`);
|
207 |
+
throw new Error(`Failed to save to GitHub: ${retryError.message}`);
|
208 |
+
}
|
209 |
+
} else {
|
210 |
+
throw new Error(`GitHub API error: ${error.response?.data?.message || error.message}`);
|
211 |
+
}
|
212 |
+
}
|
213 |
}
|
214 |
|
215 |
// 获取用户的所有PPT列表
|