|
import axios from 'axios';
|
|
import { GITHUB_CONFIG } from '../config/users.js';
|
|
import memoryStorageService from './memoryStorageService.js';
|
|
|
|
class GitHubService {
|
|
constructor() {
|
|
this.apiUrl = GITHUB_CONFIG.apiUrl;
|
|
this.token = GITHUB_CONFIG.token;
|
|
this.repositories = GITHUB_CONFIG.repositories;
|
|
this.useMemoryStorage = !this.token;
|
|
|
|
if (!this.token) {
|
|
console.warn('GitHub token not configured, using memory storage for development');
|
|
}
|
|
}
|
|
|
|
|
|
parseRepoUrl(repoUrl) {
|
|
const match = repoUrl.match(/github\.com\/([^\/]+)\/([^\/]+)/);
|
|
if (!match) throw new Error('Invalid GitHub repository URL');
|
|
return { owner: match[1], repo: match[2] };
|
|
}
|
|
|
|
|
|
async getFile(userId, fileName, repoIndex = 0) {
|
|
|
|
if (this.useMemoryStorage) {
|
|
return await memoryStorageService.getFile(userId, fileName);
|
|
}
|
|
|
|
|
|
try {
|
|
const repoUrl = this.repositories[repoIndex];
|
|
const { owner, repo } = this.parseRepoUrl(repoUrl);
|
|
const path = `users/${userId}/${fileName}`;
|
|
|
|
const response = await axios.get(
|
|
`${this.apiUrl}/repos/${owner}/${repo}/contents/${path}`,
|
|
{
|
|
headers: {
|
|
'Authorization': `token ${this.token}`,
|
|
'Accept': 'application/vnd.github.v3+json'
|
|
}
|
|
}
|
|
);
|
|
|
|
const content = Buffer.from(response.data.content, 'base64').toString('utf8');
|
|
return {
|
|
content: JSON.parse(content),
|
|
sha: response.data.sha
|
|
};
|
|
} catch (error) {
|
|
if (error.response?.status === 404) {
|
|
return null;
|
|
}
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
|
|
async saveFile(userId, fileName, data, repoIndex = 0) {
|
|
|
|
if (this.useMemoryStorage) {
|
|
return await memoryStorageService.saveFile(userId, fileName, data);
|
|
}
|
|
|
|
|
|
const repoUrl = this.repositories[repoIndex];
|
|
const { owner, repo } = this.parseRepoUrl(repoUrl);
|
|
const path = `users/${userId}/${fileName}`;
|
|
|
|
|
|
let sha = null;
|
|
try {
|
|
const existing = await this.getFile(userId, fileName, repoIndex);
|
|
if (existing) {
|
|
sha = existing.sha;
|
|
}
|
|
} catch (error) {
|
|
|
|
}
|
|
|
|
const content = Buffer.from(JSON.stringify(data, null, 2)).toString('base64');
|
|
|
|
const payload = {
|
|
message: `Update ${fileName} for user ${userId}`,
|
|
content: content,
|
|
branch: 'main'
|
|
};
|
|
|
|
if (sha) {
|
|
payload.sha = sha;
|
|
}
|
|
|
|
const response = await axios.put(
|
|
`${this.apiUrl}/repos/${owner}/${repo}/contents/${path}`,
|
|
payload,
|
|
{
|
|
headers: {
|
|
'Authorization': `token ${this.token}`,
|
|
'Accept': 'application/vnd.github.v3+json'
|
|
}
|
|
}
|
|
);
|
|
|
|
return response.data;
|
|
}
|
|
|
|
|
|
async getUserPPTList(userId) {
|
|
|
|
if (this.useMemoryStorage) {
|
|
return await memoryStorageService.getUserPPTList(userId);
|
|
}
|
|
|
|
|
|
const results = [];
|
|
|
|
for (let i = 0; i < this.repositories.length; i++) {
|
|
try {
|
|
const repoUrl = this.repositories[i];
|
|
const { owner, repo } = this.parseRepoUrl(repoUrl);
|
|
const path = `users/${userId}`;
|
|
|
|
const response = await axios.get(
|
|
`${this.apiUrl}/repos/${owner}/${repo}/contents/${path}`,
|
|
{
|
|
headers: {
|
|
'Authorization': `token ${this.token}`,
|
|
'Accept': 'application/vnd.github.v3+json'
|
|
}
|
|
}
|
|
);
|
|
|
|
const files = response.data
|
|
.filter(item => item.type === 'file' && item.name.endsWith('.json'));
|
|
|
|
|
|
for (const file of files) {
|
|
try {
|
|
const pptId = file.name.replace('.json', '');
|
|
const fileContent = await this.getFile(userId, file.name, i);
|
|
|
|
if (fileContent && fileContent.content) {
|
|
results.push({
|
|
name: pptId,
|
|
title: fileContent.content.title || '未命名演示文稿',
|
|
lastModified: fileContent.content.updatedAt || fileContent.content.createdAt,
|
|
repoIndex: i,
|
|
repoUrl: repoUrl
|
|
});
|
|
}
|
|
} catch (error) {
|
|
|
|
console.warn(`Failed to read PPT content for ${file.name}:`, error.message);
|
|
results.push({
|
|
name: file.name.replace('.json', ''),
|
|
title: file.name.replace('.json', ''),
|
|
lastModified: new Date().toISOString(),
|
|
repoIndex: i,
|
|
repoUrl: repoUrl
|
|
});
|
|
}
|
|
}
|
|
} catch (error) {
|
|
if (error.response?.status !== 404) {
|
|
console.error(`Error fetching files from repo ${i}:`, error.message);
|
|
}
|
|
}
|
|
}
|
|
|
|
return results;
|
|
}
|
|
|
|
|
|
async deleteFile(userId, fileName, repoIndex = 0) {
|
|
|
|
if (this.useMemoryStorage) {
|
|
return await memoryStorageService.deleteFile(userId, fileName);
|
|
}
|
|
|
|
|
|
const existing = await this.getFile(userId, fileName, repoIndex);
|
|
if (!existing) {
|
|
throw new Error('File not found');
|
|
}
|
|
|
|
const repoUrl = this.repositories[repoIndex];
|
|
const { owner, repo } = this.parseRepoUrl(repoUrl);
|
|
const path = `users/${userId}/${fileName}`;
|
|
|
|
const response = await axios.delete(
|
|
`${this.apiUrl}/repos/${owner}/${repo}/contents/${path}`,
|
|
{
|
|
data: {
|
|
message: `Delete ${fileName} for user ${userId}`,
|
|
sha: existing.sha,
|
|
branch: 'main'
|
|
},
|
|
headers: {
|
|
'Authorization': `token ${this.token}`,
|
|
'Accept': 'application/vnd.github.v3+json'
|
|
}
|
|
}
|
|
);
|
|
|
|
return response.data;
|
|
}
|
|
}
|
|
|
|
export default new GitHubService(); |