Skip to content

Commit f618310

Browse files
committed
1、【新增功能】可以创建文件夹啦
2、【改进优化】启动时增加addr和dir两个参数,分别为监听的地址和选择公开的目录 3、【改进优化】文件后面加了下载按钮(直接点击文件会尽量在浏览器中打开)
1 parent e033e06 commit f618310

File tree

3 files changed

+75
-25
lines changed

3 files changed

+75
-25
lines changed

build.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export CGO_ENABLED=0
2+
3+
osS='
4+
linux
5+
windows
6+
'
7+
8+
archS='
9+
amd64
10+
'
11+
12+
for os in ${osS}; do
13+
for arch in ${archS}; do
14+
export GOOS=${os}
15+
export GOARCH=${arch}
16+
go build -o tskfs_${os}_${arch} main.go
17+
chmod a+x tskfs_${os}_${arch}
18+
done
19+
done

fileServer/fs.go

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"fmt"
77
"time"
88
"html/template"
9-
"log"
109
"io"
1110
"path/filepath"
1211
"reflect"
@@ -47,10 +46,15 @@ const (
4746
{{range .}}
4847
<tr>
4948
<td>
50-
<a href="/download?path={{$Path}}/{{.Name}}">{{dirSuffix .}}</a>
49+
<a href="/download?path={{$Path}}/{{.Name}}&disposition=inline">{{dirSuffix .}}</a>
5150
</td>
5251
<td>{{humanSize .Size}}</td>
5352
<td>{{formatTime .ModTime}}</td>
53+
{{if not (isDir .)}}
54+
<td>
55+
<a href="/download?path={{$Path}}/{{.Name}}&disposition=attachment">download</a>
56+
</td>
57+
{{end}}
5458
</tr>
5559
{{end}}
5660
{{end}}
@@ -63,6 +67,11 @@ const (
6367
<input type="file" name="upload">
6468
<input type="submit" value="upload">
6569
</form>
70+
71+
<form action="/mkdir?path={{$Path}}" method="post" enctype="multipart/form-data">
72+
<input type="text" name="mkdir">
73+
<input type="submit" value="mkdir">
74+
</form>
6675
6776
</body>
6877
@@ -71,13 +80,14 @@ const (
7180
)
7281

7382
var (
74-
ADDR = ":80"
75-
BASE_PATH = "."
83+
ADDR string
84+
BASE_PATH string
7685

7786
funcMap = template.FuncMap{
7887
"humanSize": humanSize,
7988
"formatTime": formatTime,
8089
"dirSuffix": dirSuffix,
90+
"isDir": isDir,
8191
}
8292

8393
t = template.Must(template.New("TEMPLATE_LS").Funcs(funcMap).Parse(TEMPLATE_LS))
@@ -98,6 +108,9 @@ func dirSuffix(f os.FileInfo) string {
98108
return f.Name()
99109
}
100110
}
111+
func isDir(f os.FileInfo) bool {
112+
return f.IsDir()
113+
}
101114

102115
func humanSize(byteSize int64) string {
103116
var suffix = "B"
@@ -130,7 +143,7 @@ func formatTime(time time.Time) string {
130143
return time.Format("2006-01-02 15:04:05")
131144
}
132145

133-
func download(w http.ResponseWriter, r *http.Request) {
146+
func Download(w http.ResponseWriter, r *http.Request) {
134147
path := filepath.Clean(r.FormValue("path"))
135148

136149
file, err := os.Open(filepath.Join(BASE_PATH, path))
@@ -162,15 +175,16 @@ func download(w http.ResponseWriter, r *http.Request) {
162175
} else {
163176
//w.Header().Set("Content-Type", "text/plain; charset=utf-8")
164177
//w.Header().Set("Content-Type", "application/octet-stream; charset=utf-8")
165-
w.Header().Set("Content-Disposition", fmt.Sprintf("inline; filename=\"%s\"", fileStat.Name()))
178+
disposition := r.FormValue("disposition")
179+
w.Header().Set("Content-Disposition", fmt.Sprintf("%s; filename=\"%s\"", disposition, fileStat.Name()))
166180
_, err := io.Copy(w, file)
167181
if err != nil {
168182
fmt.Fprintln(w, err)
169183
}
170184
}
171185
}
172186

173-
func upload(w http.ResponseWriter, r *http.Request) {
187+
func Upload(w http.ResponseWriter, r *http.Request) {
174188
path := r.FormValue("path")
175189
file, fileHeader, err := r.FormFile("upload")
176190
if err != nil {
@@ -195,14 +209,27 @@ func upload(w http.ResponseWriter, r *http.Request) {
195209
http.Redirect(w, r, "/download?path="+path, http.StatusTemporaryRedirect)
196210
}
197211

198-
func showRouterS(w http.ResponseWriter, r *http.Request) {
212+
func Mkdir(w http.ResponseWriter, r *http.Request) {
213+
path := r.FormValue("path")
214+
dir := r.FormValue("mkdir")
215+
dirToMk := filepath.Join(BASE_PATH, path, dir)
216+
err := os.MkdirAll(dirToMk, os.ModePerm)
217+
if err != nil {
218+
fmt.Fprintln(w, err)
219+
return
220+
}
221+
222+
http.Redirect(w, r, "/download?path="+path, http.StatusTemporaryRedirect)
223+
}
224+
225+
func ShowRouterS(w http.ResponseWriter, r *http.Request) {
199226
mKeyS := reflect.ValueOf(http.DefaultServeMux).Elem().FieldByName("m").MapKeys()
200227
for p := range mKeyS {
201228
fmt.Fprintf(w, "<a href=\"%s\">%s</a><br />", mKeyS[p], mKeyS[p])
202229
}
203230
}
204231

205-
func cmdLocal(w http.ResponseWriter, r *http.Request) {
232+
func CmdLocal(w http.ResponseWriter, r *http.Request) {
206233
data, err := ioutil.ReadAll(r.Body)
207234
if err != nil {
208235
fmt.Fprintln(w, err)
@@ -232,7 +259,7 @@ func cmdLocal(w http.ResponseWriter, r *http.Request) {
232259
fmt.Fprintf(w, "%s", resultJson)
233260
}
234261

235-
func cmdSsh(w http.ResponseWriter, r *http.Request) {
262+
func CmdSsh(w http.ResponseWriter, r *http.Request) {
236263
data, err := ioutil.ReadAll(r.Body)
237264
if err != nil {
238265
fmt.Fprintln(w, err)
@@ -260,17 +287,3 @@ func cmdSsh(w http.ResponseWriter, r *http.Request) {
260287

261288
func (h *TskHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
262289
}
263-
264-
func Start() {
265-
266-
http.HandleFunc("/", showRouterS)
267-
268-
http.HandleFunc("/download", download)
269-
http.HandleFunc("/upload", upload)
270-
271-
http.HandleFunc("/cmd/local", cmdLocal)
272-
http.HandleFunc("/cmd/ssh", cmdSsh)
273-
274-
log.Printf("\nwill listen on: %s\npath to expose: %s", ADDR, BASE_PATH)
275-
log.Fatal(http.ListenAndServe(ADDR, nil))
276-
}

main.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,27 @@
11
package main
22

33
import (
4+
"net/http"
5+
"log"
46
"tskFileServer/fileServer"
7+
"flag"
58
)
69

710
func main() {
8-
fileServer.Start()
11+
12+
flag.StringVar(&fileServer.ADDR, "addr", "", "[ip:port], such as: 0.0.0.0:80")
13+
flag.StringVar(&fileServer.BASE_PATH, "dir", ".", "Directory to expose, such as: /tmp/share")
14+
flag.Parse()
15+
16+
http.HandleFunc("/", fileServer.ShowRouterS)
17+
18+
http.HandleFunc("/download", fileServer.Download)
19+
http.HandleFunc("/upload", fileServer.Upload)
20+
http.HandleFunc("/mkdir", fileServer.Mkdir)
21+
22+
http.HandleFunc("/cmd/local", fileServer.CmdLocal)
23+
http.HandleFunc("/cmd/ssh", fileServer.CmdSsh)
24+
25+
log.Printf("\nwill listen on: %s\npath to expose: %s", fileServer.ADDR, fileServer.BASE_PATH)
26+
log.Fatal(http.ListenAndServe(fileServer.ADDR, nil))
927
}

0 commit comments

Comments
 (0)