aboutsummaryrefslogtreecommitdiff
path: root/backend/studip/filetree.go
blob: 8f7c5c76a336447b3891d6915059e68d7f7bda7a (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package studip

import (
	"fmt"
	"sort"
	"strings"
	"time"

	"github.com/rclone/rclone/fs"
)

type Node struct {
	Children           []*Node
	Parent             *Node
	Name               string
	Path               string
	ID                 string
	IsReadable         bool
	IsWritable         bool
	IsDownloadable     bool
	IsEditable         bool
	IsSubfolderAllowed bool
	IsDir              bool
	ChDate             time.Time
	Size               int64
	ContentType        string
}

func (n *Node) String() string {
	if n == nil {
		return "<nil Node>"
	}

	parentPath := "<nil>"
	if n.Parent != nil {
		parentPath = n.Parent.Path
	}

	return fmt.Sprintf(
		"Node{name=%q path=%q id=%q isDir=%t size=%d children=%d readable=%t writable=%t editable=%t downloadable=%t subfolderAllowed=%t contentType=%q parentPath=%q}",
		n.Name,
		n.Path,
		n.ID,
		n.IsDir,
		n.Size,
		len(n.Children),
		n.IsReadable,
		n.IsWritable,
		n.IsEditable,
		n.IsDownloadable,
		n.IsSubfolderAllowed,
		n.ContentType,
		parentPath,
	)
}

type FileTree struct {
	root *Node
	// This is the root from rclone's perspective
	// most functions should use the relativeRoot
	relativeRoot *Node
}

func (ft *FileTree) String() string {
	if ft == nil {
		return "<nil FileTree>"
	}

	rootPath := "<nil>"
	rootID := "<nil>"
	rootChildren := -1
	if ft.root != nil {
		rootPath = ft.root.Path
		rootID = ft.root.ID
		rootChildren = len(ft.root.Children)
	}

	relativeRootPath := "<nil>"
	relativeRootID := "<nil>"
	relativeRootChildren := -1
	if ft.relativeRoot != nil {
		relativeRootPath = ft.relativeRoot.Path
		relativeRootID = ft.relativeRoot.ID
		relativeRootChildren = len(ft.relativeRoot.Children)
	}

	return fmt.Sprintf(
		"FileTree{rootPath=%q rootID=%q rootChildren=%d relativeRootPath=%q relativeRootID=%q relativeRootChildren=%d sameRoot=%t}",
		rootPath,
		rootID,
		rootChildren,
		relativeRootPath,
		relativeRootID,
		relativeRootChildren,
		ft.root == ft.relativeRoot,
	)
}

func (root *Node) GetNodeAtPath(path string) *Node {
	Assert(root != nil, fmt.Sprintf("root must be not nil; root=%q", root))

	pathSplit := splitPath(path)
	if len(pathSplit) == 0 {
		return root
	}

	currentNode := root

	for len(pathSplit) > 0 {
		if pathSplit[0] == "." {
			pathSplit = pathSplit[1:]
			continue
		}

		if pathSplit[0] == "" {
			pathSplit = pathSplit[1:]
			continue
		}

		found := false
		for _, children := range currentNode.Children {
			if strings.EqualFold(children.Name, pathSplit[0]) {
				currentNode = children
				pathSplit = pathSplit[1:]
				found = true
				break
			}
		}

		if !found {
			return nil
		}
	}

	return currentNode
}

func (ft *FileTree) ListEntries(fsys *Fs, dir string) (entries fs.DirEntries, err error) {
	Assert(ft != nil, fmt.Sprintf("ft must be not nil; ft=%q", ft))
	Assert(fsys != nil, fmt.Sprintf("fsys must be not nil; fsys=%q", fsys))

	if ft.relativeRoot == nil {
		return nil, fs.ErrorDirNotFound
	}

	if !ft.relativeRoot.IsDir {
		return nil, fs.ErrorIsFile
	}

	node := ft.relativeRoot.GetNodeAtPath(dir)
	if node == nil {
		return nil, fs.ErrorDirNotFound
	}

	if !node.IsDir {
		return nil, fs.ErrorIsFile
	}

	for _, child := range node.Children {
		Assert(
			child != nil,
			fmt.Sprintf(
				"child node must be not nil; dir=%q child=%q",
				dir, child,
			),
		)

		Assert(
			child.ID != "",
			fmt.Sprintf(
				"child node id must be not empty; dir=%q childID=%q",
				dir, child.ID,
			),
		)
		Assert(
			child.Name != "",
			fmt.Sprintf(
				"child node name must be not empty; dir=%q childID=%q",
				dir, child.ID,
			),
		)

		Assert(
			!child.ChDate.IsZero(),
			fmt.Sprintf(
				"child node chdate must be not zero; dir=%q chdate=%q",
				dir, child.ChDate,
			),
		)

		if child.IsDir {
			Assert(
				child.Size == -1,
				fmt.Sprintf(
					"child node size must be -1; dir=%q child=%q id=%q got=%d",
					dir, child.Name, child.ID, child.Size,
				),
			)

			directory := new(Directory)
			directory.fs = fsys
			directory.remote = joinPath(dir, child.Name)
			directory.id = child.ID
			directory.items = int64(len(child.Children))
			directory.name = child.Name
			directory.modTime = child.ChDate

			entries = append(entries, directory)
		} else {

			Assert(
				child.Size >= 0,
				fmt.Sprintf(
					"file node size must be >= 0; dir=%q child=%q id=%q got=%d",
					dir, child.Name, child.ID, child.Size,
				),
			)

			Assert(
				child.ContentType != "",
				fmt.Sprintf(
					"file node contenttype must be not empty; dir=%q child=%q id=%q contenttype=%q",
					dir, child.Name, child.ID, child.ContentType,
				),
			)

			object := new(Object)
			object.fs = fsys
			object.remote = joinPath(dir, child.Name)
			object.id = child.ID
			object.size = child.Size
			object.isReadable = child.IsReadable
			object.isEditable = child.IsEditable
			object.isWritable = child.IsWritable
			object.IsDownloadable = child.IsDownloadable
			object.contentType = child.ContentType
			object.modTime = child.ChDate

			entries = append(entries, object)
		}
	}

	sort.Slice(entries, func(i, j int) bool {
		return entries.Less(i, j)
	})

	return entries, nil
}