-
Notifications
You must be signed in to change notification settings - Fork 1
/
private.go
104 lines (95 loc) · 2.39 KB
/
private.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
package render
import (
"io"
"io/ioutil"
"os"
"path"
"strings"
)
func mkdirIfNotExist(dir string) error {
// mkdir target path if not exist
if _, err := os.Stat(dir); os.IsNotExist(err) {
err := os.MkdirAll(dir, 0755)
if err != nil {
return err
}
}
return nil
}
func copyFile(src, dst string) error {
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
out, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return err
}
defer func() {
if e := out.Close(); e != nil {
err = e
}
}()
_, err = io.Copy(out, in)
return nil
}
func getAllFiles(root, curr string) ([]PathFile, error) {
absPath := path.Join(root, curr)
files, err := ioutil.ReadDir(absPath)
if err != nil {
return []PathFile{}, err
}
pathfiles := []PathFile{}
for _, f := range files {
if f.IsDir() {
subpathfiles, err := getAllFiles(root, path.Join(curr, f.Name()))
if err != nil {
return []PathFile{}, err
}
pathfiles = append(pathfiles, subpathfiles...)
} else {
// set path with relative position
pathfiles = append(pathfiles, PathFile{curr, f.Name()})
}
}
return pathfiles, nil
}
func _splitRenderFile(root, curr string, keepdir DirSorted) ([]PathFile, []PathFile, error) {
absPath := path.Join(root, curr)
files, err := ioutil.ReadDir(absPath)
if err != nil {
return []PathFile{}, []PathFile{}, err
}
renderFiles := []PathFile{}
keepFiles := []PathFile{}
for _, f := range files {
if keepdir.search(f.Name()) == true {
files, err := getAllFiles(root, path.Join(curr, f.Name()))
if err != nil {
return []PathFile{}, []PathFile{}, err
}
keepFiles = append(keepFiles, files...)
} else {
if f.IsDir() { // recusion director
subNotRending := DirSorted{}
subkeepdir := f.Name() + "/"
for _, e := range keepdir {
if strings.HasPrefix(e, subkeepdir) && len(e) > len(subkeepdir) {
subNotRending = append(subNotRending, e[len(subkeepdir):])
}
}
subRenderFiles, subKeepFiles, err := _splitRenderFile(root, path.Join(curr, f.Name()), subNotRending)
if err != nil {
return []PathFile{}, []PathFile{}, err
}
// add prefix path to sub html file
renderFiles = append(renderFiles, subRenderFiles...)
keepFiles = append(keepFiles, subKeepFiles...)
} else {
renderFiles = append(renderFiles, PathFile{curr, f.Name()})
}
}
}
return renderFiles, keepFiles, nil
}