From 4d38bc3e0e50cb4c3a4f40149522443b3d7b54f7 Mon Sep 17 00:00:00 2001 From: Nour Balaha Date: Tue, 23 Apr 2024 13:18:17 +0900 Subject: [PATCH] fix(server): remove the root file from asset files (#1134) * fix: filter the root file when finding file by id * fix: error checking if statement * fix: error checking in aws file * fix: update the commented code * move the filter logic to pkg and add a test --- server/internal/infrastructure/aws/file.go | 3 +-- server/internal/infrastructure/gcp/file.go | 3 +-- server/pkg/asset/file.go | 4 +++- server/pkg/asset/file_test.go | 13 +++++++++++++ 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/server/internal/infrastructure/aws/file.go b/server/internal/infrastructure/aws/file.go index b846d8e816..944f08ac82 100644 --- a/server/internal/infrastructure/aws/file.go +++ b/server/internal/infrastructure/aws/file.go @@ -48,8 +48,7 @@ func NewFile(ctx context.Context, bucketName, baseURL, cacheControl string) (gat baseURL = fmt.Sprintf("https://%s.s3.amazonaws.com/", bucketName) } - var err error - u, _ = url.Parse(baseURL) + u, err := url.Parse(baseURL) if err != nil { return nil, rerror.NewE(i18n.T("invalid base URL")) } diff --git a/server/internal/infrastructure/gcp/file.go b/server/internal/infrastructure/gcp/file.go index 3194ce9485..1907813a87 100644 --- a/server/internal/infrastructure/gcp/file.go +++ b/server/internal/infrastructure/gcp/file.go @@ -42,8 +42,7 @@ func NewFile(bucketName, base, cacheControl string) (gateway.File, error) { base = fmt.Sprintf("https://storage.googleapis.com/%s", bucketName) } - var err error - u, _ = url.Parse(base) + u, err := url.Parse(base) if err != nil { return nil, rerror.NewE(i18n.T("invalid base URL")) } diff --git a/server/pkg/asset/file.go b/server/pkg/asset/file.go index f127c01bfc..5a800dc550 100644 --- a/server/pkg/asset/file.go +++ b/server/pkg/asset/file.go @@ -61,7 +61,9 @@ func (f *File) Files() []*File { } func (f *File) SetFiles(s []*File) { - f.files = slices.Clone(s) + f.files = lo.Filter(s, func(af *File, _ int) bool { + return af.Path() != f.Path() + }) } func (f *File) FilePaths() []string { diff --git a/server/pkg/asset/file_test.go b/server/pkg/asset/file_test.go index aadd168aab..414f3f71c6 100644 --- a/server/pkg/asset/file_test.go +++ b/server/pkg/asset/file_test.go @@ -70,6 +70,19 @@ func TestFile_Files(t *testing.T) { }, f.FlattenChildren()) } +func TestFile_SetFiles(t *testing.T) { + root := NewFile().Build() + files := []*File{NewFile().Path("aaa/a/a.txt").Build(), NewFile().Path("aaa/b.txt").Build()} + root.SetFiles(files) + assert.Equal(t, files, root.files) + + root2 := NewFile().Path("aaa.zip").Build() + files2 := []*File{NewFile().Path("aaa.zip").Build(), NewFile().Path("aaa/a/a.txt").Build(), NewFile().Path("aaa/b.txt").Build()} + expected := []*File{NewFile().Path("aaa/a/a.txt").Build(), NewFile().Path("aaa/b.txt").Build()} + root2.SetFiles(files2) + assert.Equal(t, expected, root2.files) +} + func Test_FoldFiles(t *testing.T) { assert.Equal(t, &File{