Skip to content

Commit 59af739

Browse files
committed
feat: backend changes
1 parent 41d3b23 commit 59af739

File tree

3 files changed

+51
-63
lines changed

3 files changed

+51
-63
lines changed

lib/httpapi/models.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ type MessageResponse struct {
6565

6666
type UploadResponse struct {
6767
Body struct {
68-
Ok bool `json:"ok" doc:"Indicates whether the files were uploaded successfully."`
68+
Ok bool `json:"ok" doc:"Indicates whether the files were uploaded successfully."`
69+
FilePath string `json:"filePath" doc:"Path of the file"`
6970
}
7071
}
7172

7273
type UploadRequest struct {
73-
UploadPath string `form:"uploadPath" required:"true" doc:"location where all the files in the zip will be extracted to."`
74-
Files huma.FormFile `form:"file" contentType:"application/zip" required:"true" doc:"zip of all the files that needs to be uploaded"`
74+
File huma.FormFile `form:"file" required:"true" doc:"file that needs to be uploaded"`
7575
}

lib/httpapi/server.go

Lines changed: 41 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package httpapi
22

33
import (
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

5051
func (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
581560
func (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
589580
func (s *Server) registerStaticFileRoutes() {
590581
chatHandler := FileServerWithIndexFallback(s.chatBasePath)

openapi.json

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -314,12 +314,17 @@
314314
"readOnly": true,
315315
"type": "string"
316316
},
317+
"filePath": {
318+
"description": "Path of the file",
319+
"type": "string"
320+
},
317321
"ok": {
318322
"description": "Indicates whether the files were uploaded successfully.",
319323
"type": "boolean"
320324
}
321325
},
322326
"required": [
327+
"filePath",
323328
"ok"
324329
],
325330
"type": "object"
@@ -527,28 +532,20 @@
527532
"multipart/form-data": {
528533
"encoding": {
529534
"file": {
530-
"contentType": "application/zip"
531-
},
532-
"uploadPath": {
533-
"contentType": "text/plain"
535+
"contentType": "application/octet-stream"
534536
}
535537
},
536538
"schema": {
537539
"properties": {
538540
"file": {
539541
"contentEncoding": "binary",
540542
"contentMediaType": "application/octet-stream",
541-
"description": "zip of all the files that needs to be uploaded",
543+
"description": "file that needs to be uploaded",
542544
"format": "binary",
543545
"type": "string"
544-
},
545-
"uploadPath": {
546-
"description": "location where all the files in the zip will be extracted to.",
547-
"type": "string"
548546
}
549547
},
550548
"required": [
551-
"uploadPath",
552549
"file"
553550
],
554551
"type": "object"

0 commit comments

Comments
 (0)