package storage import ( "errors" "fmt" "io" "io/ioutil" "os" "path/filepath" "porg/pipe" ) type FileSystem struct { } 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, err := file.Stat() if err != nil { fmt.Println("@@@@ ", err.Error()) return nil } switch walkMode { case Folder: if fileInfo.IsDir() { c <- path } case File: if !fileInfo.IsDir() { c <- path } case FileFolder: c <- path } return nil }) close(c) }(folder, c) 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) }