1
1
package httpapi
2
2
3
3
import (
4
- "archive/zip"
5
- "bytes"
6
4
"context"
5
+ "crypto/sha256"
6
+ "encoding/hex"
7
7
"encoding/json"
8
8
"fmt"
9
9
"io"
@@ -45,6 +45,7 @@ type Server struct {
45
45
agentType mf.AgentType
46
46
emitter * EventEmitter
47
47
chatBasePath string
48
+ tempDir string
48
49
}
49
50
50
51
func (s * Server ) NormalizeSchema (schema any ) any {
@@ -237,6 +238,14 @@ func NewServer(ctx context.Context, config ServerConfig) (*Server, error) {
237
238
FormatMessage : formatMessage ,
238
239
})
239
240
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
+
240
249
s := & Server {
241
250
router : router ,
242
251
api : api ,
@@ -247,6 +256,7 @@ func NewServer(ctx context.Context, config ServerConfig) (*Server, error) {
247
256
agentType : config .AgentType ,
248
257
emitter : emitter ,
249
258
chatBasePath : strings .TrimSuffix (config .ChatBasePath , "/" ),
259
+ tempDir : tempDir ,
250
260
}
251
261
252
262
// Register API routes
@@ -432,67 +442,36 @@ func (s *Server) uploadFiles(ctx context.Context, input *struct {
432
442
}) (* UploadResponse , error ) {
433
443
formData := input .RawBody .Data ()
434
444
435
- file := formData .Files .File
445
+ file := formData .File .File
436
446
437
447
buf , err := io .ReadAll (file )
438
448
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 )
440
450
}
441
451
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 )
444
459
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 )
446
461
}
447
462
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
487
465
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 )
492
470
}
493
471
494
472
resp := & UploadResponse {}
495
473
resp .Body .Ok = true
474
+ resp .Body .FilePath = outPath
496
475
return resp , nil
497
476
}
498
477
@@ -579,12 +558,24 @@ func (s *Server) Start() error {
579
558
580
559
// Stop gracefully stops the HTTP server
581
560
func (s * Server ) Stop (ctx context.Context ) error {
561
+ // Clean up temporary directory
562
+ s .cleanupTempDir ()
563
+
582
564
if s .srv != nil {
583
565
return s .srv .Shutdown (ctx )
584
566
}
585
567
return nil
586
568
}
587
569
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
+
588
579
// registerStaticFileRoutes sets up routes for serving static files
589
580
func (s * Server ) registerStaticFileRoutes () {
590
581
chatHandler := FileServerWithIndexFallback (s .chatBasePath )
0 commit comments