-
Notifications
You must be signed in to change notification settings - Fork 0
/
filewm.go
249 lines (205 loc) · 6.36 KB
/
filewm.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"sync"
)
const uploadDir = "./uploads"
var (
password string
passwordLock sync.RWMutex
isProtected bool
)
type FileInfo struct {
Name string `json:"name"`
IsDir bool `json:"isDir"`
}
func main() {
if err := os.MkdirAll(uploadDir, os.ModePerm); err != nil {
log.Fatalf("Failed to create upload directory: %v", err)
}
http.HandleFunc("/", authMiddleware(indexHandler))
http.HandleFunc("/upload", authMiddleware(uploadHandler))
http.HandleFunc("/files/", authMiddleware(fileHandler))
http.HandleFunc("/rename", authMiddleware(renameHandler))
http.HandleFunc("/delete", authMiddleware(deleteHandler))
http.HandleFunc("/create-folder", authMiddleware(createFolderHandler))
http.HandleFunc("/set-password", setPasswordHandler)
http.HandleFunc("/toggle-protection", toggleProtectionHandler)
http.HandleFunc("/list", authMiddleware(listHandler))
fmt.Println("Server is running on http://localhost:38500")
log.Fatal(http.ListenAndServe(":38500", nil))
}
func authMiddleware(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
passwordLock.RLock()
currentIsProtected := isProtected
currentPassword := password
passwordLock.RUnlock()
if currentIsProtected {
_, pass, ok := r.BasicAuth()
if !ok || pass != currentPassword {
w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
}
next.ServeHTTP(w, r)
}
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "index.html")
}
func uploadHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
file, header, err := r.FormFile("file")
if err != nil {
http.Error(w, "Error getting file: "+err.Error(), http.StatusBadRequest)
return
}
defer file.Close()
dir := r.FormValue("dir")
fullPath := filepath.Join(uploadDir, dir)
if err := os.MkdirAll(fullPath, os.ModePerm); err != nil {
http.Error(w, "Error creating directory: "+err.Error(), http.StatusInternalServerError)
return
}
filename := filepath.Join(fullPath, header.Filename)
out, err := os.Create(filename)
if err != nil {
http.Error(w, "Error creating file: "+err.Error(), http.StatusInternalServerError)
return
}
defer out.Close()
_, err = io.Copy(out, file)
if err != nil {
http.Error(w, "Error copying file: "+err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}
func fileHandler(w http.ResponseWriter, r *http.Request) {
filename := filepath.Join(uploadDir, r.URL.Path[7:])
http.ServeFile(w, r, filename)
}
func renameHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
var renameRequest struct {
OldPath string `json:"oldPath"`
NewPath string `json:"newPath"`
}
err := json.NewDecoder(r.Body).Decode(&renameRequest)
if err != nil {
http.Error(w, "Error decoding request body: "+err.Error(), http.StatusBadRequest)
return
}
oldPath := filepath.Join(uploadDir, renameRequest.OldPath)
newPath := filepath.Join(uploadDir, renameRequest.NewPath)
err = os.Rename(oldPath, newPath)
if err != nil {
json.NewEncoder(w).Encode(map[string]interface{}{"success": false, "error": err.Error()})
return
}
json.NewEncoder(w).Encode(map[string]interface{}{"success": true})
}
func setPasswordHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
var passwordRequest struct {
Password string `json:"password"`
}
err := json.NewDecoder(r.Body).Decode(&passwordRequest)
if err != nil {
http.Error(w, "Error decoding request body: "+err.Error(), http.StatusBadRequest)
return
}
passwordLock.Lock()
password = passwordRequest.Password
passwordLock.Unlock()
json.NewEncoder(w).Encode(map[string]interface{}{"success": true})
}
func toggleProtectionHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
passwordLock.Lock()
isProtected = !isProtected
currentIsProtected := isProtected
passwordLock.Unlock()
json.NewEncoder(w).Encode(map[string]interface{}{"success": true, "isProtected": currentIsProtected})
}
func deleteHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
var deleteRequest struct {
Path string `json:"path"`
}
err := json.NewDecoder(r.Body).Decode(&deleteRequest)
if err != nil {
http.Error(w, "Error decoding request body: "+err.Error(), http.StatusBadRequest)
return
}
fullPath := filepath.Join(uploadDir, deleteRequest.Path)
err = os.RemoveAll(fullPath)
if err != nil {
json.NewEncoder(w).Encode(map[string]interface{}{"success": false, "error": err.Error()})
return
}
json.NewEncoder(w).Encode(map[string]interface{}{"success": true})
}
func createFolderHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
var folderRequest struct {
Path string `json:"path"`
}
err := json.NewDecoder(r.Body).Decode(&folderRequest)
if err != nil {
http.Error(w, "Error decoding request body: "+err.Error(), http.StatusBadRequest)
return
}
folderPath := filepath.Join(uploadDir, folderRequest.Path)
err = os.MkdirAll(folderPath, os.ModePerm)
if err != nil {
json.NewEncoder(w).Encode(map[string]interface{}{"success": false, "error": err.Error()})
return
}
json.NewEncoder(w).Encode(map[string]interface{}{"success": true})
}
func listHandler(w http.ResponseWriter, r *http.Request) {
dir := r.URL.Query().Get("dir")
fullPath := filepath.Join(uploadDir, dir)
files, err := ioutil.ReadDir(fullPath)
if err != nil {
http.Error(w, "Error reading directory: "+err.Error(), http.StatusInternalServerError)
return
}
var fileInfos []FileInfo
for _, file := range files {
fileInfos = append(fileInfos, FileInfo{
Name: file.Name(),
IsDir: file.IsDir(),
})
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(fileInfos)
}