Skip to content

Commit

Permalink
fix(server): fs does not work on windows (#430)
Browse files Browse the repository at this point in the history
  • Loading branch information
yk-eukarya committed May 25, 2023
1 parent dacc3d4 commit 1890390
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
16 changes: 8 additions & 8 deletions server/internal/infrastructure/fs/file.go
Expand Up @@ -43,7 +43,7 @@ func (f *fileRepo) ReadAsset(ctx context.Context, filename string) (io.ReadClose
}

func (f *fileRepo) UploadAsset(ctx context.Context, file *file.File) (*url.URL, int64, error) {
filename := sanitize.Path(newAssetID() + path.Ext(file.Path))
filename := sanitize.Path(newAssetID() + filepath.Ext(file.Path))
size, err := f.upload(ctx, filepath.Join(assetDir, filename), file.Content)
if err != nil {
return nil, 0, err
Expand All @@ -59,7 +59,7 @@ func (f *fileRepo) RemoveAsset(ctx context.Context, u *url.URL) error {
if p == "" || f.urlBase == nil || u.Scheme != f.urlBase.Scheme || u.Host != f.urlBase.Host || path.Dir(p) != f.urlBase.Path {
return gateway.ErrInvalidFile
}
return f.delete(ctx, filepath.Join(assetDir, path.Base(p)))
return f.delete(ctx, filepath.Join(assetDir, filepath.Base(p)))
}

// plugin
Expand Down Expand Up @@ -102,7 +102,7 @@ func (f *fileRepo) RemoveBuiltScene(ctx context.Context, name string) error {

// helpers

func (f *fileRepo) read(ctx context.Context, filename string) (io.ReadCloser, error) {
func (f *fileRepo) read(_ context.Context, filename string) (io.ReadCloser, error) {
if filename == "" {
return nil, rerror.ErrNotFound
}
Expand All @@ -117,12 +117,12 @@ func (f *fileRepo) read(ctx context.Context, filename string) (io.ReadCloser, er
return file, nil
}

func (f *fileRepo) upload(ctx context.Context, filename string, content io.Reader) (int64, error) {
func (f *fileRepo) upload(_ context.Context, filename string, content io.Reader) (int64, error) {
if filename == "" {
return 0, gateway.ErrFailedToUploadFile
}

if fnd := path.Dir(filename); fnd != "" {
if fnd := filepath.Dir(filename); fnd != "" {
if err := f.fs.MkdirAll(fnd, 0755); err != nil {
return 0, rerror.ErrInternalBy(err)
}
Expand All @@ -144,12 +144,12 @@ func (f *fileRepo) upload(ctx context.Context, filename string, content io.Reade
return size, nil
}

func (f *fileRepo) move(ctx context.Context, from, dest string) error {
func (f *fileRepo) move(_ context.Context, from, dest string) error {
if from == "" || dest == "" || from == dest {
return gateway.ErrInvalidFile
}

if destd := path.Dir(dest); destd != "" {
if destd := filepath.Dir(dest); destd != "" {
if err := f.fs.MkdirAll(destd, 0755); err != nil {
return rerror.ErrInternalBy(err)
}
Expand All @@ -165,7 +165,7 @@ func (f *fileRepo) move(ctx context.Context, from, dest string) error {
return nil
}

func (f *fileRepo) delete(ctx context.Context, filename string) error {
func (f *fileRepo) delete(_ context.Context, filename string) error {
if filename == "" {
return gateway.ErrFailedToUploadFile
}
Expand Down
11 changes: 5 additions & 6 deletions server/internal/infrastructure/fs/file_test.go
Expand Up @@ -5,7 +5,6 @@ import (
"io"
"net/url"
"os"
"path"
"path/filepath"
"strings"
"testing"
Expand Down Expand Up @@ -56,9 +55,9 @@ func TestFile_UploadAsset(t *testing.T) {
assert.Equal(t, "https", u.Scheme)
assert.Equal(t, "example.com", u.Host)
assert.True(t, strings.HasPrefix(u.Path, "/assets/"))
assert.Equal(t, ".txt", path.Ext(u.Path))
assert.Equal(t, ".txt", filepath.Ext(u.Path))

uf, _ := fs.Open(filepath.Join("assets", path.Base(u.Path)))
uf, _ := fs.Open(filepath.Join("assets", filepath.Base(u.Path)))
c, _ := io.ReadAll(uf)
assert.Equal(t, "aaa", string(c))
}
Expand Down Expand Up @@ -251,9 +250,9 @@ func TestGetAssetFileURL(t *testing.T) {

func mockFs() afero.Fs {
files := map[string]string{
"assets/xxx.txt": "hello",
"plugins/aaa~1.0.0/foo.js": "bar",
"published/s.json": "{}",
filepath.Join("assets", "xxx.txt"): "hello",
filepath.Join("plugins", "aaa~1.0.0", "foo.js"): "bar",
filepath.Join("published", "s.json"): "{}",
}

fs := afero.NewMemMapFs()
Expand Down

0 comments on commit 1890390

Please sign in to comment.