aboutsummaryrefslogtreecommitdiff
path: root/storage
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 /storage
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 'storage')
-rw-r--r--storage/storage.go30
-rw-r--r--storage/storage_fs.go40
-rw-r--r--storage/storage_fs_test.go29
3 files changed, 99 insertions, 0 deletions
diff --git a/storage/storage.go b/storage/storage.go
new file mode 100644
index 0000000..b788efb
--- /dev/null
+++ b/storage/storage.go
@@ -0,0 +1,30 @@
+package storage
+
+import (
+ "crypto/sha256"
+ "fmt"
+ "io"
+)
+
+type WalkMode int
+
+const (
+ Folder WalkMode = iota
+ File
+ FileFolder
+)
+
+type Storage interface {
+ Walk(path string, walkMode WalkMode) <-chan string
+ Get(path string) (io.Reader, error)
+}
+
+func CalculateSHA256(r io.Reader) (string, error) {
+ h := sha256.New()
+ if _, err := io.Copy(h, r); err != nil {
+ return "", err
+ }
+
+ return fmt.Sprintf("%x", h.Sum(nil)), nil
+
+}
diff --git a/storage/storage_fs.go b/storage/storage_fs.go
new file mode 100644
index 0000000..35ce58b
--- /dev/null
+++ b/storage/storage_fs.go
@@ -0,0 +1,40 @@
+package storage
+
+import (
+ "os"
+ "path/filepath"
+)
+
+type FileSystem struct {
+ root string
+}
+
+func WalkFolder(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()
+
+ 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
+}
diff --git a/storage/storage_fs_test.go b/storage/storage_fs_test.go
new file mode 100644
index 0000000..b746c7e
--- /dev/null
+++ b/storage/storage_fs_test.go
@@ -0,0 +1,29 @@
+package storage
+
+import (
+ "porg/testutil"
+ "testing"
+)
+
+func TestWalk(t *testing.T) {
+ fileCount := 1000
+ folder := testutil.CreateFolder()
+ files := map[string]struct{}{}
+ walkedFiles := map[string]struct{}{}
+
+ for i := 0; i < fileCount; i++ {
+ files[testutil.AppendEmptyFile(folder)] = struct{}{}
+ }
+
+ c := WalkFolder(folder, File)
+ for file := range c {
+ walkedFiles[file] = struct{}{}
+ }
+
+ for k := range files {
+ _, ok := walkedFiles[k]
+ if !ok {
+ t.Errorf("File %s was not walked", k)
+ }
+ }
+}