aboutsummaryrefslogtreecommitdiff
path: root/pkg/service/filesystem.go
blob: 34eda61b9fb8a7c22b9cf1b89134b627b49d06d9 (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package service

import (
	"context"
	"io/fs"
	"net/url"
	"path"
	"strings"
	"syscall"

	"git.sr.ht/~gabrielgio/img/pkg/database/repository"
	"git.sr.ht/~gabrielgio/img/pkg/list"
)

type (
	FileSystemController struct {
		fsRepository   repository.FileSystemRepository
		userRepository repository.UserRepository
	}

	DirectoryParam struct {
		Name           string
		UrlEncodedPath string
	}

	FileParam struct {
		UrlEncodedPath string
		Info           fs.FileInfo
	}

	Page struct {
		History []*DirectoryParam
		Files   []*FileParam
	}
)

func (f *FileParam) GetUid() int {
	if stat, ok := f.Info.Sys().(*syscall.Stat_t); ok {
		return int(stat.Uid)
	}
	return 0
}

func (f *FileParam) GetGid() int {
	if stat, ok := f.Info.Sys().(*syscall.Stat_t); ok {
		return int(stat.Gid)
	}
	return 0
}

func NewFileSystemController(
	fsRepository repository.FileSystemRepository,
	userRepository repository.UserRepository,
) *FileSystemController {
	return &FileSystemController{
		fsRepository:   fsRepository,
		userRepository: userRepository,
	}
}

func getHistory(filepath string) []*DirectoryParam {
	var (
		paths  = strings.Split(filepath, "/")
		result = make([]*DirectoryParam, 0, len(paths))
		acc    = ""
	)

	// add root folder
	result = append(result, &DirectoryParam{
		Name:           "...",
		UrlEncodedPath: "",
	})

	if len(paths) == 1 && paths[0] == "" {
		return result
	}

	for _, p := range paths {
		acc = path.Join(acc, p)
		result = append(result, &DirectoryParam{
			Name:           p,
			UrlEncodedPath: url.QueryEscape(acc),
		})
	}
	return result
}

func (f *FileSystemController) GetFullpath(ctx context.Context, userID uint, filepath string) (string, error) {
	userPath, err := f.userRepository.GetPathFromUserID(ctx, userID)
	if err != nil {
		return "", err
	}

	return path.Join(userPath, filepath), nil
}

func (f *FileSystemController) IsFile(ctx context.Context, fullPath string) (bool, error) {
	inf, err := f.fsRepository.Stat(fullPath)
	if err != nil {
		return false, err
	}

	return !inf.IsDir(), nil
}

func (f *FileSystemController) GetPage(ctx context.Context, filename string, fullPath string) (*Page, error) {
	files, err := f.fsRepository.List(fullPath)
	if err != nil {
		return nil, err
	}
	params := list.Map(files, func(info fs.FileInfo) *FileParam {
		fullPath := path.Join(filename, info.Name())
		scapedFullPath := url.QueryEscape(fullPath)
		return &FileParam{
			Info:           info,
			UrlEncodedPath: scapedFullPath,
		}
	})

	return &Page{
		Files:   params,
		History: getHistory(filename),
	}, nil
}