upload multiple files

This commit is contained in:
m
2026-07-12 10:11:46 +02:00
parent 1f3bee120d
commit 67fadc4b37
2 changed files with 76 additions and 65 deletions
+1 -1
View File
@@ -36,7 +36,7 @@ func main() {
r.GET("/api/v1/files", h.ListFiles) r.GET("/api/v1/files", h.ListFiles)
r.GET("/api/v1/file/:id", h.ListFile) 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.GET("/api/v1/files/:id", h.GetFile)
r.DELETE("/api/v1/files/:id", h.DeleteFile) r.DELETE("/api/v1/files/:id", h.DeleteFile)
r.GET("/api/v1/files/search", h.SearchFiles) r.GET("/api/v1/files/search", h.SearchFiles)
+75 -64
View File
@@ -172,87 +172,98 @@ func (h *Handlers) GetFile(c *gin.Context) {
} }
func (h *Handlers) UploadFile(c *gin.Context) { func (h *Handlers) UploadFiles(c *gin.Context) {
file, err := c.FormFile("file")
form, err := c.MultipartForm()
if err != nil { if err != nil {
c.JSON(http.StatusNotFound, gin.H{ c.JSON(http.StatusBadRequest, gin.H{
"error": gin.H{ "error": gin.H{
"code": "NOT_FOUND", "code": "NOT_FOUND",
"message": "form file with file name missing", "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{ err = c.SaveUploadedFile(file, dst)
"error": gin.H{
"code": "ERROR", if err != nil {
"message": "Uploaded",
}, 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{ c.JSON(http.StatusOK, gin.H{
"data": gin.H{ "data": filesStats,
"id": dbFile.ID,
"name": dbFile.Name,
},
}) })
} }