You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

context_request.go 934B

1234567891011121314151617181920212223242526272829303132
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package context
  4. import (
  5. "io"
  6. "net/http"
  7. "strings"
  8. )
  9. // UploadStream returns the request body or the first form file
  10. // Only form files need to get closed.
  11. func (ctx *Context) UploadStream() (rd io.ReadCloser, needToClose bool, err error) {
  12. contentType := strings.ToLower(ctx.Req.Header.Get("Content-Type"))
  13. if strings.HasPrefix(contentType, "application/x-www-form-urlencoded") || strings.HasPrefix(contentType, "multipart/form-data") {
  14. if err := ctx.Req.ParseMultipartForm(32 << 20); err != nil {
  15. return nil, false, err
  16. }
  17. if ctx.Req.MultipartForm.File == nil {
  18. return nil, false, http.ErrMissingFile
  19. }
  20. for _, files := range ctx.Req.MultipartForm.File {
  21. if len(files) > 0 {
  22. r, err := files[0].Open()
  23. return r, true, err
  24. }
  25. }
  26. return nil, false, http.ErrMissingFile
  27. }
  28. return ctx.Req.Body, false, nil
  29. }