11package httpapi
22
33import (
4- "archive/zip"
5- "bytes"
64 "context"
5+ "crypto/sha256"
6+ "encoding/hex"
77 "encoding/json"
88 "fmt"
99 "io"
@@ -45,6 +45,7 @@ type Server struct {
4545 agentType mf.AgentType
4646 emitter * EventEmitter
4747 chatBasePath string
48+ tempDir string
4849}
4950
5051func (s * Server ) NormalizeSchema (schema any ) any {
@@ -237,6 +238,14 @@ func NewServer(ctx context.Context, config ServerConfig) (*Server, error) {
237238 FormatMessage : formatMessage ,
238239 })
239240 emitter := NewEventEmitter (1024 )
241+
242+ // Create temporary directory for uploads
243+ tempDir , err := os .MkdirTemp ("" , "agentapi-uploads-" )
244+ if err != nil {
245+ return nil , xerrors .Errorf ("failed to create temporary directory: %w" , err )
246+ }
247+ logger .Info ("Created temporary directory for uploads" , "tempDir" , tempDir )
248+
240249 s := & Server {
241250 router : router ,
242251 api : api ,
@@ -247,6 +256,7 @@ func NewServer(ctx context.Context, config ServerConfig) (*Server, error) {
247256 agentType : config .AgentType ,
248257 emitter : emitter ,
249258 chatBasePath : strings .TrimSuffix (config .ChatBasePath , "/" ),
259+ tempDir : tempDir ,
250260 }
251261
252262 // Register API routes
@@ -432,67 +442,36 @@ func (s *Server) uploadFiles(ctx context.Context, input *struct {
432442}) (* UploadResponse , error ) {
433443 formData := input .RawBody .Data ()
434444
435- file := formData .Files .File
445+ file := formData .File .File
436446
437447 buf , err := io .ReadAll (file )
438448 if err != nil {
439- return nil , xerrors .Errorf ("failed to upload files : %w" , err )
449+ return nil , xerrors .Errorf ("failed to upload file : %w" , err )
440450 }
441451
442- // Create a zip.Reader from the buffer
443- zipReader , err := zip .NewReader (bytes .NewReader (buf ), int64 (len (buf )))
452+ // Calculate checksum of the uploaded file to create unique subdirectory
453+ hash := sha256 .Sum256 (buf )
454+ checksum := hex .EncodeToString (hash [:8 ]) // Use first 8 bytes (16 hex chars)
455+
456+ // Create checksum-based subdirectory in tempDir
457+ uploadDir := filepath .Join (s .tempDir , checksum )
458+ err = os .MkdirAll (uploadDir , 0755 )
444459 if err != nil {
445- return nil , xerrors .Errorf ("failed to upload files : %w" , err )
460+ return nil , xerrors .Errorf ("failed to create upload directory : %w" , err )
446461 }
447462
448- for _ , f := range zipReader .File {
449- outPath := filepath .Join (formData .UploadPath , f .Name )
450-
451- if f .FileInfo ().IsDir () {
452- err := os .MkdirAll (outPath , f .Mode ())
453- if err != nil {
454- return nil , xerrors .Errorf ("failed to upload files: %w" , err )
455- }
456- continue
457- }
458-
459- err := os .MkdirAll (filepath .Dir (outPath ), f .Mode ())
460- if err != nil {
461- return nil , xerrors .Errorf ("failed to upload files: %w" , err )
462- }
463-
464- outFile , err := os .OpenFile (outPath , os .O_WRONLY | os .O_CREATE | os .O_TRUNC , f .Mode ())
465- if err != nil {
466- return nil , xerrors .Errorf ("failed to upload files: %w" , err )
467- }
468-
469- rc , err := f .Open ()
470- if err != nil {
471- err := outFile .Close ()
472- if err != nil {
473- return nil , xerrors .Errorf ("failed to upload files: %w" , err )
474- }
475- return nil , xerrors .Errorf ("failed to upload files: %w" , err )
476- }
477-
478- _ , err = io .Copy (outFile , rc )
479- if err != nil {
480- return nil , xerrors .Errorf ("failed to upload files: %w" , err )
481- }
482-
483- err = rc .Close ()
484- if err != nil {
485- return nil , xerrors .Errorf ("failed to upload files: %w" , err )
486- }
463+ // Save individual file with original filename
464+ filename := formData .File .Filename
487465
488- err = outFile . Close ( )
489- if err != nil {
490- return nil , xerrors . Errorf ( "failed to upload files: %w" , err )
491- }
466+ outPath := filepath . Join ( uploadDir , filename )
467+ err = os . WriteFile ( outPath , buf , 0644 )
468+ if err != nil {
469+ return nil , xerrors . Errorf ( "failed to write file: %w" , err )
492470 }
493471
494472 resp := & UploadResponse {}
495473 resp .Body .Ok = true
474+ resp .Body .FilePath = outPath
496475 return resp , nil
497476}
498477
@@ -579,12 +558,24 @@ func (s *Server) Start() error {
579558
580559// Stop gracefully stops the HTTP server
581560func (s * Server ) Stop (ctx context.Context ) error {
561+ // Clean up temporary directory
562+ s .cleanupTempDir ()
563+
582564 if s .srv != nil {
583565 return s .srv .Shutdown (ctx )
584566 }
585567 return nil
586568}
587569
570+ // cleanupTempDir removes the temporary directory and all its contents
571+ func (s * Server ) cleanupTempDir () {
572+ if err := os .RemoveAll (s .tempDir ); err != nil {
573+ s .logger .Error ("Failed to clean up temporary directory" , "tempDir" , s .tempDir , "error" , err )
574+ } else {
575+ s .logger .Info ("Cleaned up temporary directory" , "tempDir" , s .tempDir )
576+ }
577+ }
578+
588579// registerStaticFileRoutes sets up routes for serving static files
589580func (s * Server ) registerStaticFileRoutes () {
590581 chatHandler := FileServerWithIndexFallback (s .chatBasePath )
0 commit comments