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
7382var (
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
102115func 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
261288func (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 ("\n will listen on: %s\n path to expose: %s" , ADDR , BASE_PATH )
275- log .Fatal (http .ListenAndServe (ADDR , nil ))
276- }
0 commit comments