mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +01:00
Critical and high: - repos: unlinking a local repo removes only the Manager reference and alias, never the repository files; confirm button reads "Unlink" - auth: rate-limit by the socket address; trust proxy headers only when AUTH_TRUSTED_PROXY_HEADERS is set (documented and forwarded in compose) - composer: per-session in-memory drafts (text, bash mode, attachments); Escape no longer clears the prompt - url params: compose same-tick updates so schedule dialogs stay closed after create/delete; delete dialog names the job Medium: - tool rows show failures (non-zero shell exit, failed exploration members) - voice conversation shows the specific error and a starting state - settings: Escape in dropdowns and the shortcut recorder no longer closes the dialog; mobile deep links open the requested section; one content tree per platform - closing pushed sheets, settings, tool panels and schedule dialogs goes back in history - login returns to the requested page; signed-out pages make no authenticated requests - action terminals keep their output (Manager-side output capture and replay) - unpushed-commit detection, commit message kept on failure - binary files with unknown extensions are detected and not editable - tool panel resizes when docked and floats as a dismissible sheet when the page is too narrow - Docker image runs tini as PID 1 Low: not-found states, add-repo and branch validation errors (4xx), search-aware empty states, mobile repo sort/filter/select cues, sidebar resize affordance and collapse shortcut, mobile unread badge, preview port filtering, terminal close handling, walkthrough empty state and helper session filtering, placeholder contrast, permission labels and focus, TTS task-list sanitizing, git identity email validation, quieter git logging, hermetic worktree tests, and accessible names for icon-only controls.
446 lines
13 KiB
TypeScript
446 lines
13 KiB
TypeScript
import fs from 'fs/promises'
|
|
import path from 'path'
|
|
import { createReadStream } from 'fs'
|
|
import { createInterface } from 'readline'
|
|
import { logger } from '../utils/logger'
|
|
import { mkdirSafe } from '../utils/fs-safe'
|
|
|
|
import {
|
|
readFileContent,
|
|
readFileAsBase64,
|
|
writeFileContent,
|
|
fileExists,
|
|
deletePath,
|
|
getFileStats,
|
|
listDirectory
|
|
} from './file-operations'
|
|
import { getReposPath, getWorkspacePath, FILE_LIMITS } from '@opencode-manager/shared/config/env'
|
|
import { ALLOWED_MIME_TYPES, type AllowedMimeType } from '@opencode-manager/shared'
|
|
import type { ChunkedFileInfo, PatchOperation } from '@opencode-manager/shared'
|
|
|
|
const SHARED_WORKSPACE_BASE = getReposPath()
|
|
const WORKSPACE_BASE = getWorkspacePath()
|
|
|
|
interface FileInfo {
|
|
name: string
|
|
path: string
|
|
isDirectory: boolean
|
|
size: number
|
|
mimeType?: string
|
|
content?: string
|
|
children?: FileInfo[]
|
|
lastModified: Date
|
|
workspaceRoot?: string
|
|
}
|
|
|
|
interface FileUploadResult {
|
|
name: string
|
|
path: string
|
|
size: number
|
|
mimeType: string
|
|
}
|
|
|
|
const BINARY_MIME_TYPE = 'application/octet-stream'
|
|
const SNIFF_BYTE_COUNT = 8192
|
|
const MAX_INVALID_UTF8_RATIO = 0.1
|
|
|
|
type DetectedMimeType = AllowedMimeType | typeof BINARY_MIME_TYPE
|
|
|
|
export interface RawFileMetadata {
|
|
size: number
|
|
mimeType: DetectedMimeType
|
|
lastModified: Date
|
|
}
|
|
|
|
export async function getRawFileContent(userPath: string): Promise<Buffer> {
|
|
const validatedPath = validatePath(userPath)
|
|
logger.info(`Getting raw file content for path: ${userPath} -> ${validatedPath}`)
|
|
|
|
try {
|
|
const exists = await fileExists(validatedPath)
|
|
if (!exists) {
|
|
throw new Error('File does not exist')
|
|
}
|
|
|
|
const stats = await getFileStats(validatedPath)
|
|
if (stats.isDirectory) {
|
|
throw new Error('Path is a directory')
|
|
}
|
|
|
|
return await fs.readFile(validatedPath)
|
|
} catch (error) {
|
|
logger.error(`Failed to read raw file content ${validatedPath}:`, error)
|
|
throw { message: 'File not found or cannot be read', statusCode: 404 }
|
|
}
|
|
}
|
|
|
|
export async function getRawFileMetadata(userPath: string): Promise<RawFileMetadata | null> {
|
|
const validatedPath = validatePath(userPath)
|
|
logger.info(`Getting raw file metadata for path: ${userPath} -> ${validatedPath}`)
|
|
|
|
try {
|
|
const exists = await fileExists(validatedPath)
|
|
if (!exists) {
|
|
throw new Error('Path does not exist')
|
|
}
|
|
|
|
const stats = await getFileStats(validatedPath)
|
|
if (stats.isDirectory) {
|
|
return null
|
|
}
|
|
|
|
return {
|
|
size: stats.size,
|
|
mimeType: await detectMimeType(validatedPath),
|
|
lastModified: stats.lastModified,
|
|
}
|
|
} catch (error) {
|
|
logger.error(`Failed to read raw file metadata ${validatedPath}:`, error)
|
|
throw { message: 'File or directory not found', statusCode: 404 }
|
|
}
|
|
}
|
|
|
|
export async function getFile(userPath: string): Promise<FileInfo> {
|
|
const validatedPath = validatePath(userPath)
|
|
const responsePath = path.isAbsolute(userPath.trim()) ? path.relative(SHARED_WORKSPACE_BASE, validatedPath) : userPath
|
|
logger.info(`Getting file for path: ${userPath} -> ${validatedPath}`)
|
|
|
|
try {
|
|
// Check if path exists
|
|
const exists = await fileExists(validatedPath)
|
|
if (!exists) {
|
|
throw new Error('Path does not exist')
|
|
}
|
|
|
|
// Get file stats
|
|
const stats = await getFileStats(validatedPath)
|
|
|
|
if (stats.isDirectory) {
|
|
// It's a directory - list contents
|
|
const entries = await listDirectory(validatedPath)
|
|
const children: FileInfo[] = []
|
|
|
|
for (const entry of entries) {
|
|
children.push({
|
|
name: entry.name,
|
|
path: path.join(responsePath, entry.name),
|
|
isDirectory: entry.isDirectory,
|
|
size: entry.size,
|
|
lastModified: entry.lastModified,
|
|
})
|
|
}
|
|
|
|
return {
|
|
name: path.basename(validatedPath),
|
|
path: responsePath,
|
|
isDirectory: true,
|
|
size: 0,
|
|
children: children.sort((a, b) => {
|
|
if (a.isDirectory !== b.isDirectory) {
|
|
return a.isDirectory ? -1 : 1
|
|
}
|
|
return a.name.localeCompare(b.name)
|
|
}),
|
|
lastModified: stats.lastModified,
|
|
workspaceRoot: SHARED_WORKSPACE_BASE,
|
|
}
|
|
} else {
|
|
// It's a file - get content
|
|
let content = ''
|
|
const mimeType = await detectMimeType(validatedPath)
|
|
|
|
if (stats.size < FILE_LIMITS.MAX_SIZE_BYTES) {
|
|
try {
|
|
if (mimeType.startsWith('image/') || !mimeType.startsWith('text/')) {
|
|
content = await readFileAsBase64(validatedPath)
|
|
} else {
|
|
const fileOutput = await readFileContent(validatedPath)
|
|
content = Buffer.from(fileOutput, 'utf8').toString('base64')
|
|
}
|
|
} catch (error) {
|
|
logger.warn(`Failed to read file content: ${error}`)
|
|
}
|
|
}
|
|
|
|
return {
|
|
name: path.basename(validatedPath),
|
|
path: responsePath,
|
|
isDirectory: false,
|
|
size: stats.size,
|
|
mimeType,
|
|
content,
|
|
lastModified: stats.lastModified,
|
|
}
|
|
}
|
|
} catch (error) {
|
|
logger.error(`Failed to access path ${validatedPath}:`, error)
|
|
throw { message: 'File or directory not found', statusCode: 404 }
|
|
}
|
|
}
|
|
|
|
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')
|
|
}
|
|
|
|
const mimeType = (file.type || getKnownMimeType(file.name) || 'text/plain') as AllowedMimeType
|
|
if (!ALLOWED_MIME_TYPES.includes(mimeType) && !mimeType.startsWith('text/')) {
|
|
throw new Error('File type not allowed')
|
|
}
|
|
|
|
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 mkdirSafe(parentDir)
|
|
|
|
const buffer = await file.arrayBuffer()
|
|
|
|
await writeFileContent(fullPath, Buffer.from(buffer))
|
|
|
|
return {
|
|
name: path.basename(targetRelativePath),
|
|
path: path.join(userPath, targetRelativePath),
|
|
size: file.size,
|
|
mimeType,
|
|
}
|
|
}
|
|
|
|
export async function createFileOrFolder(userPath: string, body: { type: 'file' | 'folder', content?: string }): Promise<FileInfo> {
|
|
const validatedPath = validatePath(userPath)
|
|
|
|
if (body.type === 'folder') {
|
|
await mkdirSafe(validatedPath)
|
|
return {
|
|
name: path.basename(validatedPath),
|
|
path: userPath,
|
|
isDirectory: true,
|
|
size: 0,
|
|
lastModified: new Date(),
|
|
}
|
|
} else {
|
|
const content = body.content || ''
|
|
|
|
if (content) {
|
|
await writeFileContent(validatedPath, content)
|
|
} else {
|
|
await fs.writeFile(validatedPath, '')
|
|
}
|
|
|
|
return {
|
|
name: path.basename(validatedPath),
|
|
path: userPath,
|
|
isDirectory: false,
|
|
size: content.length,
|
|
lastModified: new Date(),
|
|
}
|
|
}
|
|
}
|
|
|
|
export async function deleteFileOrFolder(userPath: string): Promise<void> {
|
|
const validatedPath = validatePath(userPath)
|
|
|
|
await deletePath(validatedPath)
|
|
}
|
|
|
|
export async function renameOrMoveFile(userPath: string, body: { newPath: string }): Promise<FileInfo> {
|
|
const oldValidatedPath = validatePath(userPath)
|
|
const newValidatedPath = validatePath(body.newPath)
|
|
|
|
// Create parent directory if needed
|
|
await mkdirSafe(path.dirname(newValidatedPath))
|
|
|
|
// Move/rename file
|
|
await fs.rename(oldValidatedPath, newValidatedPath)
|
|
|
|
// Get stats of new file
|
|
const stats = await getFileStats(newValidatedPath)
|
|
|
|
return {
|
|
name: path.basename(newValidatedPath),
|
|
path: body.newPath,
|
|
isDirectory: stats.isDirectory,
|
|
size: stats.size,
|
|
lastModified: stats.lastModified,
|
|
}
|
|
}
|
|
|
|
function isWithinBase(resolved: string, basePath: string): boolean {
|
|
return resolved === basePath || resolved.startsWith(`${basePath}${path.sep}`)
|
|
}
|
|
|
|
function validatePath(userPath: string): string {
|
|
const trimmed = userPath.trim()
|
|
const normalized = path.normalize(trimmed || '.')
|
|
const basePath = path.resolve(WORKSPACE_BASE)
|
|
|
|
if (path.isAbsolute(normalized) && isWithinBase(path.resolve(normalized), basePath)) {
|
|
return path.resolve(normalized)
|
|
}
|
|
|
|
const resolved = path.resolve(path.join(SHARED_WORKSPACE_BASE, normalized))
|
|
if (!isWithinBase(resolved, basePath)) {
|
|
throw { message: 'Path traversal detected', statusCode: 403 }
|
|
}
|
|
|
|
return resolved
|
|
}
|
|
|
|
function getKnownMimeType(filePath: string): AllowedMimeType | undefined {
|
|
const ext = path.extname(filePath).toLowerCase()
|
|
|
|
const mimeTypes: Record<string, AllowedMimeType> = {
|
|
'.txt': 'text/plain',
|
|
'.html': 'text/html',
|
|
'.htm': 'text/html',
|
|
'.css': 'text/css',
|
|
'.js': 'text/javascript',
|
|
'.ts': 'text/typescript',
|
|
'.jsx': 'text/javascript',
|
|
'.tsx': 'text/typescript',
|
|
'.json': 'application/json',
|
|
'.jsonc': 'application/json',
|
|
'.xml': 'application/xml',
|
|
'.png': 'image/png',
|
|
'.jpg': 'image/jpeg',
|
|
'.jpeg': 'image/jpeg',
|
|
'.gif': 'image/gif',
|
|
'.webp': 'image/webp',
|
|
'.svg': 'image/svg+xml',
|
|
'.pdf': 'application/pdf',
|
|
'.zip': 'application/zip',
|
|
'.md': 'text/markdown',
|
|
'.mdx': 'text/markdown',
|
|
}
|
|
|
|
return mimeTypes[ext]
|
|
}
|
|
|
|
async function detectMimeType(filePath: string): Promise<DetectedMimeType> {
|
|
const known = getKnownMimeType(filePath)
|
|
if (known) return known
|
|
return isBinaryContent(await readFilePrefix(filePath)) ? BINARY_MIME_TYPE : 'text/plain'
|
|
}
|
|
|
|
async function readFilePrefix(filePath: string): Promise<Buffer> {
|
|
const handle = await fs.open(filePath, 'r')
|
|
try {
|
|
const buffer = Buffer.alloc(SNIFF_BYTE_COUNT)
|
|
const { bytesRead } = await handle.read(buffer, 0, SNIFF_BYTE_COUNT, 0)
|
|
return buffer.subarray(0, bytesRead)
|
|
} finally {
|
|
await handle.close()
|
|
}
|
|
}
|
|
|
|
function isBinaryContent(buffer: Buffer): boolean {
|
|
if (buffer.length === 0) return false
|
|
if (buffer.includes(0)) return true
|
|
const decoded = buffer.toString('utf8')
|
|
const invalidCharacters = decoded.split('\uFFFD').length - 1
|
|
return invalidCharacters / decoded.length > MAX_INVALID_UTF8_RATIO
|
|
}
|
|
|
|
async function readFileLinesAndCount(
|
|
filePath: string,
|
|
startLine: number,
|
|
endLine: number
|
|
): Promise<{ lines: string[]; totalLines: number }> {
|
|
return new Promise((resolve, reject) => {
|
|
const lines: string[] = []
|
|
let currentLine = 0
|
|
const stream = createReadStream(filePath, { encoding: 'utf8' })
|
|
const rl = createInterface({ input: stream, crlfDelay: Infinity })
|
|
|
|
rl.on('line', (line) => {
|
|
if (currentLine >= startLine && currentLine < endLine) {
|
|
lines.push(line)
|
|
}
|
|
currentLine++
|
|
})
|
|
rl.on('close', () => resolve({ lines, totalLines: currentLine }))
|
|
rl.on('error', reject)
|
|
})
|
|
}
|
|
|
|
export async function getFileRange(userPath: string, startLine: number, endLine: number): Promise<ChunkedFileInfo> {
|
|
const validatedPath = validatePath(userPath)
|
|
logger.info(`Getting file range for path: ${userPath} lines ${startLine}-${endLine}`)
|
|
|
|
const exists = await fileExists(validatedPath)
|
|
if (!exists) {
|
|
throw { message: 'File does not exist', statusCode: 404 }
|
|
}
|
|
|
|
const stats = await getFileStats(validatedPath)
|
|
if (stats.isDirectory) {
|
|
throw { message: 'Path is a directory', statusCode: 400 }
|
|
}
|
|
|
|
const { lines, totalLines } = await readFileLinesAndCount(validatedPath, startLine, endLine)
|
|
const clampedEnd = Math.min(endLine, totalLines)
|
|
const mimeType = await detectMimeType(validatedPath)
|
|
|
|
return {
|
|
name: path.basename(validatedPath),
|
|
path: userPath,
|
|
isDirectory: false as const,
|
|
size: stats.size,
|
|
mimeType,
|
|
lines,
|
|
totalLines,
|
|
startLine,
|
|
endLine: clampedEnd,
|
|
hasMore: clampedEnd < totalLines,
|
|
lastModified: stats.lastModified,
|
|
}
|
|
}
|
|
|
|
export async function applyFilePatches(userPath: string, patches: PatchOperation[]): Promise<{ success: boolean; totalLines: number }> {
|
|
const validatedPath = validatePath(userPath)
|
|
logger.info(`Applying ${patches.length} patches to: ${userPath}`)
|
|
|
|
const exists = await fileExists(validatedPath)
|
|
if (!exists) {
|
|
throw { message: 'File does not exist', statusCode: 404 }
|
|
}
|
|
|
|
const content = await fs.readFile(validatedPath, 'utf8')
|
|
const lines = content.split('\n')
|
|
|
|
const sortedPatches = [...patches].sort((a, b) => b.startLine - a.startLine)
|
|
|
|
for (const patch of sortedPatches) {
|
|
const { type, startLine, endLine, content: patchContent } = patch
|
|
|
|
switch (type) {
|
|
case 'replace': {
|
|
const end = endLine ?? startLine + 1
|
|
const newLines = patchContent?.split('\n') ?? []
|
|
lines.splice(startLine, end - startLine, ...newLines)
|
|
break
|
|
}
|
|
case 'insert': {
|
|
const newLines = patchContent?.split('\n') ?? []
|
|
lines.splice(startLine, 0, ...newLines)
|
|
break
|
|
}
|
|
case 'delete': {
|
|
const end = endLine ?? startLine + 1
|
|
lines.splice(startLine, end - startLine)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
await fs.writeFile(validatedPath, lines.join('\n'), 'utf8')
|
|
|
|
return { success: true, totalLines: lines.length }
|
|
}
|