Skip to content

Commit

Permalink
[pocketbase#367] fixed wildcard multiple file upload deletion and add…
Browse files Browse the repository at this point in the history
…ed support to delete a single file by its name
  • Loading branch information
ganigeorgiev committed Aug 31, 2022
1 parent 3bfa0b6 commit 235670f
Show file tree
Hide file tree
Showing 12 changed files with 158 additions and 145 deletions.
36 changes: 24 additions & 12 deletions forms/record_upsert.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,21 +194,26 @@ func (form *RecordUpsert) LoadData(r *http.Request) error {
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"]
// -----------------------------------------------------------
// Delete previously uploaded file(s)
// -----------------------------------------------------------

// if empty value was set, mark all previously uploaded files for deletion
if len(list.ToUniqueStringSlice(value)) == 0 && len(oldNames) > 0 {
form.filesToDelete = append(form.filesToDelete, oldNames...)
form.Data[key] = []string{}
} else if len(oldNames) > 0 {
indexesToDelete := make([]int, 0, len(extendedData))

if cast.ToString(value) == "" || (hasIndexedKey && cast.ToString(indexedKeyValue) == "") {
if len(oldNames) > 0 {
form.filesToDelete = append(form.filesToDelete, oldNames...)
// search for individual file name to delete (eg. "file.test.png = null")
for i, name := range oldNames {
if v, ok := extendedData[key+"."+name]; ok && cast.ToString(v) == "" {
indexesToDelete = append(indexesToDelete, i)
}
form.Data[key] = ""
}
} else if options.MaxSelect > 1 {
// search for individual file index to delete (eg. "file.0")

// search for individual file index to delete (eg. "file.0 = null")
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:])
Expand All @@ -234,7 +239,14 @@ func (form *RecordUpsert) LoadData(r *http.Request) error {
form.Data[key] = nonDeleted
}

// check if there are any new uploaded form files
// -----------------------------------------------------------
// Check for new uploaded file
// -----------------------------------------------------------

if form.getContentType(r) != "multipart/form-data" {
continue // file upload is supported only via multipart/form-data
}

files, err := rest.FindUploadedFiles(r, key)
if err != nil {
if form.config.App.IsDebug() {
Expand Down
37 changes: 19 additions & 18 deletions forms/record_upsert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func TestRecordUpsertLoadDataJson(t *testing.T) {
"onefile": nil,
"manyfiles.0": "",
"manyfiles.1": "test.png", // should be ignored
"onlyimages": nil, // should be ignored
"onlyimages": nil,
}

form := forms.NewRecordUpsert(app, record)
Expand Down Expand Up @@ -136,18 +136,12 @@ func TestRecordUpsertLoadDataJson(t *testing.T) {
}
manyfilesRemains := len(list.ToUniqueStringSlice(manyfiles))
if manyfilesRemains != 1 {
t.Fatalf("Expect only 1 manyfiles to remain, got %v", manyfiles)
t.Fatalf("Expect only 1 manyfiles to remain, got \n%v", manyfiles)
}

// cannot reset multiple file upload field with just using the field name
onlyimages, ok := form.Data["onlyimages"]
if !ok || onlyimages == nil {
t.Fatal("Expect onlyimages field to be set and not be altered")
}
onlyimagesRemains := len(list.ToUniqueStringSlice(onlyimages))
expectedRemains := 2 // 2 existing
if onlyimagesRemains != expectedRemains {
t.Fatalf("Expect onlyimages to be %d, got %d (%v)", expectedRemains, onlyimagesRemains, onlyimages)
if len(list.ToUniqueStringSlice(onlyimages)) != 0 {
t.Fatalf("Expect onlyimages field to be deleted, got \n%v", onlyimages)
}
}

Expand All @@ -167,9 +161,10 @@ func TestRecordUpsertLoadDataMultipart(t *testing.T) {
"unknown": "test456",
// file fields unset/delete
"onefile": "",
"manyfiles.0": "",
"manyfiles.0": "", // delete by index
"manyfiles.b635c395-6837-49e5-8535-b0a6ebfbdbf3.png": "", // delete by name
"manyfiles.1": "test.png", // should be ignored
"onlyimages": "", // should be ignored
"onlyimages": "",
}, "onlyimages")
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -208,16 +203,16 @@ func TestRecordUpsertLoadDataMultipart(t *testing.T) {
t.Fatal("Expect manyfiles field to be set")
}
manyfilesRemains := len(list.ToUniqueStringSlice(manyfiles))
if manyfilesRemains != 1 {
t.Fatalf("Expect only 1 manyfiles to remain, got %v", manyfiles)
if manyfilesRemains != 0 {
t.Fatalf("Expect 0 manyfiles to remain, got %v", manyfiles)
}

onlyimages, ok := form.Data["onlyimages"]
if !ok || onlyimages == nil {
t.Fatal("Expect onlyimages field to be set and not be altered")
t.Fatal("Expect onlyimages field to be set")
}
onlyimagesRemains := len(list.ToUniqueStringSlice(onlyimages))
expectedRemains := 3 // 2 existing + 1 new upload
expectedRemains := 1 // -2 removed + 1 new upload
if onlyimagesRemains != expectedRemains {
t.Fatalf("Expect onlyimages to be %d, got %d (%v)", expectedRemains, onlyimagesRemains, onlyimages)
}
Expand Down Expand Up @@ -489,8 +484,9 @@ func TestRecordUpsertSubmitSuccess(t *testing.T) {
}

formData, mp, err := tests.MockMultipartData(map[string]string{
"title": "test_save",
"onefile": "",
"title": "test_save",
"onefile": "",
"onlyimages": "",
}, "manyfiles.1", "manyfiles") // replace + new file
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -535,6 +531,11 @@ func TestRecordUpsertSubmitSuccess(t *testing.T) {
t.Fatal("Expected record.onefile to be deleted")
}

onlyimages := (recordAfter.GetStringSliceDataValue("onlyimages"))
if len(onlyimages) != 0 {
t.Fatalf("Expected all onlyimages files to be deleted, got %d (%v)", len(onlyimages), onlyimages)
}

manyfiles := (recordAfter.GetStringSliceDataValue("manyfiles"))
if len(manyfiles) != 3 {
t.Fatalf("Expected 3 manyfiles, got %d (%v)", len(manyfiles), manyfiles)
Expand Down
2 changes: 1 addition & 1 deletion ui/.env
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ PB_PROFILE_COLLECTION = "profiles"
PB_INSTALLER_PARAM = "installer"
PB_RULES_SYNTAX_DOCS = "https://pocketbase.io/docs/manage-collections#rules-filters-syntax"
PB_RELEASES = "https://github.com/pocketbase/pocketbase/releases"
PB_VERSION = "v0.5.1"
PB_VERSION = "v0.5.2"

Large diffs are not rendered by default.

Loading

0 comments on commit 235670f

Please sign in to comment.