477af66232
* feat(chat): polish syntax highlighting and tool payload rendering (#94) * [verified] feat(chat): polish syntax highlighting and tool payload rendering * [verified] fix(chat): tighten large tool payload rendering * docs: update data volume path in Docker docs Align documentation with docker-compose.yml change: hermes-web-ui-data -> hermes-web-ui, /app/dist/data -> /root/.hermes-web-ui Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * refactor: bundle server build and restructure service modules - Add build-server.mjs script for standalone server compilation - Add logger service with structured output - Restructure auth, gateway-manager, hermes-cli, hermes services - Update docker-compose volume mount path - Update tsconfig and entry point for bundled server Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * refactor: separate controllers from routes and centralize route registration - Extract business logic from route handlers into controllers/ - Add centralized route registry in routes/index.ts with public/auth/protected layers - Replace global auth whitelist with sequential middleware registration - Extract shared helpers to services/config-helpers.ts - Allow custom provider name to be user-editable in ProviderFormModal - Deduplicate custom providers by poolKey instead of base_url in getAvailable Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: auth bypass via path case, SPA serving, and provider improvements - Fix auth bypass: path case-insensitive check for /api, /v1, /upload - Fix SPA returning 401: skip auth for non-API paths (static files) - Fix profile switch: use local loading state instead of shared store ref - Auto-append /v1 to base_url when fetching models (frontend + backend) - Guard .env writing to built-in providers only - Add builtin field to provider presets, enable base_url input in form - Print auth token to console on startup (pino only writes to file) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Zhicheng Han <43314240+hanzckernel@users.noreply.github.com> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
63 lines
2.3 KiB
TypeScript
63 lines
2.3 KiB
TypeScript
import { randomBytes } from 'crypto'
|
|
import { writeFile } from 'fs/promises'
|
|
import { config } from '../config'
|
|
|
|
const MAX_UPLOAD_SIZE = 50 * 1024 * 1024 // 50MB
|
|
|
|
export async function handleUpload(ctx: any) {
|
|
const contentType = ctx.get('content-type') || ''
|
|
if (!contentType.startsWith('multipart/form-data')) {
|
|
ctx.status = 400; ctx.body = { error: 'Expected multipart/form-data' }; return
|
|
}
|
|
const boundary = '--' + contentType.split('boundary=')[1]
|
|
if (!boundary || boundary === '--undefined') {
|
|
ctx.status = 400; ctx.body = { error: 'Missing boundary' }; return
|
|
}
|
|
const chunks: Buffer[] = []
|
|
let totalSize = 0
|
|
for await (const chunk of ctx.req) {
|
|
totalSize += chunk.length
|
|
if (totalSize > MAX_UPLOAD_SIZE) {
|
|
ctx.status = 413; ctx.body = { error: `File too large (max ${MAX_UPLOAD_SIZE / 1024 / 1024}MB)` }; return
|
|
}
|
|
chunks.push(chunk)
|
|
}
|
|
const raw = Buffer.concat(chunks)
|
|
const boundaryBuf = Buffer.from(boundary)
|
|
const parts = splitMultipart(raw, boundaryBuf)
|
|
const results: { name: string; path: string }[] = []
|
|
for (const part of parts) {
|
|
const headerEnd = part.indexOf(Buffer.from('\r\n\r\n'))
|
|
if (headerEnd === -1) continue
|
|
const headerBuf = part.subarray(0, headerEnd)
|
|
const header = headerBuf.toString('utf-8')
|
|
const data = part.subarray(headerEnd + 4, part.length - 2)
|
|
let filename = ''
|
|
const filenameStarMatch = header.match(/filename\*=UTF-8''(.+)/i)
|
|
if (filenameStarMatch) { filename = decodeURIComponent(filenameStarMatch[1]) }
|
|
else {
|
|
const filenameMatch = header.match(/filename="([^"]+)"/)
|
|
if (!filenameMatch) continue
|
|
filename = filenameMatch[1]
|
|
}
|
|
const ext = filename.includes('.') ? '.' + filename.split('.').pop() : ''
|
|
const savedName = randomBytes(8).toString('hex') + ext
|
|
const savedPath = `${config.uploadDir}/${savedName}`
|
|
await writeFile(savedPath, data)
|
|
results.push({ name: filename, path: savedPath })
|
|
}
|
|
ctx.body = { files: results }
|
|
}
|
|
|
|
function splitMultipart(raw: Buffer, boundary: Buffer): Buffer[] {
|
|
const parts: Buffer[] = []
|
|
let start = 0
|
|
while (true) {
|
|
const idx = raw.indexOf(boundary, start)
|
|
if (idx === -1) break
|
|
if (start > 0) { parts.push(raw.subarray(start + 2, idx)) }
|
|
start = idx + boundary.length
|
|
}
|
|
return parts
|
|
}
|