Skip to content

Commit

Permalink
feat: support multiple files upload
Browse files Browse the repository at this point in the history
  • Loading branch information
devgianlu committed Dec 28, 2023
1 parent fbf0e19 commit 79abc54
Showing 1 changed file with 23 additions and 16 deletions.
39 changes: 23 additions & 16 deletions http/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,29 +162,36 @@ func (s *httpServer) handleUpload(ctx *fiber.Ctx) error {
path = "."
}

formFile, err := ctx.FormFile("file")
if errors.Is(err, fasthttp.ErrMissingFile) {
return newHttpError(http.StatusBadRequest, "missing file", err)
} else if err != nil {
return err
form, err := ctx.MultipartForm()
if errors.Is(err, fasthttp.ErrNoMultipartForm) {
return newHttpError(http.StatusBadRequest, "missing form", err)
}

uploadFile, err := formFile.Open()
if err != nil {
return err
formFiles, ok := form.File["file"]
if !ok {
return newHttpError(http.StatusBadRequest, "missing files", err)
}

defer func() { _ = uploadFile.Close() }()
for _, formFile := range formFiles {
uploadFile, err := formFile.Open()
if err != nil {
return err
}

localFile, err := s.storage.CreateFile(filepath.Join(path, formFile.Filename), user)
if err != nil {
return err
}
localFile, err := s.storage.CreateFile(filepath.Join(path, formFile.Filename), user)
if err != nil {
_ = uploadFile.Close()
return err
}

defer func() { _ = localFile.Close() }()
if _, err := io.Copy(localFile, uploadFile); err != nil {
_ = localFile.Close()
_ = uploadFile.Close()
return err
}

if _, err := io.Copy(localFile, uploadFile); err != nil {
return err
_ = localFile.Close()
_ = uploadFile.Close()
}

return ctx.Redirect("/files/" + strings.Join(paths, "/"))
Expand Down

0 comments on commit 79abc54

Please sign in to comment.