Преглед изворни кода

Add ListDir comment + filestorage ListDir

Signed-off-by: Kaelan Patel <kaelanspatel@gmail.com>
Kaelan Patel пре 3 година
родитељ
комит
5f912ec862
2 измењених фајлова са 28 додато и 2 уклоњено
  1. 25 2
      pkg/storage/filestorage.go
  2. 3 0
      pkg/storage/storage.go

+ 25 - 2
pkg/storage/filestorage.go

@@ -1,7 +1,6 @@
 package storage
 
 import (
-	"fmt"
 	gofs "io/fs"
 	"io/ioutil"
 	"os"
@@ -62,7 +61,15 @@ func (fs *FileStorage) List(path string) ([]*StorageInfo, error) {
 }
 
 func (fs *FileStorage) ListDirectories(path string) ([]*StorageInfo, error) {
-	return []*StorageInfo{}, fmt.Errorf("ListDirectories not supported for filestorage")
+	p := gopath.Join(fs.baseDir, path)
+
+	// Read files in the backup path
+	files, err := ioutil.ReadDir(p)
+	if err != nil {
+		return nil, err
+	}
+
+	return DirFilesToStorageInfo(files, path), nil
 }
 
 // Read uses the relative path of the storage combined with the provided path to
@@ -152,3 +159,19 @@ func FileToStorageInfo(fileInfo gofs.FileInfo) *StorageInfo {
 		ModTime: fileInfo.ModTime(),
 	}
 }
+
+// DirFilesToStorageInfo maps a []fs.FileInfo to []*storage.StorageInfo
+// but only returning StorageInfo for directories
+func DirFilesToStorageInfo(fileInfo []gofs.FileInfo, path string) []*StorageInfo {
+	var stats []*StorageInfo
+	for _, info := range fileInfo {
+		if info.IsDir() {
+			stats = append(stats, &StorageInfo{
+				Name:    filepath.Join(path, info.Name()),
+				Size:    info.Size(),
+				ModTime: info.ModTime(),
+			})
+		}
+	}
+	return stats
+}

+ 3 - 0
pkg/storage/storage.go

@@ -52,6 +52,9 @@ type Storage interface {
 	// storage information for the files.
 	List(path string) ([]*StorageInfo, error)
 
+	// ListDirectories uses the relative path of the storage combined with the provided path
+	// to return storage information for only directories contained along the path. This
+	// functions as List, but returns storage information for only directories.
 	ListDirectories(path string) ([]*StorageInfo, error)
 }