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