1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2024-12-11 13:18:31 +02:00
pocketbase/forms/record_upsert.go

465 lines
12 KiB
Go
Raw Normal View History

2022-07-06 23:19:05 +02:00
package forms
import (
"encoding/json"
"errors"
"fmt"
"log"
2022-07-06 23:19:05 +02:00
"net/http"
"regexp"
"strconv"
2022-08-06 21:16:58 +02:00
validation "github.com/go-ozzo/ozzo-validation/v4"
2022-07-06 23:19:05 +02:00
"github.com/pocketbase/dbx"
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/daos"
"github.com/pocketbase/pocketbase/forms/validators"
"github.com/pocketbase/pocketbase/models"
"github.com/pocketbase/pocketbase/models/schema"
"github.com/pocketbase/pocketbase/tools/list"
"github.com/pocketbase/pocketbase/tools/rest"
"github.com/spf13/cast"
)
2022-08-06 21:16:58 +02:00
// RecordUpsert specifies a [models.Record] upsert (create/update) form.
2022-07-06 23:19:05 +02:00
type RecordUpsert struct {
2022-08-06 21:16:58 +02:00
config RecordUpsertConfig
2022-07-06 23:19:05 +02:00
record *models.Record
filesToDelete []string // names list
filesToUpload []*rest.UploadedFile
2022-08-06 21:16:58 +02:00
Id string `form:"id" json:"id"`
2022-07-06 23:19:05 +02:00
Data map[string]any `json:"data"`
}
2022-08-06 21:16:58 +02:00
// RecordUpsertConfig is the [RecordUpsert] factory initializer config.
//
2022-08-07 14:38:21 +02:00
// NB! App is required struct member.
2022-08-06 21:16:58 +02:00
type RecordUpsertConfig struct {
2022-08-07 14:38:21 +02:00
App core.App
TxDao *daos.Dao
2022-08-06 21:16:58 +02:00
}
// NewRecordUpsert creates a new [RecordUpsert] form with initializer
// config created from the provided [core.App] and [models.Record] instances
2022-08-08 18:16:33 +02:00
// (for create you could pass a pointer to an empty Record - `models.NewRecord(collection)`).
2022-08-06 21:16:58 +02:00
//
// If you want to submit the form as part of another transaction, use
2022-08-07 14:38:21 +02:00
// [NewRecordUpsertWithConfig] with explicitly set TxDao.
2022-07-06 23:19:05 +02:00
func NewRecordUpsert(app core.App, record *models.Record) *RecordUpsert {
2022-08-06 21:16:58 +02:00
return NewRecordUpsertWithConfig(RecordUpsertConfig{
2022-08-07 14:38:21 +02:00
App: app,
2022-08-06 21:16:58 +02:00
}, record)
}
// NewRecordUpsertWithConfig creates a new [RecordUpsert] form
// with the provided config and [models.Record] instance or panics on invalid configuration
2022-08-08 18:16:33 +02:00
// (for create you could pass a pointer to an empty Record - `models.NewRecord(collection)`).
2022-08-06 21:16:58 +02:00
func NewRecordUpsertWithConfig(config RecordUpsertConfig, record *models.Record) *RecordUpsert {
2022-07-06 23:19:05 +02:00
form := &RecordUpsert{
2022-08-06 21:16:58 +02:00
config: config,
2022-07-06 23:19:05 +02:00
record: record,
filesToDelete: []string{},
filesToUpload: []*rest.UploadedFile{},
}
2022-08-07 14:38:21 +02:00
if form.config.App == nil || form.record == nil {
2022-08-06 21:16:58 +02:00
panic("Invalid initializer config or nil upsert model.")
}
2022-08-07 14:38:21 +02:00
if form.config.TxDao == nil {
form.config.TxDao = form.config.App.Dao()
}
2022-08-06 21:16:58 +02:00
form.Id = record.Id
2022-07-06 23:19:05 +02:00
form.Data = map[string]any{}
for _, field := range record.Collection().Schema.Fields() {
form.Data[field.Name] = record.GetDataValue(field.Name)
}
return form
}
func (form *RecordUpsert) getContentType(r *http.Request) string {
t := r.Header.Get("Content-Type")
for i, c := range t {
if c == ' ' || c == ';' {
return t[:i]
}
}
return t
}
func (form *RecordUpsert) extractRequestData(r *http.Request) (map[string]any, error) {
switch form.getContentType(r) {
case "application/json":
return form.extractJsonData(r)
case "multipart/form-data":
return form.extractMultipartFormData(r)
default:
return nil, errors.New("Unsupported request Content-Type.")
}
}
func (form *RecordUpsert) extractJsonData(r *http.Request) (map[string]any, error) {
result := map[string]any{}
err := rest.ReadJsonBodyCopy(r, &result)
return result, err
}
func (form *RecordUpsert) extractMultipartFormData(r *http.Request) (map[string]any, error) {
result := map[string]any{}
// parse form data (if not already)
if err := r.ParseMultipartForm(rest.DefaultMaxMemory); err != nil {
return result, err
}
arrayValueSupportTypes := schema.ArraybleFieldTypes()
for key, values := range r.PostForm {
if len(values) == 0 {
result[key] = nil
continue
}
field := form.record.Collection().Schema.GetFieldByName(key)
if field != nil && list.ExistInSlice(field.Type, arrayValueSupportTypes) {
result[key] = values
} else {
result[key] = values[0]
}
}
return result, nil
}
func (form *RecordUpsert) normalizeData() error {
for _, field := range form.record.Collection().Schema.Fields() {
if v, ok := form.Data[field.Name]; ok {
form.Data[field.Name] = field.PrepareValue(v)
}
}
return nil
}
// LoadData loads and normalizes json OR multipart/form-data request data.
//
// File upload is supported only via multipart/form-data.
//
// To REPLACE previously uploaded file(s) you can suffix the field name
// with the file index (eg. `myfile.0`) and set the new value.
// For single file upload fields, you can skip the index and directly
// assign the file value to the field name (eg. `myfile`).
//
// To DELETE previously uploaded file(s) you can suffix the field name
// with the file index (eg. `myfile.0`) and set it to null or empty string.
// For single file upload fields, you can skip the index and directly
// reset the field using its field name (eg. `myfile`).
func (form *RecordUpsert) LoadData(r *http.Request) error {
requestData, err := form.extractRequestData(r)
if err != nil {
return err
}
2022-08-06 21:16:58 +02:00
if id, ok := requestData["id"]; ok {
form.Id = cast.ToString(id)
}
2022-07-06 23:19:05 +02:00
// extend base data with the extracted one
extendedData := form.record.Data()
rawData, err := json.Marshal(requestData)
if err != nil {
return err
}
if err := json.Unmarshal(rawData, &extendedData); err != nil {
return err
}
for _, field := range form.record.Collection().Schema.Fields() {
key := field.Name
value := extendedData[key]
2022-07-06 23:19:05 +02:00
value = field.PrepareValue(value)
if field.Type != schema.FieldTypeFile {
form.Data[key] = value
continue
}
options, _ := field.Options.(*schema.FileOptions)
oldNames := list.ToUniqueStringSlice(form.Data[key])
// delete previously uploaded file(s)
if options.MaxSelect == 1 {
// search for unset zero indexed key as a fallback
indexedKeyValue, hasIndexedKey := extendedData[key+".0"]
2022-07-06 23:19:05 +02:00
if cast.ToString(value) == "" || (hasIndexedKey && cast.ToString(indexedKeyValue) == "") {
if len(oldNames) > 0 {
form.filesToDelete = append(form.filesToDelete, oldNames...)
}
form.Data[key] = ""
}
} else if options.MaxSelect > 1 {
// search for individual file index to delete (eg. "file.0")
keyExp, _ := regexp.Compile(`^` + regexp.QuoteMeta(key) + `\.\d+$`)
indexesToDelete := make([]int, 0, len(extendedData))
for indexedKey := range extendedData {
if keyExp.MatchString(indexedKey) && cast.ToString(extendedData[indexedKey]) == "" {
index, indexErr := strconv.Atoi(indexedKey[len(key)+1:])
if indexErr != nil || index >= len(oldNames) {
2022-07-06 23:19:05 +02:00
continue
}
indexesToDelete = append(indexesToDelete, index)
2022-07-06 23:19:05 +02:00
}
}
// slice to fill only with the non-deleted indexes
nonDeleted := make([]string, 0, len(oldNames))
for i, name := range oldNames {
// not marked for deletion
if !list.ExistInSlice(i, indexesToDelete) {
nonDeleted = append(nonDeleted, name)
continue
}
// store the id to actually delete the file later
form.filesToDelete = append(form.filesToDelete, name)
2022-07-06 23:19:05 +02:00
}
form.Data[key] = nonDeleted
}
2022-07-06 23:19:05 +02:00
// check if there are any new uploaded form files
files, err := rest.FindUploadedFiles(r, key)
if err != nil {
2022-08-07 14:38:21 +02:00
if form.config.App.IsDebug() {
log.Printf("%q uploaded file error: %v\n", key, err)
}
continue // skip invalid or missing file(s)
}
2022-07-06 23:19:05 +02:00
// refresh oldNames list
oldNames = list.ToUniqueStringSlice(form.Data[key])
if options.MaxSelect == 1 {
// delete previous file(s) before replacing
if len(oldNames) > 0 {
form.filesToDelete = list.ToUniqueStringSlice(append(form.filesToDelete, oldNames...))
2022-07-06 23:19:05 +02:00
}
form.filesToUpload = append(form.filesToUpload, files[0])
form.Data[key] = files[0].Name()
} else if options.MaxSelect > 1 {
// append the id of each uploaded file instance
form.filesToUpload = append(form.filesToUpload, files...)
for _, file := range files {
oldNames = append(oldNames, file.Name())
}
form.Data[key] = oldNames
2022-07-06 23:19:05 +02:00
}
}
return form.normalizeData()
}
// Validate makes the form validatable by implementing [validation.Validatable] interface.
func (form *RecordUpsert) Validate() error {
2022-08-06 21:16:58 +02:00
// base form fields validator
baseFieldsErrors := validation.ValidateStruct(form,
validation.Field(
&form.Id,
validation.When(
form.record.IsNew(),
validation.Length(models.DefaultIdLength, models.DefaultIdLength),
2022-08-11 09:29:01 +02:00
validation.Match(idRegex),
2022-08-06 21:16:58 +02:00
).Else(validation.In(form.record.Id)),
),
)
if baseFieldsErrors != nil {
return baseFieldsErrors
}
// record data validator
2022-07-06 23:19:05 +02:00
dataValidator := validators.NewRecordDataValidator(
2022-08-07 14:38:21 +02:00
form.config.TxDao,
2022-07-06 23:19:05 +02:00
form.record,
form.filesToUpload,
)
return dataValidator.Validate(form.Data)
}
// DrySubmit performs a form submit within a transaction and reverts it.
// For actual record persistence, check the `form.Submit()` method.
//
// This method doesn't handle file uploads/deletes or trigger any app events!
func (form *RecordUpsert) DrySubmit(callback func(txDao *daos.Dao) error) error {
if err := form.Validate(); err != nil {
return err
}
2022-08-08 18:16:33 +02:00
isNew := form.record.IsNew()
2022-08-06 21:16:58 +02:00
// custom insertion id can be set only on create
2022-08-08 18:16:33 +02:00
if isNew && form.Id != "" {
2022-08-06 21:16:58 +02:00
form.record.MarkAsNew()
form.record.SetId(form.Id)
}
2022-07-06 23:19:05 +02:00
// bulk load form data
if err := form.record.Load(form.Data); err != nil {
return err
}
2022-08-07 14:38:21 +02:00
return form.config.TxDao.RunInTransaction(func(txDao *daos.Dao) error {
2022-07-06 23:19:05 +02:00
tx, ok := txDao.DB().(*dbx.Tx)
if !ok {
return errors.New("failed to get transaction db")
}
defer tx.Rollback()
2022-08-08 18:16:33 +02:00
2022-07-06 23:19:05 +02:00
txDao.BeforeCreateFunc = nil
txDao.AfterCreateFunc = nil
txDao.BeforeUpdateFunc = nil
txDao.AfterUpdateFunc = nil
if err := txDao.SaveRecord(form.record); err != nil {
return err
}
2022-08-08 18:16:33 +02:00
// restore record isNew state
if isNew {
form.record.MarkAsNew()
}
if callback != nil {
return callback(txDao)
}
return nil
2022-07-06 23:19:05 +02:00
})
}
// Submit validates the form and upserts the form Record model.
//
// You can optionally provide a list of InterceptorFunc to further
// modify the form behavior before persisting it.
func (form *RecordUpsert) Submit(interceptors ...InterceptorFunc) error {
2022-07-06 23:19:05 +02:00
if err := form.Validate(); err != nil {
return err
}
2022-08-06 21:16:58 +02:00
// custom insertion id can be set only on create
if form.record.IsNew() && form.Id != "" {
form.record.MarkAsNew()
form.record.SetId(form.Id)
}
2022-07-06 23:19:05 +02:00
// bulk load form data
if err := form.record.Load(form.Data); err != nil {
return err
}
return runInterceptors(func() error {
2022-08-07 14:38:21 +02:00
return form.config.TxDao.RunInTransaction(func(txDao *daos.Dao) error {
// persist record model
if err := txDao.SaveRecord(form.record); err != nil {
return err
}
2022-07-06 23:19:05 +02:00
// upload new files (if any)
if err := form.processFilesToUpload(); err != nil {
return err
}
2022-07-06 23:19:05 +02:00
// delete old files (if any)
if err := form.processFilesToDelete(); err != nil { //nolint:staticcheck
// for now fail silently to avoid reupload when `form.Submit()`
// is called manually (aka. not from an api request)...
}
2022-07-06 23:19:05 +02:00
return nil
})
}, interceptors...)
2022-07-06 23:19:05 +02:00
}
func (form *RecordUpsert) processFilesToUpload() error {
if len(form.filesToUpload) == 0 {
return nil // nothing to upload
}
if !form.record.HasId() {
return errors.New("The record is not persisted yet.")
}
2022-08-07 14:38:21 +02:00
fs, err := form.config.App.NewFilesystem()
2022-07-06 23:19:05 +02:00
if err != nil {
return err
}
defer fs.Close()
var uploadErrors []error
2022-07-06 23:19:05 +02:00
for i := len(form.filesToUpload) - 1; i >= 0; i-- {
file := form.filesToUpload[i]
path := form.record.BaseFilesPath() + "/" + file.Name()
if err := fs.Upload(file.Bytes(), path); err == nil {
// remove the uploaded file from the list
2022-07-06 23:19:05 +02:00
form.filesToUpload = append(form.filesToUpload[:i], form.filesToUpload[i+1:]...)
} else {
// store the upload error
uploadErrors = append(uploadErrors, fmt.Errorf("File %d: %v", i, err))
2022-07-06 23:19:05 +02:00
}
}
if len(uploadErrors) > 0 {
return fmt.Errorf("Failed to upload all files: %v", uploadErrors)
2022-07-06 23:19:05 +02:00
}
return nil
}
func (form *RecordUpsert) processFilesToDelete() error {
if len(form.filesToDelete) == 0 {
return nil // nothing to delete
}
if !form.record.HasId() {
return errors.New("The record is not persisted yet.")
}
2022-08-07 14:38:21 +02:00
fs, err := form.config.App.NewFilesystem()
2022-07-06 23:19:05 +02:00
if err != nil {
return err
}
defer fs.Close()
var deleteErrors []error
2022-07-06 23:19:05 +02:00
for i := len(form.filesToDelete) - 1; i >= 0; i-- {
filename := form.filesToDelete[i]
path := form.record.BaseFilesPath() + "/" + filename
if err := fs.Delete(path); err == nil {
// remove the deleted file from the list
2022-07-06 23:19:05 +02:00
form.filesToDelete = append(form.filesToDelete[:i], form.filesToDelete[i+1:]...)
} else {
// store the delete error
deleteErrors = append(deleteErrors, fmt.Errorf("File %d: %v", i, err))
2022-07-06 23:19:05 +02:00
}
// try to delete the related file thumbs (if any)
fs.DeletePrefix(form.record.BaseFilesPath() + "/thumbs_" + filename + "/")
}
if len(deleteErrors) > 0 {
return fmt.Errorf("Failed to delete all files: %v", deleteErrors)
2022-07-06 23:19:05 +02:00
}
return nil
}