mirror of
https://github.com/hanzoai/runtime.git
synced 2026-10-30 18:23:13 +00:00
- Add main Dockerfile for production builds - Add Dockerfile.runtime for secure code execution - Add Dockerfile.runtime.fixed with improved configuration - Include .dockerignore for build optimization - Add integration documentation and publishing guides - Update API and CLI components for better runtime support - Enhance sandbox and container management features
92 lines
1.8 KiB
Go
92 lines
1.8 KiB
Go
// Copyright 2025 Hanzo Industries Inc.
|
|
// SPDX-License-Identifier: AGPL-3.0
|
|
|
|
package fs
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func UploadFiles(c *gin.Context) {
|
|
reader, err := c.Request.MultipartReader()
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"errors": []string{"invalid multipart form"}})
|
|
return
|
|
}
|
|
|
|
dests := make(map[string]string)
|
|
var errs []string
|
|
|
|
for {
|
|
part, err := reader.NextPart()
|
|
if err == io.EOF {
|
|
break
|
|
}
|
|
if err != nil {
|
|
errs = append(errs, fmt.Sprintf("reading part: %v", err))
|
|
continue
|
|
}
|
|
|
|
name := part.FormName()
|
|
|
|
if strings.HasSuffix(name, ".path") {
|
|
data, err := io.ReadAll(part)
|
|
if err != nil {
|
|
idx := extractIndex(name)
|
|
errs = append(errs, fmt.Sprintf("path[%s]: %v", idx, err))
|
|
continue
|
|
}
|
|
idx := extractIndex(name)
|
|
dests[idx] = string(data)
|
|
continue
|
|
}
|
|
|
|
if strings.HasSuffix(name, ".file") {
|
|
idx := extractIndex(name)
|
|
dest, ok := dests[idx]
|
|
if !ok {
|
|
errs = append(errs, fmt.Sprintf("file[%s]: missing .path metadata", idx))
|
|
continue
|
|
}
|
|
|
|
if d := filepath.Dir(dest); d != "" {
|
|
if err := os.MkdirAll(d, 0o755); err != nil {
|
|
errs = append(errs, fmt.Sprintf("%s: mkdir %s: %v", dest, d, err))
|
|
continue
|
|
}
|
|
}
|
|
|
|
f, err := os.Create(dest)
|
|
if err != nil {
|
|
errs = append(errs, fmt.Sprintf("%s: create: %v", dest, err))
|
|
continue
|
|
}
|
|
|
|
if _, err := io.Copy(f, part); err != nil {
|
|
errs = append(errs, fmt.Sprintf("%s: write: %v", dest, err))
|
|
}
|
|
f.Close()
|
|
continue
|
|
}
|
|
}
|
|
|
|
if len(errs) > 0 {
|
|
c.JSON(http.StatusBadRequest, gin.H{"errors": errs})
|
|
return
|
|
}
|
|
|
|
c.Status(http.StatusOK)
|
|
}
|
|
|
|
func extractIndex(fieldName string) string {
|
|
s := strings.TrimPrefix(fieldName, "files[")
|
|
return strings.TrimSuffix(strings.TrimSuffix(s, "].path"), "].file")
|
|
}
|