-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathh_set.go
More file actions
88 lines (66 loc) 路 1.74 KB
/
Copy pathh_set.go
File metadata and controls
88 lines (66 loc) 路 1.74 KB
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
package main
import (
"fmt"
"io"
// "time"
// "unsafe"
"net/http"
"os"
"path/filepath"
"path"
)
func handler_upload(w http.ResponseWriter, r *http.Request) {
mode_up := r.URL.Query().Get("fo")
if mode_up != "" {
decode_url := func_decode(path.Clean(mode_up))
build_path := filepath.Join(root, decode_url)
fileInfo, err := os.Stat(build_path)
if err != nil || !fileInfo.IsDir() {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// Only parse the headers, not the content
reader, err := r.MultipartReader()
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
for {
part, err := reader.NextPart()
if err == io.EOF {
break
}
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Get filename from query parameter (more reliable than multipart filename)
filename := r.URL.Query().Get("filename")
if filename == "" {
filename = part.FileName()
}
if filename == "" {
continue
}
build_log := fmt.Sprintf("%s -> %s", filename, decode_url)
func_log("\033[97m", r.RemoteAddr, "[SET] ", build_log)
// Create destination file
dstPath := filepath.Join(build_path, filepath.Base(filename))
dst, err := os.Create(dstPath)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Stream directly to disk
if _, err := io.Copy(dst, part); err != nil {
dst.Close()
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
dst.Close()
}
fmt.Fprint(w, "Files uploaded successfully")
} else {
fmt.Fprint(w, "error ...")
}
}