diff --git a/storage.go b/storage.go index 37654e5..6c87c61 100644 --- a/storage.go +++ b/storage.go @@ -7,6 +7,7 @@ import ( "github.com/minio/minio-go/v7" "github.com/minio/minio-go/v7/pkg/credentials" "io" + "io/fs" "net/http" "os" "path/filepath" @@ -68,8 +69,14 @@ func New(config *Config) (*Storage, error) { newStorage.s3Client = s3Client case "local": - if err := os.MkdirAll(config.Path, 0740); err != nil { + if statInfo, err := os.Stat(config.Path); err != nil && errors.Is(err, fs.ErrNotExist) { + if err := os.MkdirAll(config.Path, 0740); err != nil { + return nil, err + } + } else if err != nil { return nil, err + } else if statInfo.IsDir() { + return nil, ErrInvalidPath } default: return nil, ErrInvalidStorageType @@ -164,3 +171,14 @@ func (s *Storage) Write(name string, data []byte, opts WriteOptions) error { return os.WriteFile(filepath.Join(s.config.Path, name), data, 0640) } } + +func (s *Storage) Delete(name string) error { + if s.s3Client != nil { + if err := s.s3Client.RemoveObject(s.ctx, s.config.S3BucketName, name, minio.RemoveObjectOptions{}); err != nil { + return err + } + return nil + } else { + return os.Remove(filepath.Join(s.config.Path, name)) + } +} diff --git a/storage_test.go b/storage_test.go index 370e91a..700aa9e 100644 --- a/storage_test.go +++ b/storage_test.go @@ -1,7 +1,6 @@ package fsys import ( - "fmt" "github.com/stretchr/testify/assert" "io" "testing" @@ -36,6 +35,10 @@ func TestStorageLocal(t *testing.T) { err = file.Close() assert.NoError(t, err) + t.Log("== Delete ==") + err = stor.Delete("test.txt") + assert.NoError(t, err) + } func TestStorageS3(t *testing.T) { @@ -56,7 +59,6 @@ func TestStorageS3(t *testing.T) { t.Log("== Read ==") fileContent, err := stor.Read("test.txt") assert.NoError(t, err) - fmt.Println(fileContent) assert.Equal(t, []byte("hi"), fileContent) t.Log("== Open ==") @@ -71,4 +73,8 @@ func TestStorageS3(t *testing.T) { err = file.Close() assert.NoError(t, err) + t.Log("== Delete ==") + err = stor.Delete("test.txt") + assert.NoError(t, err) + }