/
view.listFiles.go
161 lines (132 loc) · 3.66 KB
/
view.listFiles.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
package fe26
import (
"net/http"
log "github.com/sirupsen/logrus"
"github.com/AndresBott/Fe26/pkg/f"
"path/filepath"
"github.com/AndresBott/Fe26/internal/fileManager"
"strings"
"errors"
"encoding/json"
"net/url"
"github.com/dustin/go-humanize"
"github.com/Masterminds/sprig"
"html/template"
"github.com/gobuffalo/packr/v2"
)
type Breadcrumb struct{
Name string
Url string
}
type listFileData struct {
Files []fileManager.File
Dirs []fileManager.File
RequestUrl string
RequestBasePath string
RequestParentPath string
BreadCrumbs []Breadcrumb
FeBase string
Fe26Version string
Fe26Repository string
}
// take a relative directory path and separate breadcrumb types
// return a sequencial array of breadcrumbs
func breadcrumbs(directoryQueryPath string) []Breadcrumb {
pathParts := strings.Split(directoryQueryPath,"/")
var newPath []Breadcrumb
breadCrumbsUrl := ""
for _, item := range pathParts {
if item != ""{
breadCrumbsUrl = breadCrumbsUrl + "/"+item
b := Breadcrumb{
Name:item,
Url:breadCrumbsUrl,
}
newPath = append(newPath, b)
}
}
return newPath
}
// list the files of a dir based on the http query string
func getFilesData(directoryQueryPath string) (listFileData,error) {
data := listFileData{}
if directoryQueryPath == ""{
directoryQueryPath = "/"
}
sanitizedPath,err := f.Sanitize(directoryQueryPath)
if err != nil{
data.RequestUrl="/"
return data,err
}
if directoryQueryPath != sanitizedPath{
data.RequestUrl=sanitizedPath
return data,errors.New("Request malformed: "+directoryQueryPath)
}
data.RequestBasePath = ""
if directoryQueryPath != "/"{
data.RequestBasePath = directoryQueryPath
}
data.RequestUrl = directoryQueryPath
data.RequestParentPath = filepath.Dir(directoryQueryPath)
d := fileManager.NewFileManager(directoryQueryPath,Config.docRoot)
data.Files = d.Files
data.Dirs = d.Dirs
data.BreadCrumbs = breadcrumbs(directoryQueryPath)
data.FeBase = Config.FeBase
data.Fe26Version = Fe26Version
data.Fe26Repository = Fe26Repository
return data,nil
}
func ListFilesHTML(w http.ResponseWriter, r *http.Request) {
d := r.URL.Query().Get("d")
data,err := getFilesData(d)
if err != nil{
log.Warn("Wrong query parameter, redirecting to: "+data.RequestUrl)
http.Redirect(w, r, Config.FeBase+"?d="+data.RequestUrl, 302)
return
}
log.Info("LIST: "+data.RequestUrl)
templates := packr.New("Templates", "../../web/templates")
s, _ := templates.FindString("listFiles.html")
s2 ,_ := templates.FindString("head.html")
//s3 ,_ := templates.FindString("fileupload.html")
tmpl,err := template.New("main").Funcs(
// todo externalize funcMAp
template.FuncMap{
"urlEncode": func(uri string) string {
//https://play.golang.org/p/pOfrn-Wsq5
s := url.URL{Path: uri}
return s.String()
//return url.QueryEscape(uri)
},
"humanizeBytes": func (b int64) string{
return humanize.Bytes(uint64(b))
},
}).Funcs(sprig.FuncMap()).Parse(s+s2)
if err != nil{
log.Warn(err)
}
err = tmpl.ExecuteTemplate(w, "layout", data)
if err != nil {
log.Error(err)
}
}
func ListFilesJson(w http.ResponseWriter, r *http.Request) {
d := r.URL.Query().Get("d")
data,err := getFilesData(d)
// TODO: maybe json should not redirect
if err != nil{
log.Warn("Wrong query parameter, redirecting to: "+data.RequestUrl)
http.Redirect(w, r, Config.FeBase+"?d="+data.RequestUrl, 302)
return
}
log.Info("JSON LIST: "+data.RequestUrl)
js, err := json.Marshal(data)
if err != nil {
http.Error(w, ServerError, http.StatusInternalServerError)
log.Error("Unable to serve: "+data.RequestUrl+ " due to "+err.Error())
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}