Skip to content

Commit

Permalink
fix(server): remove the root file from asset files (#1134)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
nourbalaha authored Apr 23, 2024
1 parent 71e388a commit 4d38bc3
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
3 changes: 1 addition & 2 deletions server/internal/infrastructure/aws/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
Expand Down
3 changes: 1 addition & 2 deletions server/internal/infrastructure/gcp/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
Expand Down
4 changes: 3 additions & 1 deletion server/pkg/asset/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 13 additions & 0 deletions server/pkg/asset/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit 4d38bc3

Please sign in to comment.