102 lines
3.0 KiB
TypeScript
102 lines
3.0 KiB
TypeScript
// Gitea API integration for XFlow canvas sync
|
|
// Uses Gitea REST API to store mind maps in MMAVTEX repo
|
|
|
|
interface GiteaFile {
|
|
content: string // base64
|
|
sha?: string
|
|
}
|
|
|
|
const getConfig = () => {
|
|
const config = useRuntimeConfig()
|
|
return {
|
|
url: config.giteaUrl,
|
|
token: config.giteaToken,
|
|
org: config.giteaOrg,
|
|
mindmapRepo: config.giteaMindmapRepo,
|
|
projectRepo: config.giteaProjectRepo,
|
|
}
|
|
}
|
|
|
|
const headers = (token: string) => ({
|
|
'Authorization': `token ${token}`,
|
|
'Content-Type': 'application/json',
|
|
'Accept': 'application/json'
|
|
})
|
|
|
|
// Get file from Gitea (returns null if not found)
|
|
export async function giteaGetFile(path: string, repo?: string): Promise<GiteaFile | null> {
|
|
const { url, token, org, mindmapRepo } = getConfig()
|
|
const repoName = repo || mindmapRepo
|
|
try {
|
|
const res = await $fetch<any>(`${url}/api/v1/repos/${org}/${repoName}/contents/${path}`, {
|
|
headers: headers(token)
|
|
})
|
|
return { content: res.content, sha: res.sha }
|
|
} catch {
|
|
return null
|
|
}
|
|
}
|
|
|
|
// Create or update file in Gitea
|
|
export async function giteaPutFile(path: string, content: string, message: string, sha?: string, repo?: string): Promise<boolean> {
|
|
const { url, token, org, mindmapRepo } = getConfig()
|
|
const repoName = repo || mindmapRepo
|
|
const encoded = Buffer.from(content).toString('base64')
|
|
|
|
try {
|
|
await $fetch(`${url}/api/v1/repos/${org}/${repoName}/contents/${path}`, {
|
|
method: sha ? 'PUT' : 'POST',
|
|
headers: headers(token),
|
|
body: {
|
|
message,
|
|
content: encoded,
|
|
...(sha ? { sha } : {})
|
|
}
|
|
})
|
|
return true
|
|
} catch (e) {
|
|
console.error('Gitea putFile error:', e)
|
|
return false
|
|
}
|
|
}
|
|
|
|
// Save canvas to Gitea MMAVTEX repo
|
|
export async function saveCanvasToGitea(canvasId: string, canvasData: any, commitMessage?: string): Promise<boolean> {
|
|
const path = `canvases/${canvasId}.canvas`
|
|
const content = JSON.stringify(canvasData, null, 2)
|
|
const message = commitMessage || `Update canvas ${canvasId} at ${new Date().toLocaleString('ru-RU')}`
|
|
|
|
// Get existing file SHA if exists (required for update)
|
|
const existing = await giteaGetFile(path)
|
|
return giteaPutFile(path, content, message, existing?.sha)
|
|
}
|
|
|
|
// Load canvas from Gitea
|
|
export async function loadCanvasFromGitea(canvasId: string): Promise<any | null> {
|
|
const path = `canvases/${canvasId}.canvas`
|
|
const file = await giteaGetFile(path)
|
|
if (!file) return null
|
|
|
|
try {
|
|
const decoded = Buffer.from(file.content, 'base64').toString('utf-8')
|
|
return JSON.parse(decoded)
|
|
} catch {
|
|
return null
|
|
}
|
|
}
|
|
|
|
// List all canvases in Gitea
|
|
export async function listCanvasesFromGitea(): Promise<string[]> {
|
|
const { url, token, org, mindmapRepo } = getConfig()
|
|
try {
|
|
const res = await $fetch<any[]>(`${url}/api/v1/repos/${org}/${mindmapRepo}/contents/canvases`, {
|
|
headers: headers(token)
|
|
})
|
|
return res
|
|
.filter((f: any) => f.name.endsWith('.canvas'))
|
|
.map((f: any) => f.name.replace('.canvas', ''))
|
|
} catch {
|
|
return []
|
|
}
|
|
}
|