aboutsummaryrefslogtreecommitdiff
path: root/pkg/database/localfs/filesystem.go
blob: d516ce9934ae8e951b2e6c3a4c80b6deb646bb43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package localfs

import (
	"io/fs"
	"os"
	"path"
	"strings"
)

type FileSystemRepository struct {
	root string
}

func NewFileSystemRepository() *FileSystemRepository {
	return &FileSystemRepository{}
}

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)
}