forked from jordic/file_server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
340 lines (274 loc) · 6.58 KB
/
main.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package main
import (
"archive/zip"
"bytes"
"encoding/json"
"flag"
"fmt"
"github.com/jordic/file_server/util"
"html/template"
"io"
"io/ioutil"
"log"
"net/http"
_ "net/http/pprof"
"os"
"path"
"path/filepath"
"strings"
"time"
)
var (
dir string
port string
logging bool
depth int
)
//var cpuprof string
const MAX_MEMORY = 1 * 1024 * 1024
const VERSION = "0.94c"
type File struct {
Name string
Size int64
ModTime time.Time
IsDir bool
IsText bool
}
func main() {
//fmt.Println(len(os.Args), os.Args)
if len(os.Args) > 1 && os.Args[1] == "-v" {
fmt.Println("Version " + VERSION)
os.Exit(0)
}
flag.StringVar(&dir, "dir", ".", "Specify a directory to server files from.")
flag.StringVar(&port, "port", ":8080", "Port to bind the file server")
flag.BoolVar(&logging, "log", true, "Enable Log (true/false)")
//flag.IntVar(&depth, "depth", 5, "Depth directory crwaler")
//flag.StringVar(&cpuprof, "cpuprof", "", "write cpu and mem profile")
flag.Parse()
if logging == false {
log.SetOutput(ioutil.Discard)
}
// If no path is passed to app, normalize to path formath
if dir == "." {
dir, _ = filepath.Abs(dir)
}
if _, err := os.Stat(dir); err != nil {
log.Fatalf("Directory %s not exist", dir)
}
// normalize dir, ending with... /
if strings.HasSuffix(dir, "/") == false {
dir = dir + "/"
}
// build index files in background
go Build_index(dir)
mux := http.NewServeMux()
mux.Handle("/-/assets/", http.HandlerFunc(serve_statics))
mux.Handle("/-/api/dirs", makeGzipHandler(http.HandlerFunc(SearchHandle)))
mux.Handle("/", makeGzipHandler(http.HandlerFunc(handleReq)))
log.Printf("Listening on port %s .....", port)
http.ListenAndServe(port, mux)
}
type AssetDownload struct {
*bytes.Reader
io.Closer
}
func NewAssetDownload(a []byte) *AssetDownload {
return &AssetDownload{
bytes.NewReader(a),
ioutil.NopCloser(nil),
}
}
func serve_statics(w http.ResponseWriter, r *http.Request) {
file := r.URL.Path[10:]
by, err := Asset("data/" + file)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
asset := NewAssetDownload(by)
http.ServeContent(w, r, file, time.Now(), asset)
return
}
func handleReq(w http.ResponseWriter, r *http.Request) {
//Is_Ajax := strings.Contains(r.Header.Get("Accept"), "application/json")
if r.Method == "PUT" {
AjaxUpload(w, r)
return
}
if r.Method == "POST" {
WebCommandHandler(w, r)
return
}
if strings.HasSuffix(r.URL.Path, "/") {
log.Printf("Index dir %s", r.URL.Path)
handleDir(w, r)
} else {
log.Printf("downloading file %s", path.Clean(dir+r.URL.Path))
r.Header.Del("If-Modified-Since")
http.ServeFile(w, r, path.Clean(dir+r.URL.Path))
//http.ServeContent(w, r, r.URL.Path)
//w.Write([]byte("this is a test inside file handler"))
}
}
func handleDir(w http.ResponseWriter, r *http.Request) {
var d string = ""
//log.Printf("len %d,, %s", len(r.URL.Path), dir)
if len(r.URL.Path) == 1 {
// handle root dir
d = dir
} else {
d += dir + r.URL.Path[1:]
}
// handle json format of dir...
if r.FormValue("format") == "json" {
w.Header().Set("Content-Type", "application/json")
result := &DirJson{w, d}
err := result.Get()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
return
}
if r.FormValue("format") == "zip" {
result := &DirZip{w, d}
err := result.Get()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
return
}
// If we dont receive json param... we are asking, for genric app ui...
template_file, err := Asset("data/main.html")
if err != nil {
log.Fatalf("Cant load template main")
}
t := template.Must(template.New("listing").Delims("[%", "%]").Parse(string(template_file)))
v := map[string]interface{}{
"Path": r.URL.Path,
"version": VERSION,
}
w.Header().Set("Content-Type", "text/html")
t.Execute(w, v)
}
func AjaxUpload(w http.ResponseWriter, r *http.Request) {
reader, err := r.MultipartReader()
if err != nil {
fmt.Print(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
pa := r.URL.Path[1:]
for {
part, err := reader.NextPart()
if err == io.EOF {
break
}
var ff string
if dir != "." {
ff = dir + pa + part.FileName()
} else {
ff = pa + part.FileName()
}
dst, err := os.Create(ff)
defer dst.Close()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if _, err := io.Copy(dst, part); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
fmt.Fprint(w, "ok")
return
}
// DirJson handle dir listings in json format
type DirJson struct {
w io.Writer
d string
}
// Get Writes to stdout, json string...
func (t *DirJson) Get() error {
//fmt.Print(t.d)
thedir, err := os.Open(t.d)
if err != nil {
return err
}
defer thedir.Close()
finfo, err := thedir.Readdir(-1)
if err != nil {
return err
}
var aout []*File
for _, fi := range finfo {
xf := &File{
fi.Name(),
fi.Size(),
fi.ModTime(),
fi.IsDir(),
false,
}
// detect is if text file
if fi.IsDir() == false {
xf.IsText = util.IsTextFile(t.d + fi.Name())
}
// if is a symlink ... follow it to test if is a real dir...
if fi.Mode()&os.ModeSymlink == os.ModeSymlink {
//fmt.Printf("%s is a symlink", xf.Name)
fx, err := os.Readlink(t.d + fi.Name())
if err != nil {
continue
}
fxi, err := os.Stat(fx)
if err != nil {
continue
}
//fmt.Printf("%s is a dir %#v\n", t.d+fi.Name(), fxi.IsDir())
// If is a dir, populate object with it.
if fxi.IsDir() {
xf.IsDir = true
}
}
aout = append(aout, xf)
}
xo, err := json.Marshal(aout)
if err != nil {
return err
}
t.w.Write(xo)
return nil
}
type DirZip struct {
w http.ResponseWriter
d string
}
func (t *DirZip) Get() error {
zipFileName := fmt.Sprintf("%s.zip", filepath.Base(t.d))
t.w.Header().Set("Content-Type", "application/zip")
t.w.Header().Set("Content-Disposition", `attachment; filename="`+zipFileName+`"`)
zw := zip.NewWriter(t.w)
defer zw.Close()
filepath.Walk(t.d, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
zipPath := path[len(t.d):]
zipPath = strings.TrimLeft(strings.Replace(zipPath, `\`, "/", -1), `/`)
ze, err := zw.Create(zipPath)
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot create zip entry <%s>: %s\n", zipPath, err)
return err
}
file, err := os.Open(path)
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot open file <%s>: %s\n", path, err)
return err
}
defer file.Close()
io.Copy(ze, file)
return nil
})
return nil
}