mirror of
https://github.com/pocketbase/pocketbase.git
synced 2025-02-15 17:14:03 +02:00
added empty dir delete test for trailing slash prefixes
This commit is contained in:
parent
d2e355e8cb
commit
5be32e8651
@ -8,7 +8,6 @@ import (
|
|||||||
"mime/multipart"
|
"mime/multipart"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"sort"
|
"sort"
|
||||||
@ -276,6 +275,8 @@ func (s *System) Delete(fileKey string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DeletePrefix deletes everything starting with the specified prefix.
|
// DeletePrefix deletes everything starting with the specified prefix.
|
||||||
|
//
|
||||||
|
// The prefix could be subpath (ex. "/a/b/") or filename prefix (ex. "/a/b/file_").
|
||||||
func (s *System) DeletePrefix(prefix string) []error {
|
func (s *System) DeletePrefix(prefix string) []error {
|
||||||
failed := []error{}
|
failed := []error{}
|
||||||
|
|
||||||
@ -285,7 +286,14 @@ func (s *System) DeletePrefix(prefix string) []error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dirsMap := map[string]struct{}{}
|
dirsMap := map[string]struct{}{}
|
||||||
dirsMap[prefix] = struct{}{}
|
|
||||||
|
var isPrefixDir bool
|
||||||
|
|
||||||
|
// treat the prefix as directory only if it ends with trailing slash
|
||||||
|
if strings.HasSuffix(prefix, "/") {
|
||||||
|
isPrefixDir = true
|
||||||
|
dirsMap[strings.TrimRight(prefix, "/")] = struct{}{}
|
||||||
|
}
|
||||||
|
|
||||||
// delete all files with the prefix
|
// delete all files with the prefix
|
||||||
// ---
|
// ---
|
||||||
@ -303,8 +311,11 @@ func (s *System) DeletePrefix(prefix string) []error {
|
|||||||
|
|
||||||
if err := s.Delete(obj.Key); err != nil {
|
if err := s.Delete(obj.Key); err != nil {
|
||||||
failed = append(failed, err)
|
failed = append(failed, err)
|
||||||
} else {
|
} else if isPrefixDir {
|
||||||
dirsMap[path.Dir(obj.Key)] = struct{}{}
|
slashIdx := strings.LastIndex(obj.Key, "/")
|
||||||
|
if slashIdx > -1 {
|
||||||
|
dirsMap[obj.Key[:slashIdx]] = struct{}{}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// ---
|
// ---
|
||||||
|
@ -101,7 +101,7 @@ func TestFileSystemDelete(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFileSystemDeletePrefix(t *testing.T) {
|
func TestFileSystemDeletePrefixWithoutTrailingSlash(t *testing.T) {
|
||||||
dir := createTestDir(t)
|
dir := createTestDir(t)
|
||||||
defer os.RemoveAll(dir)
|
defer os.RemoveAll(dir)
|
||||||
|
|
||||||
@ -115,7 +115,7 @@ func TestFileSystemDeletePrefix(t *testing.T) {
|
|||||||
t.Fatal("Expected error, got nil", errs)
|
t.Fatal("Expected error, got nil", errs)
|
||||||
}
|
}
|
||||||
|
|
||||||
if errs := fs.DeletePrefix("missing/"); len(errs) != 0 {
|
if errs := fs.DeletePrefix("missing"); len(errs) != 0 {
|
||||||
t.Fatalf("Not existing prefix shouldn't error, got %v", errs)
|
t.Fatalf("Not existing prefix shouldn't error, got %v", errs)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -123,13 +123,50 @@ func TestFileSystemDeletePrefix(t *testing.T) {
|
|||||||
t.Fatalf("Expected nil, got errors %v", errs)
|
t.Fatalf("Expected nil, got errors %v", errs)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ensure that the test/ files are deleted
|
// ensure that the test/* files are deleted
|
||||||
if exists, _ := fs.Exists("test/sub1.txt"); exists {
|
if exists, _ := fs.Exists("test/sub1.txt"); exists {
|
||||||
t.Fatalf("Expected test/sub1.txt to be deleted")
|
t.Fatalf("Expected test/sub1.txt to be deleted")
|
||||||
}
|
}
|
||||||
if exists, _ := fs.Exists("test/sub2.txt"); exists {
|
if exists, _ := fs.Exists("test/sub2.txt"); exists {
|
||||||
t.Fatalf("Expected test/sub2.txt to be deleted")
|
t.Fatalf("Expected test/sub2.txt to be deleted")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// the test directory should remain since the prefix didn't have trailing slash
|
||||||
|
if _, err := os.Stat(filepath.Join(dir, "test")); os.IsNotExist(err) {
|
||||||
|
t.Fatal("Expected the prefix dir to remain")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFileSystemDeletePrefixWithTrailingSlash(t *testing.T) {
|
||||||
|
dir := createTestDir(t)
|
||||||
|
defer os.RemoveAll(dir)
|
||||||
|
|
||||||
|
fs, err := filesystem.NewLocal(dir)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer fs.Close()
|
||||||
|
|
||||||
|
if errs := fs.DeletePrefix("missing/"); len(errs) != 0 {
|
||||||
|
t.Fatalf("Not existing prefix shouldn't error, got %v", errs)
|
||||||
|
}
|
||||||
|
|
||||||
|
if errs := fs.DeletePrefix("test/"); len(errs) != 0 {
|
||||||
|
t.Fatalf("Expected nil, got errors %v", errs)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ensure that the test/* files are deleted
|
||||||
|
if exists, _ := fs.Exists("test/sub1.txt"); exists {
|
||||||
|
t.Fatalf("Expected test/sub1.txt to be deleted")
|
||||||
|
}
|
||||||
|
if exists, _ := fs.Exists("test/sub2.txt"); exists {
|
||||||
|
t.Fatalf("Expected test/sub2.txt to be deleted")
|
||||||
|
}
|
||||||
|
|
||||||
|
// the test directory should be also deleted since the prefix has trailing slash
|
||||||
|
if _, err := os.Stat(filepath.Join(dir, "test")); !os.IsNotExist(err) {
|
||||||
|
t.Fatal("Expected the prefix dir to be deleted")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFileSystemUploadMultipart(t *testing.T) {
|
func TestFileSystemUploadMultipart(t *testing.T) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user