diff --git a/backend/cmd/server/main.go b/backend/cmd/server/main.go index 04548b4..3d214c7 100644 --- a/backend/cmd/server/main.go +++ b/backend/cmd/server/main.go @@ -36,7 +36,7 @@ func main() { r.GET("/api/v1/files", h.ListFiles) r.GET("/api/v1/file/:id", h.ListFile) - r.POST("/api/v1/files/upload", h.UploadFile) + r.POST("/api/v1/files/upload", h.UploadFiles) r.GET("/api/v1/files/:id", h.GetFile) r.DELETE("/api/v1/files/:id", h.DeleteFile) r.GET("/api/v1/files/search", h.SearchFiles) diff --git a/backend/internal/handlers/files.go b/backend/internal/handlers/files.go index 1babe06..701e19e 100644 --- a/backend/internal/handlers/files.go +++ b/backend/internal/handlers/files.go @@ -172,87 +172,98 @@ func (h *Handlers) GetFile(c *gin.Context) { } -func (h *Handlers) UploadFile(c *gin.Context) { - - file, err := c.FormFile("file") +func (h *Handlers) UploadFiles(c *gin.Context) { + form, err := c.MultipartForm() if err != nil { - c.JSON(http.StatusNotFound, gin.H{ + c.JSON(http.StatusBadRequest, gin.H{ "error": gin.H{ "code": "NOT_FOUND", "message": "form file with file name missing", }, }) + return + } + files := form.File["file"] + + type FileStats struct { + Name string `json:"name"` + Id string `json:"id"` } - dst := filepath.Join("./uploads/", filepath.Base(file.Filename)) + filesStats := []FileStats{} - err = c.SaveUploadedFile(file, dst) + for _, file := range files { - if err != nil { + dst := filepath.Join("./uploads/", filepath.Base(file.Filename)) - c.JSON(http.StatusInternalServerError, gin.H{ - "error": gin.H{ - "code": "ERROR", - "message": "Uploaded", - }, + err = c.SaveUploadedFile(file, dst) + + if err != nil { + + c.JSON(http.StatusInternalServerError, gin.H{ + "error": gin.H{ + "code": "ERROR", + "message": "Uploaded", + }, + }) + + return + + } + + fileByte, err := os.ReadFile(dst) + + if err != nil { + + c.JSON(http.StatusInternalServerError, gin.H{ + "error": gin.H{ + "code": "ERROR", + "message": "Error reading file", + }, + }) + + return + + } + checksum := service.CreateSHA256Hash(fileByte) + + ctx := context.Background() + + id := make([]byte, 16) + rand.Read(id) + + createFileParams := db.CreateFileParams{ + ID: hex.EncodeToString(id), + Name: file.Filename, + Size: file.Size, + StorageKey: dst, + Checksum: hex.EncodeToString(checksum), + } + + dbFile, err := h.queries.CreateFile(ctx, createFileParams) + if err != nil { + + fmt.Println(err) + + c.JSON(http.StatusInternalServerError, gin.H{ + "error": gin.H{ + "code": "DB_ERROR", + "message": "Failed to save file metadata", + }, + }) + return + } + + filesStats = append(filesStats, FileStats{ + Name: dbFile.Name, + Id: dbFile.ID, }) - return - - } - - fileByte, err := os.ReadFile(dst) - - if err != nil { - - // fmt.Println("error reading file") - - c.JSON(http.StatusInternalServerError, gin.H{ - "error": gin.H{ - "code": "ERROR", - "message": "Error reading file", - }, - }) - - return - - } - - checksum := service.CreateSHA256Hash(fileByte) - - ctx := context.Background() - - id := make([]byte, 16) - rand.Read(id) - - createFileParams := db.CreateFileParams{ - ID: hex.EncodeToString(id), - Name: file.Filename, - Size: file.Size, - StorageKey: dst, - Checksum: hex.EncodeToString(checksum), - } - - dbFile, err := h.queries.CreateFile(ctx, createFileParams) - if err != nil { - - fmt.Println(err) - - c.JSON(http.StatusInternalServerError, gin.H{ - "error": gin.H{ - "code": "DB_ERROR", - "message": "Failed to save file metadata", - }, - }) - return } c.JSON(http.StatusOK, gin.H{ - "data": gin.H{ - "id": dbFile.ID, - "name": dbFile.Name, - }, + "data": filesStats, }) }