aboutsummaryrefslogtreecommitdiff
path: root/pkg/database/localfs/filesystem.go
blob: c7c645817235c7c6b2f3fa2e5408bcc9e8caa63d (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
48
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)
}