Skip to content

Commit

Permalink
refactor(storage): move s3fs to storage/fs/s3 and extract file types (#…
Browse files Browse the repository at this point in the history
…2565)

into storage/fs/blob package
  • Loading branch information
erka authored Dec 19, 2023
1 parent c05a807 commit aa32f6d
Show file tree
Hide file tree
Showing 9 changed files with 244 additions and 124 deletions.
37 changes: 37 additions & 0 deletions internal/storage/fs/blob/dir.go
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}
}
24 changes: 24 additions & 0 deletions internal/storage/fs/blob/dir_test.go
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())

}
44 changes: 44 additions & 0 deletions internal/storage/fs/blob/file.go
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,
}
}
28 changes: 28 additions & 0 deletions internal/storage/fs/blob/file_test.go
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)
}
61 changes: 61 additions & 0 deletions internal/storage/fs/blob/fileinfo.go
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,
}
}
29 changes: 29 additions & 0 deletions internal/storage/fs/blob/fileinfo_test.go
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)
}
Loading

0 comments on commit aa32f6d

Please sign in to comment.