-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFSTree.go
93 lines (84 loc) · 1.64 KB
/
FSTree.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
package main
import (
"path/filepath"
"io/ioutil"
"log"
"github.com/pkg/errors"
"os"
)
const (
File = "0"
Directory = "1"
Gif = "g"
)
type file struct {
name string
ext string
size int64
}
type directory struct {
path string
name string
files []file
directories []directory
}
func (d *directory) iterate(i func(string, string)) {
for _, f := range d.directories {
i(f.name, Directory)
}
for _, f := range d.files {
if f.ext == ".gif" {
i(f.name, Gif)
} else {
i(f.name, File)
}
}
}
func (d *directory) addDirectory(addDirectory *directory) {
d.directories = append(d.directories, *addDirectory)
}
func (d *directory) getIndex(workingDirectory string) (v []byte, err error) {
for _, f := range d.files {
if f.name == "index.txt" {
return ioutil.ReadFile(workingDirectory+d.path+"/"+f.name)
}
}
return nil, errors.New("No index available")
}
func (d *directory) addFile(addFile *file) {
d.files = append(d.files, *addFile)
}
func GetDirectoryAtPath(workingDirectory string, path string) *directory {
stat, err := os.Stat(workingDirectory+path)
if err != nil {
log.Fatal(err)
return nil
}
if stat.IsDir() {
fileInfo, err := ioutil.ReadDir(workingDirectory+path)
var returnDirectory = directory{
path: path,
name: ".",
}
if err != nil {
log.Fatal(err)
return &returnDirectory
}
for _, f := range fileInfo {
if f.IsDir() {
returnDirectory.addDirectory(&directory{
name: f.Name(),
path: path,
})
} else {
returnDirectory.addFile(&file{
name: f.Name(),
size: f.Size(),
ext: filepath.Ext(f.Name()),
})
}
}
return &returnDirectory
}
return nil
}