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/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)
+22 -11
View File
@@ -172,18 +172,28 @@ 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"`
}
filesStats := []FileStats{}
for _, file := range files {
dst := filepath.Join("./uploads/", filepath.Base(file.Filename))
@@ -206,8 +216,6 @@ func (h *Handlers) UploadFile(c *gin.Context) {
if err != nil {
// fmt.Println("error reading file")
c.JSON(http.StatusInternalServerError, gin.H{
"error": gin.H{
"code": "ERROR",
@@ -218,7 +226,6 @@ func (h *Handlers) UploadFile(c *gin.Context) {
return
}
checksum := service.CreateSHA256Hash(fileByte)
ctx := context.Background()
@@ -248,11 +255,15 @@ func (h *Handlers) UploadFile(c *gin.Context) {
return
}
filesStats = append(filesStats, FileStats{
Name: dbFile.Name,
Id: dbFile.ID,
})
}
c.JSON(http.StatusOK, gin.H{
"data": gin.H{
"id": dbFile.ID,
"name": dbFile.Name,
},
"data": filesStats,
})
}