aboutsummaryrefslogtreecommitdiff
path: root/fileop/fileop.go
diff options
context:
space:
mode:
authorGabriel Arakaki Giovanini <mail@gabrielgio.me>2022-09-10 17:33:30 +0200
committerGabriel Arakaki Giovanini <mail@gabrielgio.me>2022-09-10 17:33:30 +0200
commit3451d56ead6e57f503962b876c89284f1fb73a90 (patch)
tree172599f1f3acd77bc918c55403eb78255ced43e6 /fileop/fileop.go
parent544bbeeaf836436305cbed87ae1019511de62535 (diff)
downloadporg-3451d56ead6e57f503962b876c89284f1fb73a90.tar.gz
porg-3451d56ead6e57f503962b876c89284f1fb73a90.tar.bz2
porg-3451d56ead6e57f503962b876c89284f1fb73a90.zip
ref: Create a storage interface
This `Storage` interface will define all the interactions with the storage system. For now I plan to support native file system through go's standard library and Nextcloud through *webdav*. So this is the first step in that direction.
Diffstat (limited to 'fileop/fileop.go')
-rw-r--r--fileop/fileop.go46
1 files changed, 36 insertions, 10 deletions
diff --git a/fileop/fileop.go b/fileop/fileop.go
index d08cb82..1be3f2e 100644
--- a/fileop/fileop.go
+++ b/fileop/fileop.go
@@ -8,7 +8,15 @@ import (
"path/filepath"
)
-func WalkFolder(folder string) <-chan string {
+type WalkMode int
+
+const (
+ Folder WalkMode = iota
+ File
+ FileFolder
+)
+
+func WalkFolder(folder string, walkMode WalkMode) <-chan string {
c := make(chan string)
go func(folder string, c chan string) {
@@ -16,7 +24,17 @@ func WalkFolder(folder string) <-chan string {
file, _ := os.Open(path)
defer file.Close()
fileInfo, _ := file.Stat()
- if !fileInfo.IsDir() {
+
+ switch walkMode {
+ case Folder:
+ if fileInfo.IsDir() {
+ c <- path
+ }
+ case File:
+ if !fileInfo.IsDir() {
+ c <- path
+ }
+ case FileFolder:
c <- path
}
return nil
@@ -28,15 +46,9 @@ func WalkFolder(folder string) <-chan string {
return c
}
-func CalculateSHA256(file string) (string, error) {
- f, err := os.Open(file)
- if err != nil {
- return "", err
- }
- defer f.Close()
-
+func CalculateSHA256(r io.Reader) (string, error) {
h := sha256.New()
- if _, err := io.Copy(h, f); err != nil {
+ if _, err := io.Copy(h, r); err != nil {
return "", err
}
@@ -64,3 +76,17 @@ func Move() chan<- *MoveCommand {
return c
}
+
+func IsEmpty(name string) (bool, error) {
+ f, err := os.Open(name)
+ if err != nil {
+ return false, err
+ }
+ defer f.Close()
+
+ _, err = f.Readdirnames(1)
+ if err == io.EOF {
+ return true, nil
+ }
+ return false, err
+}