69 lines
2.0 KiB
TypeScript
69 lines
2.0 KiB
TypeScript
import { APPS_DIR, allApps } from '$apps'
|
|
import { generateManifest } from '../sync'
|
|
import { existsSync, mkdirSync, readFileSync, unlinkSync, writeFileSync } from 'fs'
|
|
import { dirname, join } from 'path'
|
|
import { Hype } from '@because/hype'
|
|
|
|
const router = Hype.router()
|
|
|
|
router.get('/apps', c => c.json(allApps().map(a => a.name)))
|
|
|
|
router.get('/apps/:app/manifest', c => {
|
|
const appName = c.req.param('app')
|
|
if (!appName) return c.json({ error: 'App not found' }, 404)
|
|
|
|
const appPath = join(APPS_DIR, appName)
|
|
if (!existsSync(appPath)) return c.json({ error: 'App not found' }, 404)
|
|
|
|
const manifest = generateManifest(appPath, appName)
|
|
return c.json(manifest)
|
|
})
|
|
|
|
router.get('/apps/:app/files/:path{.+}', c => {
|
|
const appName = c.req.param('app')
|
|
const filePath = c.req.param('path')
|
|
|
|
if (!appName || !filePath) return c.json({ error: 'Invalid path' }, 400)
|
|
|
|
const fullPath = join(APPS_DIR, appName, filePath)
|
|
if (!existsSync(fullPath)) return c.json({ error: 'File not found' }, 404)
|
|
|
|
const content = readFileSync(fullPath)
|
|
return new Response(content)
|
|
})
|
|
|
|
router.put('/apps/:app/files/:path{.+}', async c => {
|
|
const appName = c.req.param('app')
|
|
const filePath = c.req.param('path')
|
|
|
|
if (!appName || !filePath) return c.json({ error: 'Invalid path' }, 400)
|
|
|
|
const fullPath = join(APPS_DIR, appName, filePath)
|
|
const dir = dirname(fullPath)
|
|
|
|
// Ensure directory exists
|
|
if (!existsSync(dir)) {
|
|
mkdirSync(dir, { recursive: true })
|
|
}
|
|
|
|
const body = await c.req.arrayBuffer()
|
|
writeFileSync(fullPath, new Uint8Array(body))
|
|
|
|
return c.json({ ok: true })
|
|
})
|
|
|
|
router.delete('/apps/:app/files/:path{.+}', c => {
|
|
const appName = c.req.param('app')
|
|
const filePath = c.req.param('path')
|
|
|
|
if (!appName || !filePath) return c.json({ error: 'Invalid path' }, 400)
|
|
|
|
const fullPath = join(APPS_DIR, appName, filePath)
|
|
if (!existsSync(fullPath)) return c.json({ error: 'File not found' }, 404)
|
|
|
|
unlinkSync(fullPath)
|
|
return c.json({ ok: true })
|
|
})
|
|
|
|
export default router
|