mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 04:44:21 +01:00
Add ZIP download feature for repositories
- Add archive service to create ZIP files excluding gitignored files - Add download endpoint to backend repo routes - Add download button to repo cards with loading state - Update README with new ZIP download feature
This commit is contained in:
1 parent
725f782079
commit
af9f645f30
7 files changed
+723
-10
No files matched your search
@@ -3,11 +3,11 @@ import type { Database } from 'bun:sqlite'
|
||||
import * as db from '../db/queries'
|
||||
import * as repoService from '../services/repo'
|
||||
import * as gitOperations from '../services/git-operations'
|
||||
import * as archiveService from '../services/archive'
|
||||
import { SettingsService } from '../services/settings'
|
||||
import { writeFileContent } from '../services/file-operations'
|
||||
import { opencodeServerManager } from '../services/opencode-single-server'
|
||||
import { logger } from '../utils/logger'
|
||||
import { withTransactionAsync } from '../db/transactions'
|
||||
import { getOpenCodeConfigFilePath, getReposPath } from '@opencode-manager/shared'
|
||||
import path from 'path'
|
||||
|
||||
@@ -256,6 +256,44 @@ export function createRepoRoutes(database: Database) {
|
||||
return c.json({ error: error.message }, 500)
|
||||
}
|
||||
})
|
||||
|
||||
app.get('/:id/download', async (c) => {
|
||||
try {
|
||||
const id = parseInt(c.req.param('id'))
|
||||
const repo = db.getRepoById(database, id)
|
||||
|
||||
if (!repo) {
|
||||
return c.json({ error: 'Repo not found' }, 404)
|
||||
}
|
||||
|
||||
const repoPath = path.resolve(getReposPath(), repo.localPath)
|
||||
const repoName = path.basename(repo.localPath)
|
||||
|
||||
logger.info(`Starting archive creation for repo ${id}: ${repoPath}`)
|
||||
const archivePath = await archiveService.createRepoArchive(repoPath)
|
||||
const archiveSize = await archiveService.getArchiveSize(archivePath)
|
||||
const archiveStream = archiveService.getArchiveStream(archivePath)
|
||||
|
||||
archiveStream.on('end', () => {
|
||||
archiveService.deleteArchive(archivePath)
|
||||
})
|
||||
|
||||
archiveStream.on('error', () => {
|
||||
archiveService.deleteArchive(archivePath)
|
||||
})
|
||||
|
||||
return new Response(archiveStream as unknown as ReadableStream, {
|
||||
headers: {
|
||||
'Content-Type': 'application/zip',
|
||||
'Content-Disposition': `attachment; filename="${repoName}.zip"`,
|
||||
'Content-Length': archiveSize.toString(),
|
||||
}
|
||||
})
|
||||
} catch (error: any) {
|
||||
logger.error('Failed to create repo archive:', error)
|
||||
return c.json({ error: error.message }, 500)
|
||||
}
|
||||
})
|
||||
|
||||
return app
|
||||
}
|
||||
@@ -0,0 +1,172 @@
|
||||
import archiver from 'archiver'
|
||||
import { createWriteStream, createReadStream } from 'fs'
|
||||
import { readdir, stat, unlink } from 'fs/promises'
|
||||
import path from 'path'
|
||||
import os from 'os'
|
||||
import { logger } from '../utils/logger'
|
||||
|
||||
async function getIgnoredPaths(repoPath: string, paths: string[]): Promise<Set<string>> {
|
||||
if (paths.length === 0) return new Set()
|
||||
|
||||
try {
|
||||
const { spawn } = await import('child_process')
|
||||
|
||||
return new Promise((resolve) => {
|
||||
const ignored = new Set<string>()
|
||||
const proc = spawn('git', ['check-ignore', '--stdin'], {
|
||||
cwd: repoPath,
|
||||
shell: false
|
||||
})
|
||||
|
||||
let stdout = ''
|
||||
|
||||
proc.stdout?.on('data', (data: Buffer) => {
|
||||
stdout += data.toString()
|
||||
})
|
||||
|
||||
proc.stdin?.write(paths.join('\n'))
|
||||
proc.stdin?.end()
|
||||
|
||||
proc.on('close', () => {
|
||||
const ignoredPaths = stdout.split('\n').filter(p => p.trim())
|
||||
for (const p of ignoredPaths) {
|
||||
ignored.add(p)
|
||||
}
|
||||
resolve(ignored)
|
||||
})
|
||||
|
||||
proc.on('error', () => {
|
||||
resolve(new Set())
|
||||
})
|
||||
})
|
||||
} catch {
|
||||
return new Set()
|
||||
}
|
||||
}
|
||||
|
||||
async function collectFiles(
|
||||
repoPath: string,
|
||||
relativePath: string = ''
|
||||
): Promise<string[]> {
|
||||
const fullPath = path.join(repoPath, relativePath)
|
||||
const entries = await readdir(fullPath, { withFileTypes: true })
|
||||
const files: string[] = []
|
||||
|
||||
for (const entry of entries) {
|
||||
const entryRelPath = relativePath ? path.join(relativePath, entry.name) : entry.name
|
||||
|
||||
if (entry.name === '.git') continue
|
||||
|
||||
if (entry.isDirectory()) {
|
||||
files.push(entryRelPath + '/')
|
||||
const subFiles = await collectFiles(repoPath, entryRelPath)
|
||||
files.push(...subFiles)
|
||||
} else {
|
||||
files.push(entryRelPath)
|
||||
}
|
||||
}
|
||||
|
||||
return files
|
||||
}
|
||||
|
||||
async function filterIgnoredPaths(repoPath: string, allPaths: string[]): Promise<string[]> {
|
||||
const batchSize = 1000
|
||||
const ignoredSet = new Set<string>()
|
||||
|
||||
for (let i = 0; i < allPaths.length; i += batchSize) {
|
||||
const batch = allPaths.slice(i, i + batchSize)
|
||||
const ignored = await getIgnoredPaths(repoPath, batch)
|
||||
for (const p of ignored) {
|
||||
ignoredSet.add(p)
|
||||
if (p.endsWith('/')) {
|
||||
ignoredSet.add(p.slice(0, -1))
|
||||
} else {
|
||||
ignoredSet.add(p + '/')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const filteredPaths: string[] = []
|
||||
const ignoredDirs = new Set<string>()
|
||||
|
||||
for (const p of allPaths) {
|
||||
const isDir = p.endsWith('/')
|
||||
const cleanPath = isDir ? p.slice(0, -1) : p
|
||||
|
||||
let isUnderIgnoredDir = false
|
||||
for (const ignoredDir of ignoredDirs) {
|
||||
if (cleanPath.startsWith(ignoredDir + '/')) {
|
||||
isUnderIgnoredDir = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if (isUnderIgnoredDir) continue
|
||||
|
||||
if (ignoredSet.has(p) || ignoredSet.has(cleanPath)) {
|
||||
if (isDir) {
|
||||
ignoredDirs.add(cleanPath)
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
filteredPaths.push(p)
|
||||
}
|
||||
|
||||
return filteredPaths
|
||||
}
|
||||
|
||||
export async function createRepoArchive(repoPath: string): Promise<string> {
|
||||
const repoName = path.basename(repoPath)
|
||||
const tempFile = path.join(os.tmpdir(), `${repoName}-${Date.now()}.zip`)
|
||||
|
||||
logger.info(`Creating archive for ${repoPath} at ${tempFile}`)
|
||||
|
||||
const allPaths = await collectFiles(repoPath)
|
||||
const filteredPaths = await filterIgnoredPaths(repoPath, allPaths)
|
||||
|
||||
const output = createWriteStream(tempFile)
|
||||
const archive = archiver('zip', { zlib: { level: 5 } })
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
output.on('close', () => {
|
||||
logger.info(`Archive created: ${tempFile} (${archive.pointer()} bytes)`)
|
||||
resolve(tempFile)
|
||||
})
|
||||
|
||||
archive.on('error', (err) => {
|
||||
logger.error('Archive error:', err)
|
||||
reject(err)
|
||||
})
|
||||
|
||||
archive.pipe(output)
|
||||
|
||||
for (const relativePath of filteredPaths) {
|
||||
if (relativePath.endsWith('/')) continue
|
||||
|
||||
const fullPath = path.join(repoPath, relativePath)
|
||||
const archivePath = path.join(repoName, relativePath)
|
||||
archive.file(fullPath, { name: archivePath })
|
||||
}
|
||||
|
||||
archive.finalize()
|
||||
})
|
||||
}
|
||||
|
||||
export async function deleteArchive(filePath: string): Promise<void> {
|
||||
try {
|
||||
await unlink(filePath)
|
||||
logger.info(`Deleted temp archive: ${filePath}`)
|
||||
} catch (error) {
|
||||
logger.warn(`Failed to delete temp archive: ${filePath}`, error)
|
||||
}
|
||||
}
|
||||
|
||||
export function getArchiveStream(filePath: string) {
|
||||
return createReadStream(filePath)
|
||||
}
|
||||
|
||||
export async function getArchiveSize(filePath: string): Promise<number> {
|
||||
const stats = await stat(filePath)
|
||||
return stats.size
|
||||
}
|
||||
Reference in new issue
Block a user