-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.go
128 lines (93 loc) · 2.69 KB
/
server.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
package main
import (
"os"
"log"
"net/http"
"html/template"
"github.com/gorilla/mux"
"github.com/mitchellh/go-homedir"
"strings"
)
type FList struct {
Files []FullPath
}
type FullPath struct {
Name string
Path string
}
func Run(){
r := mux.NewRouter()
r.HandleFunc("/", DefaultHandler).Methods("GET")
r.HandleFunc("/", PathHandler).Methods("POST")
r.HandleFunc("/sort", SortHandler).Methods("POST")
r.HandleFunc("/regsort", RegSortHandler).Methods("POST")
r.PathPrefix("/public/").Handler(http.StripPrefix("/public/", http.FileServer(http.Dir("./public/"))))
http.Handle("/", r)
http.ListenAndServe(":8080", nil)
}
func DefaultHandler(res http.ResponseWriter, req *http.Request){
path, err := homedir.Dir()
log.Println(path)
checkErr(err)
fobj := CreateFList(path)
t, err := template.ParseFiles("index.html")
checkErr(err)
err = t.Execute(res, fobj)
checkErr(err)
}
func PathHandler(res http.ResponseWriter, req *http.Request){
path := req.FormValue("path")
log.Println(path)
fobj := CreateFList(path)
t, err := template.ParseFiles("index.html")
checkErr(err)
err = t.Execute(res, fobj)
checkErr(err)
}
type SortObj struct {
Path string
}
func SortHandler(res http.ResponseWriter, req *http.Request){
path := req.FormValue("path")
Sort(path)
t, err := template.ParseFiles("sortpage.html")
checkErr(err)
err = t.Execute(res, &SortObj{Path: path})
checkErr(err)
}
func RegSortHandler(res http.ResponseWriter, req *http.Request){
path := req.FormValue("path")
pattern := req.FormValue("pattern")
SortWithRegexp(path, pattern)
t, err := template.ParseFiles("sortpage.html")
checkErr(err)
err = t.Execute(res, &SortObj{Path: path})
checkErr(err)
}
func checkErr(err error){
if err != nil {
log.Fatal(err)
}
}
func CreateFList(path string) *FList {
fobj := &FList{Files: make([]FullPath, 0, 100)}
dir, err := os.Open(path)
checkErr(err)
fi, err := dir.Readdir(100)
//checkErr(err)
if(len(fi)==0){
fobj.Files = append(fobj.Files, FullPath{Name:"This Directory is empty", Path: path})
return fobj
}
for _, file := range fi {
if file.IsDir() {
if (!strings.HasPrefix(file.Name(), ".")){ //Doesn't display Hidden folders
fobj.Files = append(fobj.Files,
FullPath{Name:file.Name(), Path: path+"/"+file.Name()})
log.Println(file.Name())
}
}
}
//fobj := &FList{Files: fi}
return fobj
}