diff options
Diffstat (limited to 'pkg/database/localfs')
-rw-r--r-- | pkg/database/localfs/filesystem.go | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/pkg/database/localfs/filesystem.go b/pkg/database/localfs/filesystem.go new file mode 100644 index 0000000..c7c6458 --- /dev/null +++ b/pkg/database/localfs/filesystem.go @@ -0,0 +1,49 @@ +package localfs + +import ( + "io/fs" + "os" + "path" + "strings" +) + +type FileSystemRepository struct { + root string +} + +func NewFileSystemRepository(root string) *FileSystemRepository { + return &FileSystemRepository{ + root: root, + } +} + +func (self *FileSystemRepository) getFilesFromPath(filepath string) ([]fs.FileInfo, error) { + dirs, err := os.ReadDir(filepath) + if err != nil { + return nil, err + } + + infos := make([]fs.FileInfo, 0, len(dirs)) + for _, dir := range dirs { + if strings.HasPrefix(dir.Name(), ".") { + continue + } + info, err := dir.Info() + if err != nil { + return nil, err + } + infos = append(infos, info) + } + + return infos, nil +} + +func (self *FileSystemRepository) List(filepath string) ([]fs.FileInfo, error) { + workingPath := path.Join(self.root, filepath) + return self.getFilesFromPath(workingPath) +} + +func (self *FileSystemRepository) Stat(filepath string) (fs.FileInfo, error) { + workingPath := path.Join(self.root, filepath) + return os.Stat(workingPath) +} |