Compare commits

...

4 commits

4 changed files with 167 additions and 22 deletions

16
go.mod
View file

@ -2,7 +2,10 @@ module egtyl.xyz/omnibill/fsys
go 1.23.2 go 1.23.2
require github.com/minio/minio-go/v7 v7.0.80 require (
github.com/minio/minio-go/v7 v7.0.80
github.com/stretchr/testify v1.9.0
)
require ( require (
github.com/davecgh/go-spew v1.1.1 // indirect github.com/davecgh/go-spew v1.1.1 // indirect
@ -11,14 +14,13 @@ require (
github.com/goccy/go-json v0.10.3 // indirect github.com/goccy/go-json v0.10.3 // indirect
github.com/google/uuid v1.6.0 // indirect github.com/google/uuid v1.6.0 // indirect
github.com/klauspost/compress v1.17.11 // indirect github.com/klauspost/compress v1.17.11 // indirect
github.com/klauspost/cpuid/v2 v2.2.8 // indirect github.com/klauspost/cpuid/v2 v2.2.9 // indirect
github.com/minio/md5-simd v1.1.2 // indirect github.com/minio/md5-simd v1.1.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rs/xid v1.6.0 // indirect github.com/rs/xid v1.6.0 // indirect
github.com/stretchr/testify v1.9.0 // indirect golang.org/x/crypto v0.29.0 // indirect
golang.org/x/crypto v0.28.0 // indirect golang.org/x/net v0.31.0 // indirect
golang.org/x/net v0.30.0 // indirect golang.org/x/sys v0.27.0 // indirect
golang.org/x/sys v0.26.0 // indirect golang.org/x/text v0.20.0 // indirect
golang.org/x/text v0.19.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect
) )

22
go.sum
View file

@ -11,8 +11,8 @@ github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
github.com/klauspost/compress v1.17.11 h1:In6xLpyWOi1+C7tXUUWv2ot1QvBjxevKAaI6IXrJmUc= github.com/klauspost/compress v1.17.11 h1:In6xLpyWOi1+C7tXUUWv2ot1QvBjxevKAaI6IXrJmUc=
github.com/klauspost/compress v1.17.11/go.mod h1:pMDklpSncoRMuLFrf1W9Ss9KT+0rH90U12bZKk7uwG0= github.com/klauspost/compress v1.17.11/go.mod h1:pMDklpSncoRMuLFrf1W9Ss9KT+0rH90U12bZKk7uwG0=
github.com/klauspost/cpuid/v2 v2.0.1/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= github.com/klauspost/cpuid/v2 v2.0.1/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.2.8 h1:+StwCXwm9PdpiEkPyzBXIy+M9KUb4ODm0Zarf1kS5BM= github.com/klauspost/cpuid/v2 v2.2.9 h1:66ze0taIn2H33fBvCkXuv9BmCwDfafmiIVpKV9kKGuY=
github.com/klauspost/cpuid/v2 v2.2.8/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= github.com/klauspost/cpuid/v2 v2.2.9/go.mod h1:rqkxqrZ1EhYM9G+hXH7YdowN5R5RGN6NK4QwQ3WMXF8=
github.com/minio/md5-simd v1.1.2 h1:Gdi1DZK69+ZVMoNHRXJyNcxrMA4dSxoYHZSQbirFg34= github.com/minio/md5-simd v1.1.2 h1:Gdi1DZK69+ZVMoNHRXJyNcxrMA4dSxoYHZSQbirFg34=
github.com/minio/md5-simd v1.1.2/go.mod h1:MzdKDxYpY2BT9XQFocsiZf/NKVtR7nkE4RoEpN+20RM= github.com/minio/md5-simd v1.1.2/go.mod h1:MzdKDxYpY2BT9XQFocsiZf/NKVtR7nkE4RoEpN+20RM=
github.com/minio/minio-go/v7 v7.0.80 h1:2mdUHXEykRdY/BigLt3Iuu1otL0JTogT0Nmltg0wujk= github.com/minio/minio-go/v7 v7.0.80 h1:2mdUHXEykRdY/BigLt3Iuu1otL0JTogT0Nmltg0wujk=
@ -23,15 +23,15 @@ github.com/rs/xid v1.6.0 h1:fV591PaemRlL6JfRxGDEPl69wICngIQ3shQtzfy2gxU=
github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0= github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw= golang.org/x/crypto v0.29.0 h1:L5SG1JTTXupVV3n6sUqMTeWbjAyfPwoda2DLX8J8FrQ=
golang.org/x/crypto v0.28.0/go.mod h1:rmgy+3RHxRZMyY0jjAJShp2zgEdOqj2AO7U0pYmeQ7U= golang.org/x/crypto v0.29.0/go.mod h1:+F4F4N5hv6v38hfeYwTdx20oUvLLc+QfrE9Ax9HtgRg=
golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4= golang.org/x/net v0.31.0 h1:68CPQngjLL0r2AlUKiSxtQFKvzRVbnzLwMUn5SzcLHo=
golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU= golang.org/x/net v0.31.0/go.mod h1:P4fl1q7dY2hnZFxEk4pPSkDHF+QqjitcnDjUQyMM+pM=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo= golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug=
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM= golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4=
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View file

@ -19,6 +19,7 @@ var (
ErrInvalidStorageType = errors.New("invalid storage type") ErrInvalidStorageType = errors.New("invalid storage type")
ErrInvalidPath = errors.New("invalid path") ErrInvalidPath = errors.New("invalid path")
ErrFileNotFound = errors.New("file not found") ErrFileNotFound = errors.New("file not found")
ErrFolderNotFound = errors.New("folder not found")
) )
type Storage struct { type Storage struct {
@ -182,6 +183,17 @@ func (s *Storage) Write(name string, data []byte, opts WriteOptions) error {
} }
return nil return nil
} else { } else {
if fileInfo, err := os.Stat(filepath.Join(s.config.Path, filepath.Dir(name))); err != nil && errors.Is(err, fs.ErrNotExist) {
if err := os.MkdirAll(filepath.Join(s.config.Path, filepath.Dir(name)), 0740); err != nil {
return err
}
} else if err != nil {
return err
} else {
if !fileInfo.IsDir() {
return ErrInvalidPath
}
}
return os.WriteFile(filepath.Join(s.config.Path, name), data, 0600) return os.WriteFile(filepath.Join(s.config.Path, name), data, 0600)
} }
} }
@ -205,7 +217,8 @@ type FileInfo struct {
Size int64 Size int64
ModTime time.Time ModTime time.Time
IsDir bool IsDir bool
Mode os.FileMode // The file permissions, will always be 0777 when using S3.
Mode os.FileMode
} }
// Stat grabs the file info from the Storage interface. // Stat grabs the file info from the Storage interface.
@ -228,7 +241,7 @@ func (s *Storage) Stat(name string) (*FileInfo, error) {
fileInfo.Name = objInfo.Key fileInfo.Name = objInfo.Key
fileInfo.Size = objInfo.Size fileInfo.Size = objInfo.Size
fileInfo.ModTime = objInfo.LastModified fileInfo.ModTime = objInfo.LastModified
fileInfo.Mode = 0777 // Used as a fake perm. Doesn't actually do anything. fileInfo.Mode = fs.FileMode(0777) // Used as a fake perm. Doesn't actually do anything.
} else { } else {
fInfo, err := os.Stat(filepath.Join(s.config.Path, name)) fInfo, err := os.Stat(filepath.Join(s.config.Path, name))
if err != nil { if err != nil {
@ -273,3 +286,80 @@ func (s *Storage) Copy(name string, dest string) error {
return nil return nil
} }
} }
// Rename is a wrapper over the Move function.
func (s *Storage) Rename(name, dest string) error {
return s.Move(name, dest)
}
// Move moves a file from one place to another.
// It takes a file name, and the destination name as its parameter.
// The function returns an error, if any.
func (s *Storage) Move(name string, dest string) error {
if s.s3Client != nil {
if _, err := s.s3Client.CopyObject(s.ctx, minio.CopyDestOptions{
Bucket: s.config.S3BucketName,
Object: dest,
}, minio.CopySrcOptions{
Bucket: s.config.S3BucketName,
Object: name,
}); err != nil {
return err
}
if err := s.s3Client.RemoveObject(s.ctx, s.config.S3BucketName, name, minio.RemoveObjectOptions{}); err != nil {
return err
}
return nil
} else {
return os.Rename(filepath.Join(s.config.Path, name), filepath.Join(s.config.Path, dest))
}
}
// ReadDir reads all files within a certain directory.
// It takes a folder name as its parameter.
// The function returns an array of FileInfo, and an error, if any.
func (s *Storage) ReadDir(name string) ([]FileInfo, error) {
var fileInfo []FileInfo
if s.s3Client != nil {
for object := range s.s3Client.ListObjects(s.ctx, s.config.S3BucketName, minio.ListObjectsOptions{
Prefix: name + "/",
}) {
if object.Err != nil {
minioErr := minio.ToErrorResponse(object.Err)
if minioErr.Code == "NoSuchKey" {
return nil, ErrFolderNotFound
}
return nil, object.Err
}
fileInfo = append(fileInfo, FileInfo{
Name: object.Key,
Size: object.Size,
ModTime: object.LastModified,
Mode: fs.FileMode(0777),
IsDir: object.Size == 0,
})
}
} else {
dirInfo, err := os.ReadDir(filepath.Join(s.config.Path, name))
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
return nil, ErrFolderNotFound
}
return nil, err
}
for _, fInfo := range dirInfo {
info, err := fInfo.Info()
if err != nil {
return nil, err
}
fileInfo = append(fileInfo, FileInfo{
Name: info.Name(),
Size: info.Size(),
ModTime: info.ModTime(),
Mode: info.Mode(),
IsDir: info.IsDir(),
})
}
}
return fileInfo, nil
}

View file

@ -16,9 +16,16 @@ func TestStorageLocal(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
t.Log("== Write ==") t.Log("== Write ==")
err = stor.Write("test.txt", []byte("hi"), WriteOptions{}) err = stor.Write("test.txt", []byte("hi"), WriteOptions{})
assert.NoError(t, err) assert.NoError(t, err)
err = stor.Write("testing/1.txt", []byte("hi"), WriteOptions{})
assert.NoError(t, err)
err = stor.Write("testing/2.txt", []byte("hi"), WriteOptions{})
assert.NoError(t, err)
err = stor.Write("testing/test/2.txt", []byte("hi"), WriteOptions{})
assert.NoError(t, err)
t.Log("== Stat ==") t.Log("== Stat ==")
fileInfo, err := stor.Stat("test.txt") fileInfo, err := stor.Stat("test.txt")
assert.NoError(t, err) assert.NoError(t, err)
@ -31,6 +38,12 @@ func TestStorageLocal(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, []byte("hi"), fileContent) assert.Equal(t, []byte("hi"), fileContent)
t.Log("== ReadDir ==")
files, err := stor.ReadDir("testing")
assert.NoError(t, err)
assert.Equal(t, 3, len(files))
t.Log("== Copy ==") t.Log("== Copy ==")
err = stor.Copy("test.txt", "test2.txt") err = stor.Copy("test.txt", "test2.txt")
assert.NoError(t, err) assert.NoError(t, err)
@ -39,7 +52,17 @@ func TestStorageLocal(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, fs.FileMode(0600), copyFileInfo.Mode) assert.Equal(t, fs.FileMode(0600), copyFileInfo.Mode)
assert.Equal(t, "test2.txt", copyFileInfo.Name) assert.Equal(t, "test2.txt", copyFileInfo.Name)
assert.Equal(t, int64(len("hi")), fileInfo.Size) assert.Equal(t, int64(len("hi")), copyFileInfo.Size)
t.Log("== Move ==")
err = stor.Move("test2.txt", "test3.txt")
assert.NoError(t, err)
moveFileInfo, err := stor.Stat("test3.txt")
assert.NoError(t, err)
assert.Equal(t, fs.FileMode(0600), moveFileInfo.Mode)
assert.Equal(t, "test3.txt", moveFileInfo.Name)
assert.Equal(t, int64(len("hi")), moveFileInfo.Size)
t.Log("== Open ==") t.Log("== Open ==")
file, err := stor.Open("test.txt") file, err := stor.Open("test.txt")
@ -71,10 +94,19 @@ func TestStorageS3(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
t.Log("== Write ==") t.Log("== Write ==")
err = stor.Write("test.txt", []byte("hi"), WriteOptions{}) err = stor.Write("test.txt", []byte("hi"), WriteOptions{})
assert.NoError(t, err) assert.NoError(t, err)
err = stor.Write("testing/1.txt", []byte("hi"), WriteOptions{})
assert.NoError(t, err)
err = stor.Write("testing/2.txt", []byte("hi"), WriteOptions{})
assert.NoError(t, err)
err = stor.Write("testing/test/2.txt", []byte("hi"), WriteOptions{})
assert.NoError(t, err)
t.Log("== Stat ==") t.Log("== Stat ==")
fileInfo, err := stor.Stat("test.txt") fileInfo, err := stor.Stat("test.txt")
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, "test.txt", fileInfo.Name) assert.Equal(t, "test.txt", fileInfo.Name)
@ -82,11 +114,19 @@ func TestStorageS3(t *testing.T) {
assert.Equal(t, fs.FileMode(0777), fileInfo.Mode) assert.Equal(t, fs.FileMode(0777), fileInfo.Mode)
t.Log("== Read ==") t.Log("== Read ==")
fileContent, err := stor.Read("test.txt") fileContent, err := stor.Read("test.txt")
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, []byte("hi"), fileContent) assert.Equal(t, []byte("hi"), fileContent)
t.Log("== ReadDir ==")
files, err := stor.ReadDir("testing")
assert.NoError(t, err)
assert.Equal(t, 3, len(files))
t.Log("== Copy ==") t.Log("== Copy ==")
err = stor.Copy("test.txt", "test2.txt") err = stor.Copy("test.txt", "test2.txt")
assert.NoError(t, err) assert.NoError(t, err)
@ -94,9 +134,21 @@ func TestStorageS3(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, fs.FileMode(0777), copyFileInfo.Mode) assert.Equal(t, fs.FileMode(0777), copyFileInfo.Mode)
assert.Equal(t, "test2.txt", copyFileInfo.Name) assert.Equal(t, "test2.txt", copyFileInfo.Name)
assert.Equal(t, int64(len("hi")), fileInfo.Size) assert.Equal(t, int64(len("hi")), copyFileInfo.Size)
t.Log("== Move ==")
err = stor.Move("test2.txt", "test3.txt")
assert.NoError(t, err)
moveFileInfo, err := stor.Stat("test3.txt")
assert.NoError(t, err)
assert.Equal(t, fs.FileMode(0777), moveFileInfo.Mode)
assert.Equal(t, "test3.txt", moveFileInfo.Name)
assert.Equal(t, int64(len("hi")), moveFileInfo.Size)
t.Log("== Open ==") t.Log("== Open ==")
file, err := stor.Open("test.txt") file, err := stor.Open("test.txt")
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, "test.txt", file.Name) assert.Equal(t, "test.txt", file.Name)
@ -109,6 +161,7 @@ func TestStorageS3(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
t.Log("== Delete ==") t.Log("== Delete ==")
err = stor.Delete("test.txt") err = stor.Delete("test.txt")
assert.NoError(t, err) assert.NoError(t, err)