This repository has been archived by the owner on Jun 21, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
handler.go
142 lines (140 loc) · 3.68 KB
/
handler.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
package main
import (
"io"
"log"
"net/http"
"os"
"path"
"strings"
)
func handler(w http.ResponseWriter, r *http.Request) {
urlPath := path.Clean(r.URL.Path)
localPath := path.Join(dir, urlPath)
switch r.Method {
case "POST":
if noupload {
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
return
}
if isProtected("upload", auth) {
u, p, ok := r.BasicAuth()
if !ok || user != u || password != p {
w.Header().Set("WWW-Authenticate", "Basic")
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
}
if h := r.Header.Get("Content-Type"); !strings.HasPrefix(h, "multipart/form-data") {
folder := path.Clean(r.PostFormValue("folder"))
if folder != "" && folder != "/" {
switch err := os.Mkdir(path.Join(localPath, folder), 0750).(type) {
case *os.PathError:
http.Error(w, err.Op+" "+path.Join(urlPath, folder)+": "+
err.Err.Error(), 500)
return
case error:
http.Error(w, err.Error(), 500)
return
}
}
http.Redirect(w, r, path.Join(urlPath, folder), 302)
return
}
body, err := r.MultipartReader()
if err != nil {
http.Error(w, err.Error(), 500)
return
}
for part, err := body.NextPart(); err == nil; part, err = body.NextPart() {
formName := part.FormName()
if formName != "file" {
log.Printf("Skipping '%s'", formName)
continue
}
log.Printf("Handling '%s'", formName)
destFile, err := os.Create(path.Join(localPath, part.FileName()))
if err != nil {
http.Error(w, err.Error(), 500)
return
}
defer destFile.Close()
if _, err := io.Copy(destFile, part); err != nil {
http.Error(w, err.Error(), 500)
return
}
}
http.Redirect(w, r, urlPath, 302)
case "GET":
if isProtected("index", auth) {
u, p, ok := r.BasicAuth()
if !ok || user != u || password != p {
w.Header().Set("WWW-Authenticate", "Basic")
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
}
entryInfo, err := os.Stat(localPath)
if err != nil {
log.Printf("ERROR: os.Stat('%s')", localPath)
http.Error(w, err.Error(), 500)
return
}
if entryInfo.IsDir() && !strings.HasSuffix(r.URL.Path, "/") {
http.Redirect(w, r, r.URL.Path+"/", 302)
return
}
if entryInfo.IsDir() && index != "" {
if fi, err := os.Stat(path.Join(localPath, index)); err == nil {
if !fi.IsDir() {
http.Redirect(w, r, path.Join(urlPath, index), 302)
return
}
}
}
if entryInfo.IsDir() {
sortKey := r.URL.Query().Get("key")
sortOrder := asc
switch r.URL.Query().Get("order") {
case "asc":
sortOrder = asc
sortOrderMap[sortKey] = "desc"
case "desc":
sortOrder = desc
sortOrderMap[sortKey] = "asc"
}
entries, err := readDir(localPath, sortKey, sortOrder)
if err != nil {
log.Printf("ERROR: ReadDir('%s')", localPath)
http.Error(w, err.Error(), 500)
return
}
ctx := context{urlPath == "/", !noupload, entries, sortOrderMap}
if err := tmpl.Execute(w, ctx); err != nil {
log.Println("ERROR: Executing template")
http.Error(w, err.Error(), 500)
return
}
} else {
if isProtected("download", auth) {
u, p, ok := r.BasicAuth()
if !ok || user != u || password != p {
w.Header().Set("WWW-Authenticate", "Basic")
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
}
f, err := os.Open(localPath)
if err != nil {
log.Printf("ERROR: os.Open('%s')", localPath)
http.Error(w, err.Error(), 500)
return
}
defer f.Close()
log.Printf("Serving '%s'", localPath)
http.ServeContent(w, r, entryInfo.Name(), entryInfo.ModTime(), f)
}
default:
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
}
return
}