aboutsummaryrefslogtreecommitdiff
path: root/storage/storage_fs.go
diff options
context:
space:
mode:
authorGabriel Arakaki Giovanini <mail@gabrielgio.me>2022-10-16 19:13:41 +0200
committerGabriel Arakaki Giovanini <mail@gabrielgio.me>2022-10-16 19:16:35 +0200
commiteb1b7d7d9149114eb6b4287b7cb40c49dccfb26e (patch)
tree12f797651abeda9512d56d8922199ae9edb2a293 /storage/storage_fs.go
parent98844247a424558939228b82e9b5f28d723c4fe0 (diff)
downloadporg-master.tar.gz
porg-master.tar.bz2
porg-master.zip
feat: Add storage interfaceHEADmaster
With this is easier to interact with storage layers.
Diffstat (limited to 'storage/storage_fs.go')
-rw-r--r--storage/storage_fs.go86
1 files changed, 83 insertions, 3 deletions
diff --git a/storage/storage_fs.go b/storage/storage_fs.go
index 35ce58b..16bf8e8 100644
--- a/storage/storage_fs.go
+++ b/storage/storage_fs.go
@@ -1,22 +1,38 @@
package storage
import (
+ "errors"
+ "fmt"
+ "io"
+ "io/ioutil"
"os"
"path/filepath"
+ "porg/pipe"
)
type FileSystem struct {
- root string
}
-func WalkFolder(folder string, walkMode WalkMode) <-chan string {
+func NewFileSystem() *FileSystem {
+ return &FileSystem{}
+}
+
+func (fs *FileSystem) Get(path string) (io.Reader, error) {
+ return os.Open(path)
+}
+
+func (fs *FileSystem) Walk(folder string, walkMode WalkMode) <-chan string {
c := make(chan string)
go func(folder string, c chan string) {
filepath.Walk(folder, func(path string, info os.FileInfo, err error) error {
file, _ := os.Open(path)
defer file.Close()
- fileInfo, _ := file.Stat()
+ fileInfo, err := file.Stat()
+ if err != nil {
+ fmt.Println("@@@@ ", err.Error())
+ return nil
+ }
switch walkMode {
case Folder:
@@ -38,3 +54,67 @@ func WalkFolder(folder string, walkMode WalkMode) <-chan string {
return c
}
+
+func (fs *FileSystem) Put(path string, f io.Reader) error {
+ fo, err := os.Create(path)
+ if err != nil {
+ return err
+ }
+
+ if _, err := io.Copy(fo, f); err != nil {
+ return err
+ }
+
+ return nil
+}
+
+func (fs *FileSystem) List(path string) ([]string, error) {
+ files, err := ioutil.ReadDir(path)
+ if err != err {
+ return nil, err
+ }
+
+ findPath := func(f os.FileInfo) string {
+ return path + "/" + f.Name()
+ }
+ return pipe.Map(findPath, files), nil
+}
+
+func (fs *FileSystem) Move(src string, dst string) error {
+ return os.Rename(src, dst)
+}
+
+func (fs *FileSystem) Copy(src string, dst string) error {
+ in, err := os.Open(src)
+ if err != nil {
+ return err
+ }
+ defer in.Close()
+
+ out, err := os.Create(dst)
+ if err != nil {
+ return err
+ }
+ defer out.Close()
+
+ _, err = io.Copy(out, in)
+ if err != nil {
+ return err
+ }
+ return out.Close()
+}
+
+func (fs *FileSystem) Exists(path string) (bool, error) {
+ _, err := os.Stat(path)
+ if err == nil {
+ return true, nil
+ }
+ if errors.Is(err, os.ErrNotExist) {
+ return false, nil
+ }
+ return false, err
+}
+
+func (fs *FileSystem) Mkdir(path string) error {
+ return os.MkdirAll(path, 0777)
+}