-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(storage): move s3fs to storage/fs/s3 and extract file types (#…
…2565) into storage/fs/blob package
- Loading branch information
Showing
9 changed files
with
244 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package blob | ||
|
||
import ( | ||
"io" | ||
"io/fs" | ||
) | ||
|
||
type Dir struct { | ||
*FileInfo | ||
} | ||
|
||
// ensure Dir implements fs.FileInfo | ||
var _ fs.FileInfo = &Dir{} | ||
|
||
func (d *Dir) Stat() (fs.FileInfo, error) { | ||
return d.FileInfo, nil | ||
} | ||
|
||
func (d *Dir) Read(p []byte) (n int, err error) { | ||
return 0, io.EOF | ||
} | ||
|
||
func (d *Dir) Close() error { | ||
return nil | ||
} | ||
|
||
func (d *Dir) IsDir() bool { | ||
return true | ||
} | ||
|
||
func (d *Dir) Mode() fs.FileMode { | ||
return fs.ModeDir | ||
} | ||
|
||
func NewDir(fileInfo *FileInfo) *Dir { | ||
return &Dir{fileInfo} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package blob | ||
|
||
import ( | ||
"io" | ||
"io/fs" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNewDir(t *testing.T) { | ||
fi := &FileInfo{} | ||
d := NewDir(fi) | ||
s, err := d.Stat() | ||
require.NoError(t, err) | ||
require.Equal(t, fi, s) | ||
require.True(t, d.IsDir()) | ||
require.Equal(t, fs.ModeDir, d.Mode()) | ||
n, err := d.Read([]byte{}) | ||
require.Equal(t, 0, n) | ||
require.Error(t, io.EOF, err) | ||
require.NoError(t, d.Close()) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package blob | ||
|
||
import ( | ||
"io" | ||
"io/fs" | ||
"time" | ||
) | ||
|
||
type File struct { | ||
bucket string | ||
key string | ||
length int64 | ||
body io.ReadCloser | ||
lastModified time.Time | ||
} | ||
|
||
// ensure File implements the fs.File interface | ||
var _ fs.File = &File{} | ||
|
||
func (f *File) Stat() (fs.FileInfo, error) { | ||
return &FileInfo{ | ||
name: f.key, | ||
size: f.length, | ||
modTime: f.lastModified, | ||
}, nil | ||
} | ||
|
||
func (f *File) Read(p []byte) (int, error) { | ||
return f.body.Read(p) | ||
} | ||
|
||
func (f *File) Close() error { | ||
return f.body.Close() | ||
} | ||
|
||
func NewFile(bucket string, key string, length int64, body io.ReadCloser, lastModified time.Time) *File { | ||
return &File{ | ||
bucket: bucket, | ||
key: key, | ||
length: length, | ||
body: body, | ||
lastModified: lastModified, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package blob | ||
|
||
import ( | ||
"io" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNewFile(t *testing.T) { | ||
modTime := time.Now() | ||
r := io.NopCloser(strings.NewReader("hello")) | ||
f := NewFile("bucket", "f.txt", 5, r, modTime) | ||
fi, err := f.Stat() | ||
require.NoError(t, err) | ||
require.Equal(t, "f.txt", fi.Name()) | ||
require.Equal(t, int64(5), fi.Size()) | ||
require.Equal(t, modTime, fi.ModTime()) | ||
buf := make([]byte, fi.Size()) | ||
n, err := f.Read(buf) | ||
require.NoError(t, err) | ||
require.Equal(t, 5, n) | ||
require.Equal(t, []byte("hello"), buf) | ||
err = f.Close() | ||
require.NoError(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package blob | ||
|
||
import ( | ||
"io/fs" | ||
"time" | ||
) | ||
|
||
// ensure FileInfo implements fs.FileInfo | ||
var _ fs.FileInfo = &FileInfo{} | ||
|
||
// ensure FileInfo implements fs.DirEntry | ||
var _ fs.DirEntry = &FileInfo{} | ||
|
||
type FileInfo struct { | ||
name string | ||
size int64 | ||
modTime time.Time | ||
isDir bool | ||
} | ||
|
||
func (fi *FileInfo) Name() string { | ||
return fi.name | ||
} | ||
|
||
func (fi *FileInfo) Size() int64 { | ||
return fi.size | ||
} | ||
|
||
func (fi *FileInfo) Type() fs.FileMode { | ||
return 0 | ||
} | ||
|
||
func (fi *FileInfo) Mode() fs.FileMode { | ||
return fs.ModePerm | ||
} | ||
|
||
func (fi *FileInfo) ModTime() time.Time { | ||
return fi.modTime | ||
} | ||
|
||
func (fi *FileInfo) IsDir() bool { | ||
return fi.isDir | ||
} | ||
func (fi *FileInfo) SetDir(v bool) { | ||
fi.isDir = v | ||
} | ||
|
||
func (fi *FileInfo) Sys() any { | ||
return nil | ||
} | ||
func (fi *FileInfo) Info() (fs.FileInfo, error) { | ||
return fi, nil | ||
} | ||
|
||
func NewFileInfo(name string, size int64, modTime time.Time) *FileInfo { | ||
return &FileInfo{ | ||
name: name, | ||
size: size, | ||
modTime: modTime, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package blob | ||
|
||
import ( | ||
"io/fs" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestFileInfo(t *testing.T) { | ||
modTime := time.Now() | ||
fi := NewFileInfo("f.txt", 100, modTime) | ||
require.Equal(t, fs.FileMode(0), fi.Type()) | ||
require.Equal(t, "f.txt", fi.Name()) | ||
require.Equal(t, int64(100), fi.Size()) | ||
require.Equal(t, modTime, fi.ModTime()) | ||
require.Equal(t, false, fi.isDir) | ||
info, err := fi.Info() | ||
require.NoError(t, err) | ||
require.Equal(t, fi, info) | ||
require.Nil(t, fi.Sys()) | ||
} | ||
|
||
func TestFileInfoIsDir(t *testing.T) { | ||
fi := FileInfo{} | ||
fi.SetDir(true) | ||
require.Equal(t, true, fi.isDir) | ||
} |
Oops, something went wrong.