Skip to content

Commit d105f8f

Browse files
authored
bug: error with cloudstorage.ErrObjectNotFound for dirs (#98)
this matches the behavior of the google provider
1 parent adf3d2f commit d105f8f

3 files changed

Lines changed: 77 additions & 6 deletions

File tree

localfs/store.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,25 @@ func (l *LocalStore) Folders(ctx context.Context, csq cloudstorage.Query) ([]str
219219
func (l *LocalStore) NewReader(o string) (io.ReadCloser, error) {
220220
return l.NewReaderWithContext(context.Background(), o)
221221
}
222-
func (l *LocalStore) NewReaderWithContext(ctx context.Context, o string) (io.ReadCloser, error) {
222+
func (l *LocalStore) pathForObject(o string) (string, error) {
223223
fo := path.Join(l.storepath, o)
224224
if !cloudstorage.Exists(fo) {
225-
return nil, cloudstorage.ErrObjectNotFound
225+
return "", cloudstorage.ErrObjectNotFound
226+
}
227+
stat, err := os.Stat(fo)
228+
if err != nil {
229+
return "", err
230+
}
231+
if stat.IsDir() {
232+
return "", cloudstorage.ErrObjectNotFound
233+
}
234+
return fo, nil
235+
}
236+
237+
func (l *LocalStore) NewReaderWithContext(ctx context.Context, o string) (io.ReadCloser, error) {
238+
fo, err := l.pathForObject(o)
239+
if err != nil {
240+
return nil, err
226241
}
227242
return csbufio.OpenReader(fo)
228243
}
@@ -260,11 +275,11 @@ func (l *LocalStore) NewWriterWithContext(ctx context.Context, o string, metadat
260275
}
261276

262277
func (l *LocalStore) Get(ctx context.Context, o string) (cloudstorage.Object, error) {
263-
fo := path.Join(l.storepath, o)
264-
265-
if !cloudstorage.Exists(fo) {
266-
return nil, cloudstorage.ErrObjectNotFound
278+
fo, err := l.pathForObject(o)
279+
if err != nil {
280+
return nil, err
267281
}
282+
268283
var updated time.Time
269284
if stat, err := os.Stat(fo); err == nil {
270285
updated = stat.ModTime()

localfs/store_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package localfs_test
22

33
import (
4+
"context"
45
"os"
56
"testing"
67

@@ -51,3 +52,39 @@ func TestAll(t *testing.T) {
5152
assert.NotEqual(t, nil, err)
5253
assert.Equal(t, nil, store)
5354
}
55+
56+
func TestNewReaderDir(t *testing.T) {
57+
// When a dir is requested, serve the index.html file instead
58+
localFsConf := &cloudstorage.Config{
59+
Type: localfs.StoreType,
60+
AuthMethod: localfs.AuthFileSystem,
61+
LocalFS: "/tmp/mockcloud",
62+
TmpDir: "/tmp/localcache",
63+
}
64+
store, err := cloudstorage.NewStore(localFsConf)
65+
testutils.MockFile(store, "test/index.html", "test")
66+
assert.Equal(t, nil, err)
67+
assert.Equal(t, nil, err)
68+
_, err = store.NewReader("test")
69+
assert.Equal(t, err, cloudstorage.ErrObjectNotFound)
70+
err = store.Delete(context.Background(), "test/index.html")
71+
assert.Equal(t, nil, err)
72+
}
73+
74+
func TestGetDir(t *testing.T) {
75+
// When a dir is requested, serve the index.html file instead
76+
localFsConf := &cloudstorage.Config{
77+
Type: localfs.StoreType,
78+
AuthMethod: localfs.AuthFileSystem,
79+
LocalFS: "/tmp/mockcloud",
80+
TmpDir: "/tmp/localcache",
81+
}
82+
store, err := cloudstorage.NewStore(localFsConf)
83+
testutils.MockFile(store, "test/index.html", "test")
84+
assert.Equal(t, nil, err)
85+
assert.Equal(t, nil, err)
86+
_, err = store.Get(context.Background(), "test")
87+
assert.Equal(t, err, cloudstorage.ErrObjectNotFound)
88+
err = store.Delete(context.Background(), "test/index.html")
89+
assert.Equal(t, nil, err)
90+
}

testutils/testutils.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -903,3 +903,22 @@ func MultipleRW(t TestingT, store cloudstorage.Store, conf *cloudstorage.Config)
903903
assert.Equal(t, nil, err)
904904
}
905905
}
906+
907+
func MockFile(store cloudstorage.Store, path string, body string) error {
908+
obj, err := store.NewObject(path)
909+
if err != nil {
910+
return err
911+
}
912+
f, err := obj.Open(cloudstorage.ReadWrite)
913+
if err != nil {
914+
return err
915+
}
916+
w := bufio.NewWriter(f)
917+
918+
if _, err := w.WriteString(body); err != nil {
919+
return err
920+
}
921+
w.Flush()
922+
obj.Close()
923+
return nil
924+
}

0 commit comments

Comments
 (0)