1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2025-01-10 00:43:36 +02:00
pocketbase/tools/rest/uploaded_file.go

40 lines
997 B
Go
Raw Normal View History

2022-07-06 23:19:05 +02:00
package rest
import (
"net/http"
"github.com/pocketbase/pocketbase/tools/filesystem"
2022-07-06 23:19:05 +02:00
)
// DefaultMaxMemory defines the default max memory bytes that
// will be used when parsing a form request body.
const DefaultMaxMemory = 32 << 20 // 32mb
// FindUploadedFiles extracts all form files of "key" from a http request
// and returns a slice with filesystem.File instances (if any).
func FindUploadedFiles(r *http.Request, key string) ([]*filesystem.File, error) {
2022-07-06 23:19:05 +02:00
if r.MultipartForm == nil {
err := r.ParseMultipartForm(DefaultMaxMemory)
if err != nil {
return nil, err
}
}
if r.MultipartForm == nil || r.MultipartForm.File == nil || len(r.MultipartForm.File[key]) == 0 {
return nil, http.ErrMissingFile
}
result := make([]*filesystem.File, 0, len(r.MultipartForm.File[key]))
2022-07-06 23:19:05 +02:00
2022-10-30 10:28:14 +02:00
for _, fh := range r.MultipartForm.File[key] {
file, err := filesystem.NewFileFromMultipart(fh)
2022-07-06 23:19:05 +02:00
if err != nil {
return nil, err
}
result = append(result, file)
2022-07-06 23:19:05 +02:00
}
return result, nil
}