-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
174 lines (154 loc) · 4.21 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
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
package handler
import (
"encoding/json"
"fmt"
"html/template"
"io"
"mime"
"strings"
"net/http"
"os"
"path/filepath"
"github.com/chyok/st/config"
"github.com/chyok/st/internal/util"
"github.com/chyok/st/web"
)
func ReceiveHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
// serve upload page for receive
tmpl, err := template.New("index").Parse(web.UploadPage)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = tmpl.Execute(w, config.G.DeviceName)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
case http.MethodPost:
// receive file and save
file, header, err := r.FormFile("file")
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
defer file.Close()
_, params, err := mime.ParseMediaType(header.Header.Get("Content-Disposition"))
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
filename := filepath.FromSlash(params["filename"])
fmt.Printf("Downloading [%s]...\n", filename)
dirPath := filepath.Dir(filename)
err = os.MkdirAll(dirPath, os.ModePerm)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
out, err := os.Create(filename)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer out.Close()
_, err = io.Copy(out, file)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Printf("[✅] Download [%s] Success.\n", filename)
default:
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
}
}
func FileServerHandler(w http.ResponseWriter, r *http.Request) {
currentPath := config.G.FilePath
fileInfo, err := os.Stat(currentPath)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
basePath := filepath.Base(currentPath)
if fileInfo.IsDir() {
path := r.URL.Path[len("/download/"+basePath):]
fullPath := filepath.Join(currentPath, path)
http.ServeFile(w, r, fullPath)
} else {
http.ServeFile(w, r, currentPath)
}
}
func SendHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
// serve download page for send
realFilePath := filepath.Join(config.G.FilePath, r.URL.Path[1:])
downloadPath := filepath.Join(filepath.Base(config.G.FilePath), r.URL.Path[1:])
fileInfo, err := os.Stat(realFilePath)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
data := struct {
DeviceName string
IsDir bool
FileName string
DownloadPath string
UrlPath string
Files []os.DirEntry
}{
DeviceName: config.G.DeviceName,
DownloadPath: downloadPath,
UrlPath: strings.TrimSuffix(r.URL.Path, "/"),
}
if fileInfo.IsDir() {
data.IsDir = true
files, err := os.ReadDir(realFilePath)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
data.Files = files
} else {
data.FileName = filepath.Base(realFilePath)
}
tmpl, err := template.New("download").Parse(web.DownloadPage)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = tmpl.Execute(w, data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
case http.MethodPost:
// return file or folder information in JSON format for convenient send to the recipient
currentPath := config.G.FilePath
fileInfo, err := os.Stat(currentPath)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var pathInfo struct {
Type string `json:"type"`
Paths []string `json:"paths"`
}
if fileInfo.IsDir() {
files, err := util.GetDirFilePaths(currentPath, true)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
pathInfo.Paths = files
pathInfo.Type = "dir"
} else {
pathInfo.Paths = []string{filepath.Base(currentPath)}
pathInfo.Type = "file"
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(pathInfo)
default:
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
}
}