-
Notifications
You must be signed in to change notification settings - Fork 5
/
file-directory.go
279 lines (251 loc) · 7.01 KB
/
file-directory.go
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/* For license and copyright information please see LEGAL file in repository */
/*
dos abbreviations for "Default Operating System"
*/
package dos
import (
goos "os"
"sort"
"strings"
"../../file"
"../../protocol"
)
// FileDirectory use to store app needed data from repo like html, css, js, ...
type FileDirectory struct {
metadata fileDirectoryMetaData
parentDirectory *FileDirectory // Not nill if the directory has parent!
directories map[string]*FileDirectory // map key is dir name
files map[string]*File // map key is file name
}
func (dir *FileDirectory) MetaData() protocol.FileDirectoryMetaData { return &dir.metadata }
func (dir *FileDirectory) ParentDirectory() protocol.FileDirectory { return dir.parentDirectory }
// Mkdir make new directory in relative path from given directory.
func (dir *FileDirectory) Directories(offset, limit uint64) (dirs []protocol.FileDirectory) {
// TODO:::
return
}
// Directory return the directory by its name or make new one if desire name not exist.
func (dir *FileDirectory) Directory(name string) (dr protocol.FileDirectory, err protocol.Error) {
var goErr error
var exist bool
var dirPath = dir.metadata.uri.Path() + "/" + name + "/"
dr, exist = dir.directories[name]
if !exist {
goErr = goos.Mkdir(dirPath, 0700)
if goErr != nil {
// err =
return
}
}
if dr == nil {
var dir = FileDirectory{
parentDirectory: dir,
}
err = dir.init(dirPath)
if err != nil {
return
}
dir.directories[name] = &dir
}
return
}
// Files use to get all files in order by name.
func (dir *FileDirectory) Files(offset, limit uint64) (files []protocol.File) {
var keys = make([]string, 0, len(dir.files))
for k := range dir.files {
keys = append(keys, k)
}
sort.Strings(keys)
files = make([]protocol.File, len(dir.files))
for i, k := range keys {
files[i] = dir.files[k]
}
return
}
// File use to get a file by its full name with extension
// And make new one if desire name not exist.
func (dir *FileDirectory) File(name string) (file protocol.File, err protocol.Error) {
file, _ = dir.files[name]
if file == nil {
// make new
var fi = File{
parentDirectory: dir,
}
err = fi.init(name)
if err != nil {
return
}
dir.files[name] = &fi
}
return
}
// FileByPath use to get a file by its path in the directory
func (dir *FileDirectory) FileByPath(uriPath string) (file protocol.File, err protocol.Error) {
file, err = dir.fileByPath(uriPath)
return
}
func (dir *FileDirectory) fileByPath(uriPath string) (file *File, err protocol.Error) {
var pathParts = strings.Split(uriPath, "/")
var pathPartsLen = len(pathParts)
var fullFileName = pathParts[pathPartsLen-1]
if pathPartsLen < 3 {
file = dir.files[fullFileName]
return
}
if pathParts[0] == "" {
pathParts = pathParts[1:]
}
var directory *FileDirectory
directory, err = dir.directoryByPathParts(pathParts)
if directory == nil || err != nil {
return
}
file = directory.files[fullFileName]
return
}
// DirectoryByPath use to get a directory by its full path location.
// path is the file location in FileSystems include file name
func (dir *FileDirectory) DirectoryByPath(pathParts []string) (directory protocol.FileDirectory, err protocol.Error) {
directory, err = dir.directoryByPathParts(pathParts)
return
}
func (dir *FileDirectory) directoryByPath(uriPath string) (directory *FileDirectory, err protocol.Error) {
var pathParts = strings.Split(uriPath, "/")
if pathParts[0] == "" {
pathParts = pathParts[1:]
}
directory, err = dir.directoryByPathParts(pathParts)
return
}
func (dir *FileDirectory) directoryByPathParts(pathParts []string) (directory *FileDirectory, err protocol.Error) {
var pathPartsLen = len(pathParts)
directory = dir
for i := 0; i < pathPartsLen; i++ {
directory, _ = directory.directories[pathParts[i]]
if directory == nil {
return
}
}
return
}
// FindFiles use to get a file by some part of its name!
func (dir *FileDirectory) FindFiles(partName string, num uint) (files []protocol.File) {
for fileName, file := range dir.files {
if strings.Contains(fileName, partName) {
files = append(files, file)
if len(files) == int(num) {
return
}
}
}
return
}
// FindFiles use to get a file by some part of its name!
func (dir *FileDirectory) FindFile(partName string) (files protocol.File) {
for fileName, file := range dir.files {
if strings.Contains(fileName, partName) {
return file
}
}
return
}
// FindFileRecursively use to get a file by its ful name with extension in recursively!
func (dir *FileDirectory) FindFileRecursively(partName string) (file *File) {
file = dir.files[partName]
if file != nil {
return
}
if dir.directories != nil {
for _, dep := range dir.directories {
file = dep.FindFileRecursively(partName)
if file != nil {
return
}
}
}
return nil
}
func (dir *FileDirectory) Rename(oldURIPath, newURIPath string) (err protocol.Error) {
// TODO:::
return
}
func (dir *FileDirectory) Copy(uriPath, newURIPath string) (err protocol.Error) {
// var file = File{
// Directory: f.Directory,
// Path: f.Path,
// FullName: f.FullName,
// Name: f.Name,
// Extension: f.Extension,
// mediaType: f.mediaType,
// State: f.State,
// data: f.data,
// }
return
}
func (dir *FileDirectory) Move(uriPath, newURIPath string) (err protocol.Error) {
// var file = File{
// Directory: f.Directory,
// Path: f.Path,
// FullName: f.FullName,
// Name: f.Name,
// Extension: f.Extension,
// mediaType: f.mediaType,
// State: f.State,
// data: make([]byte, len(f.data)),
// }
// copy(file.data, f.data)
return
}
func (dir *FileDirectory) Delete(uriPath string) (err protocol.Error) {
goos.Remove(uriPath)
if file.IsPathDirectory(uriPath) {
var dir, _ = dir.directoryByPath(uriPath)
delete(dir.parentDirectory.directories, dir.metadata.URI().Name())
} else {
var file, _ = dir.fileByPath(uriPath)
delete(file.parentDirectory.files, file.metadata.URI().Name())
}
return
}
func (dir *FileDirectory) Wipe(uriPath string) (err protocol.Error) {
// TODO::: first write null data to uri path
err = dir.Delete(uriPath)
return
}
// GetDependencyRecursively use to get a dependency by its name in recursively!
func (dir *FileDirectory) GetDependencyRecursively(name string) *FileDirectory {
var t *FileDirectory
var ok bool
if dir.directories != nil {
t, ok = dir.directories[name]
if !ok {
for _, dep := range dir.directories {
t = dep.GetDependencyRecursively(name)
if t != nil {
break
}
}
}
}
return t
}
// init use to read||update Directory from disk.
func (dir *FileDirectory) init(path string) (err protocol.Error) {
dir.metadata.URI().Init(path)
dir.files = make(map[string]*File)
dir.directories = make(map[string]*FileDirectory)
var dirEntry, goErr = goos.ReadDir(dir.metadata.uri.Path())
if goErr != nil {
// err =
return
}
for _, file := range dirEntry {
var fileName = file.Name()
if file.IsDir() {
dir.directories[fileName] = nil
} else {
dir.files[fileName] = nil
}
}
return
}