mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 04:44:21 +01:00
Add multi-file and directory upload support with progress tracking
This commit is contained in:
1 parent
a0581746de
commit
74e0997b3e
5 files changed
+310
-37
No files matched your search
@@ -66,7 +66,8 @@ export function createFileRoutes(_database: Database) {
|
||||
return c.json({ error: 'No file provided' }, 400)
|
||||
}
|
||||
|
||||
const result = await fileService.uploadFile(path, file)
|
||||
const relativePath = body.relativePath as string | undefined
|
||||
const result = await fileService.uploadFile(path, file, relativePath)
|
||||
return c.json(result)
|
||||
} catch (error: any) {
|
||||
logger.error('Failed to upload file:', error)
|
||||
|
||||
@@ -138,7 +138,7 @@ export async function getFile(userPath: string): Promise<FileInfo> {
|
||||
}
|
||||
}
|
||||
|
||||
export async function uploadFile(userPath: string, file: File): Promise<FileUploadResult> {
|
||||
export async function uploadFile(userPath: string, file: File, relativePath?: string): Promise<FileUploadResult> {
|
||||
if (file.size > FILE_LIMITS.MAX_UPLOAD_SIZE_BYTES) {
|
||||
throw new Error('File too large')
|
||||
}
|
||||
@@ -148,17 +148,25 @@ export async function uploadFile(userPath: string, file: File): Promise<FileUplo
|
||||
throw new Error('File type not allowed')
|
||||
}
|
||||
|
||||
const validatedPath = validatePath(userPath)
|
||||
const fileName = file.name || path.basename(userPath)
|
||||
const fullPath = path.join(validatedPath, fileName)
|
||||
const validatedBasePath = validatePath(userPath)
|
||||
const targetRelativePath = relativePath || file.name || path.basename(userPath)
|
||||
const fullPath = path.join(validatedBasePath, targetRelativePath)
|
||||
|
||||
const finalValidatedPath = validatePath(path.join(userPath, targetRelativePath))
|
||||
if (finalValidatedPath !== fullPath) {
|
||||
throw { message: 'Invalid relative path', statusCode: 400 }
|
||||
}
|
||||
|
||||
const parentDir = path.dirname(fullPath)
|
||||
await fs.mkdir(parentDir, { recursive: true })
|
||||
|
||||
const buffer = await file.arrayBuffer()
|
||||
|
||||
await writeFileContent(fullPath, Buffer.from(buffer))
|
||||
|
||||
return {
|
||||
name: fileName,
|
||||
path: path.join(userPath, fileName),
|
||||
name: path.basename(targetRelativePath),
|
||||
path: path.join(userPath, targetRelativePath),
|
||||
size: file.size,
|
||||
mimeType,
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user