initial v0.8 pre-release

This commit is contained in:
Gani Georgiev
2022-10-30 10:28:14 +02:00
parent 9cbb2e750e
commit 90dba45d7c
388 changed files with 21580 additions and 13603 deletions
+17 -19
View File
@@ -1,15 +1,14 @@
package rest
import (
"bytes"
"fmt"
"io"
"mime/multipart"
"net/http"
"path/filepath"
"regexp"
"strings"
"github.com/gabriel-vasile/mimetype"
"github.com/pocketbase/pocketbase/tools/inflector"
"github.com/pocketbase/pocketbase/tools/security"
)
@@ -24,7 +23,6 @@ var extensionInvalidCharsRegex = regexp.MustCompile(`[^\w\.\*\-\+\=\#]+`)
type UploadedFile struct {
name string
header *multipart.FileHeader
bytes []byte
}
// Name returns an assigned unique name to the uploaded file.
@@ -37,11 +35,6 @@ func (f *UploadedFile) Header() *multipart.FileHeader {
return f.header
}
// Bytes returns a slice with the file content.
func (f *UploadedFile) Bytes() []byte {
return f.bytes
}
// FindUploadedFiles extracts all form files of `key` from a http request
// and returns a slice with `UploadedFile` instances (if any).
func FindUploadedFiles(r *http.Request, key string) ([]*UploadedFile, error) {
@@ -56,26 +49,32 @@ func FindUploadedFiles(r *http.Request, key string) ([]*UploadedFile, error) {
return nil, http.ErrMissingFile
}
result := make([]*UploadedFile, len(r.MultipartForm.File[key]))
result := make([]*UploadedFile, 0, len(r.MultipartForm.File[key]))
for i, fh := range r.MultipartForm.File[key] {
for _, fh := range r.MultipartForm.File[key] {
file, err := fh.Open()
if err != nil {
return nil, err
}
defer file.Close()
buf := bytes.NewBuffer(nil)
if _, err := io.Copy(buf, file); err != nil {
return nil, err
}
// extension
// ---
originalExt := filepath.Ext(fh.Filename)
sanitizedExt := extensionInvalidCharsRegex.ReplaceAllString(originalExt, "")
if sanitizedExt == "" {
// try to detect the extension from the mime type
mt, err := mimetype.DetectReader(file)
if err != nil {
return nil, err
}
sanitizedExt = mt.Extension()
}
// name
// ---
originalName := strings.TrimSuffix(fh.Filename, originalExt)
sanitizedName := inflector.Snakecase(originalName)
if length := len(sanitizedName); length < 3 {
// the name is too short so we concatenate an additional random part
sanitizedName += ("_" + security.RandomString(10))
@@ -91,11 +90,10 @@ func FindUploadedFiles(r *http.Request, key string) ([]*UploadedFile, error) {
sanitizedExt,
)
result[i] = &UploadedFile{
result = append(result, &UploadedFile{
name: uploadedFilename,
header: fh,
bytes: buf.Bytes(),
}
})
}
return result, nil