-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.go
878 lines (784 loc) · 21.7 KB
/
server.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
package main
import (
"bytes"
"compress/gzip"
"errors"
"flag"
"fmt"
"io"
"math"
"net"
"net/http"
"net/url"
"os"
"path/filepath"
"runtime"
"sort"
"strconv"
"strings"
"sync"
"text/template"
"time"
"github.com/vbauerster/mpb/v8"
"github.com/vbauerster/mpb/v8/decor"
"gopkg.in/natefinch/lumberjack.v2"
"gopkg.in/yaml.v3"
)
func serverMain(exe string, args []string) error {
fs := flag.NewFlagSet(exe, flag.ExitOnError)
cnf := fs.String("c", "server.yaml", "config file")
path := fs.String("p", "", "path")
listen := fs.String("s", "", "ip:port")
reg := fs.Bool("reg", false, "add right click registry")
err := fs.Parse(args)
if err != nil {
return err
}
var config struct {
Listen string `yaml:"listen"`
Path string `yaml:"path"`
Auth string `yaml:"auth"`
Timeout string `yaml:"timeout"`
Deny bool `yaml:"deny"`
Certificate struct {
Domain string `yaml:"domain"`
Cert string `yaml:"cert"`
Key string `yaml:"key"`
} `yaml:"certificate"`
Log struct {
Logger *lumberjack.Logger `yaml:"logger"`
Template string `yaml:"template"`
} `yaml:"log"`
}
if data, err := os.ReadFile(*cnf); err == nil {
// 读取配置文件成功时才解析配置,否则只生效参数配置
if err = yaml.Unmarshal(data, &config); err != nil {
return err
}
}
// 常用参数覆盖配置文件
if *path != "" {
config.Path = *path
}
if *listen != "" {
config.Listen = *listen
}
var user, pass string
if config.Auth != "" {
var ok bool
user, pass, ok = strings.Cut(config.Auth, ":")
if !ok || user == "" || pass == "" {
return errors.New("invalid auth")
}
}
timeout := time.Minute
if t, err := time.ParseDuration(config.Timeout); err == nil {
timeout = t // 配置文件读取超时时间成功
}
tcpAddr, err := net.ResolveTCPAddr("tcp", config.Listen)
if err != nil {
return err
}
if runtime.GOOS == `windows` && *reg {
if tcpAddr.Port < 80 {
return fmt.Errorf("usage: %s -s ip:port -reg", exe)
}
return createRegFile(exe, tcpAddr.String())
}
uri := &url.URL{Scheme: schemeHttp}
if config.Certificate.Cert != "" || config.Certificate.Key != "" {
if config.Certificate.Cert == "" {
return errors.New("cert file is null")
}
if config.Certificate.Key == "" {
return errors.New("key file is null")
}
uri.Scheme = schemeHttps
}
addr, err := net.ListenTCP("tcp", tcpAddr)
if err != nil {
return err
}
var urls []string
if len(tcpAddr.IP) == 0 {
if ips := InternalIp(); len(ips) > 0 {
for _, v := range ips {
tcpAddr.IP = v // 拼接本机所有ip:port
uri.Host = tcpAddr.String()
urls = append(urls, uri.String())
}
}
tcpAddr.IP = net.IPv4(127, 0, 0, 1)
}
if config.Certificate.Domain == "" {
uri.Host = tcpAddr.String()
} else {
uri.Host = config.Certificate.Domain
}
if !((uri.Scheme == schemeHttp && tcpAddr.Port == 80) ||
(uri.Scheme == schemeHttps && tcpAddr.Port == 443)) {
// 不是特殊协议和端口,需要拼接端口,特殊协议不需要带上端口
uri.Host = net.JoinHostPort(uri.Host, strconv.Itoa(tcpAddr.Port))
}
urls = append(urls, uri.String())
addrStr := uri.String()
config.Path, err = filepath.Abs(config.Path)
if err != nil {
return err
}
tpl, err := template.New("").Parse(`{{range $i,$v := .urls}}
web service: {{$v}}
{{- end}}
server:
{{.exec}} -s {{.listen}} -p {{.path}}
registry:
{{.exec}} -s {{.listen}} -reg
cli get:
{{.exec}} cli -c{{if or .user .pass}} -auth "{{.user}}:{{.pass}}"{{end}}{{if .https}} -ca ca.crt{{end}} -o C:\{{.example}} "{{.addr}}/{{.example}}"
cli post:
{{.exec}} cli -c{{if or .user .pass}} -auth "{{.user}}:{{.pass}}"{{end}}{{if .https}} -ca ca.crt{{end}} -d @C:\{{.example}} "{{.addr}}/{{.example}}"
Get File:
wget {{if .https}}--ca-certificate ca.crt {{end}}{{if or .user .pass}}--user "{{.user}}" --password "{{.pass}}" {{end}}-c --content-disposition "{{.addr}}/{{.example}}"
curl {{if .https}}--cacert ca.crt {{end}}{{if or .user .pass}}-u "{{.user}}:{{.pass}}" {{end}}-C - -OJ "{{.addr}}/{{.example}}"
Post File:
wget {{if .https}}--ca-certificate ca.crt {{end}}{{if or .user .pass}}--user "{{.user}}" --password "{{.pass}}" {{end}}-qO - --post-file=C:\{{.example}} "{{.addr}}/{{.example}}"
curl {{if .https}}--cacert ca.crt {{end}}{{if or .user .pass}}-u "{{.user}}:{{.pass}}" {{end}}--data-binary @C:\{{.example}} "{{.addr}}/{{.example}}"
curl {{if .https}}--cacert ca.crt {{end}}{{if or .user .pass}}-u "{{.user}}:{{.pass}}" {{end}}-F "file=@C:\{{.example}}" "{{.addr}}/{{.example}}/"
Get Offset:
curl {{if .https}}--cacert ca.crt {{end}}{{if or .user .pass}}-u "{{.user}}:{{.pass}}" {{end}}-H "Content-Type:application/offset" "{{.addr}}/{{.example}}"
wget {{if .https}}--ca-certificate ca.crt {{end}}{{if or .user .pass}}--user "{{.user}}" --password "{{.pass}}" {{end}}-qO - --header "Content-Type:application/offset" "{{.addr}}/{{.example}}"
Put File:
curl {{if .https}}--cacert ca.crt {{end}}{{if or .user .pass}}-u "{{.user}}:{{.pass}}" {{end}}-C - -T C:\{{.example}} "{{.addr}}/{{.example}}"
`)
if err != nil {
return err
}
err = tpl.Execute(os.Stdout, map[string]any{
"urls": urls,
"exec": exe,
"listen": addr.Addr().String(),
"path": config.Path,
"user": user,
"pass": pass,
"https": uri.Scheme == schemeHttps,
"example": "example.txt",
"addr": addrStr,
})
if err != nil {
return err
}
srh := &fileServer{
path: config.Path,
pBar: newMpbProgress(),
scheme: uri.Scheme,
auth: config.Auth,
deny: config.Deny,
out: os.Stdout,
}
if config.Log.Logger != nil && config.Log.Logger.Filename != "-" {
//goland:noinspection GoUnhandledErrorResult
defer config.Log.Logger.Close()
// log.logger.filename = "-" 或 不配置log.logger 时日志输出到 stdout
// 否则按照日志库的配置输出日志,支持日志文件按大小和天数切分
srh.out = config.Log.Logger
}
if config.Log.Template != "" {
// 当配置日志模板时,使用自定义模板输出日志
srh.log, err = template.New("").Funcs(template.FuncMap{
"time": func(layout string) any {
switch now := time.Now(); layout {
case "Unix":
return now.Unix()
case "UnixMilli":
return now.UnixMilli()
case "UnixMicro":
return now.UnixMicro()
case "UnixNano":
return now.UnixNano()
default:
return now.Format(layout)
}
},
}).Parse(config.Log.Template)
if err != nil {
return err
}
}
srv := &http.Server{
Addr: addrStr,
Handler: srh,
ReadTimeout: timeout,
ReadHeaderTimeout: timeout,
}
if uri.Scheme == schemeHttps {
return srv.ServeTLS(addr, config.Certificate.Cert, config.Certificate.Key)
}
return srv.Serve(addr)
}
type fileServer struct {
path string
pBar *mpb.Progress
scheme string
auth string
deny bool
out io.Writer
log *template.Template
}
const (
headerType = "Content-Type"
headerLength = "Content-Length"
headerRange = "Range"
offsetLength = "Offset-Length"
offsetAppend = "append"
typeDefault = "application/x-www-form-urlencoded" // curl,wget默认
typeGzip = "application/x-gzip"
typeOffset = "application/offset"
schemeHttp = "http"
schemeHttps = "https"
)
var (
respOk = []byte("ok")
)
//goland:noinspection GoUnhandledErrorResult
func (fs *fileServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Body != nil {
//goland:noinspection GoUnhandledErrorResult
defer r.Body.Close()
}
if r.RequestURI == "/favicon.ico" {
w.Write(icoData) // 返回网页的图标
return
}
var err error
if fs.auth != "" {
if user, pass, ok := r.BasicAuth(); !ok || user+":"+pass != fs.auth {
w.Header().Add("WWW-Authenticate", `Basic realm="Please Authenticate"`)
err = &webErr{code: http.StatusUnauthorized, msg: "Authenticate Error"}
}
}
if err == nil {
pool := bytePool.Get().(*poolByte)
defer bytePool.Put(pool)
if fs.log != nil {
buf := bytes.NewBuffer(pool.buf[:0])
err = fs.log.Execute(buf, map[string]any{
"req": r,
})
//goland:noinspection GoUnhandledErrorResult
if err != nil {
fmt.Fprintf(fs.out, "log error: %v\n", err)
} else {
fmt.Fprintf(fs.out, "%s", buf.Bytes())
}
}
switch r.Method {
case http.MethodGet:
err = fs.get(w, r, pool.buf)
case http.MethodPost:
err = fs.post(w, r, pool.buf)
case http.MethodHead:
err = fs.head(w, r)
case http.MethodPut:
err = fs.put(w, r, pool.buf)
default:
err = &webErr{
code: http.StatusMethodNotAllowed,
msg: r.Method + " not support",
}
}
}
if err != nil {
var e *webErr
if !errors.As(err, &e) {
e = &webErr{msg: "Internal Server Error", err: err}
}
if e.code == 0 {
e.code = http.StatusInternalServerError
}
w.Header().Set(headerType, "text/html;charset=utf-8")
w.WriteHeader(e.code)
fmt.Fprintf(w, `<html><head><title>UpDownFile</title></head><body><center><h1>%s</h1></center><hr><center>%v</center></body></html>`, e.msg, e.err)
fmt.Fprintf(fs.out, "code:%d,msg:%s,err:%v\n", e.code, e.msg, e.err)
}
}
type webErr struct {
err error
msg string
code int
}
func (w *webErr) Error() string {
return w.msg
}
func (fs *fileServer) open(r *http.Request) (fr *os.File, fi os.FileInfo, err error) {
fr, err = os.Open(filepath.Join(fs.path, r.URL.Path))
if err != nil {
if os.IsNotExist(err) {
err = &webErr{
code: http.StatusNotFound,
msg: "404 Not Found",
err: err,
}
}
return
}
fi, err = fr.Stat()
if err != nil {
_ = fr.Close()
} else if fs.deny && fi.IsDir() {
err = &webErr{
code: http.StatusForbidden,
msg: "deny directory request",
}
}
return
}
func (fs *fileServer) head(w http.ResponseWriter, r *http.Request) error {
fr, fi, err := fs.open(r)
if err != nil {
return err
}
http.ServeContent(w, r, fr.Name(), fi.ModTime(), fr)
_ = fr.Close()
return nil
}
var dirHtmlTpl = sync.OnceValue(func() *template.Template {
// 浏览器获取目录时显示一个简易的web页面,有需要时只加载1次
return template.Must(template.New("").Parse(`<html lang="zh"><head><title>list dir</title></head><body>
<div style="position:fixed;bottom:20px;right:10px"><p>
<label><input type="radio" name="sort" onclick="sortDir(0)"{{if eq .sort 0}}checked{{end}}>名称升序</label>
<label><input type="radio" name="sort" onclick="sortDir(1)"{{if eq .sort 1}}checked{{end}}>名称降序</label>
</p><p>
<label><input type="radio" name="sort" onclick="sortDir(2)"{{if eq .sort 2}}checked{{end}}>时间升序</label>
<label><input type="radio" name="sort" onclick="sortDir(3)"{{if eq .sort 3}}checked{{end}}>时间降序</label>
</p><p>
<label><input type="radio" name="sort" onclick="sortDir(4)"{{if eq .sort 4}}checked{{end}}>大小升序</label>
<label><input type="radio" name="sort" onclick="sortDir(5)"{{if eq .sort 5}}checked{{end}}>大小降序</label>
</p><p>
<label><input type="radio" name="sort" onclick="sortDir(6)"{{if eq .sort 6}}checked{{end}}>后缀升序</label>
<label><input type="radio" name="sort" onclick="sortDir(7)"{{if eq .sort 7}}checked{{end}}>后缀降序</label>
</p>
<p><input type="file" id="file"></p>
<progress value="0" id="progress"></progress>
<p><input type="button" onclick="uploadFile()" value="上传文件"></p>
<input type="button" onclick="backSuper()" value="返回上级"/>
<a href="#top" style="margin:5px">顶部</a>
<a href="#bottom">底部</a>
</div>
<style>td:nth-child(3){text-align:right}</style>
<table border="1" align="center">
<tr><th>序号</th><th>类型</th><th>大小</th><th>修改时间</th><th>链接</th></tr>
{{- range $i,$v := .info}}
<tr><td>{{$v.Index}}</td><td>{{$v.Type}}</td><td>{{$v.Size}}</td><td>{{$v.Time}}</td><td><a href="{{$v.Href}}"{{if eq $v.Type "F"}}download{{end}}>{{$v.Name}}</a></td></tr>
{{- end}}
</table>
<a name="bottom"></a>
<script>
function uploadFile() {
let upload = document.getElementById('file').files[0]
if (!upload) {
alert('请选择上传文件')
return
}
let params = new FormData()
params.append('file', upload)
let xhr = new XMLHttpRequest()
xhr.onerror = function() {
alert('请求失败')
}
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
if (xhr.responseText === "ok") {
window.location.reload()
} else {
alert(xhr.responseText)
}
} else {
alert(xhr.status)
}
}
}
let progress = document.getElementById('progress')
xhr.upload.onprogress = function(e) {
progress.value = e.loaded
progress.max = e.total
}
xhr.open('POST', window.location.pathname, true)
xhr.send(params)
}
function sortDir(type) {
window.location.href = window.location.origin + window.location.pathname + '?sort=' + type
}
function backSuper() {
let url = window.location.pathname
let i = url.length - 1
for (;i >= 0 && url[i] === '/';i--){}
for (;i >= 0 && url[i] !== '/';i--){}
window.location.href = window.location.origin + url.substring(0,i+1)
}</script></body></html>`))
})
func (fs *fileServer) get(w http.ResponseWriter, r *http.Request, buf []byte) error {
fr, fi, err := fs.open(r)
if err != nil {
return err
}
//goland:noinspection GoUnhandledErrorResult
defer fr.Close()
size := fi.Size()
w.Header().Set(offsetLength, string(strconv.AppendInt(buf[:0], size, 10)))
ht := r.Header.Get(headerType)
if ht == typeOffset {
if fi.IsDir() {
return &webErr{
code: http.StatusForbidden,
msg: "unable to get directory size",
}
}
return fs.offset(w, r, size)
}
switch {
case fi.IsDir():
dir, sortNum, err := fs.sortDir(fr, r.FormValue("sort"))
if err != nil {
return err
}
type lineFileInfo struct {
Type string
Size string
Time string
Href string
Name string
Index int
}
info := make([]lineFileInfo, len(dir))
for i, v := range dir {
tmp := lineFileInfo{
Index: i + 1,
Size: convertByte(v.Size(), false),
Time: string(v.ModTime().AppendFormat(buf[:0], time.DateTime)),
Name: v.Name(),
}
href := append(buf[:0], url.PathEscape(v.Name())...)
if v.IsDir() {
tmp.Type = "D"
href = append(href, '/')
} else {
tmp.Type = "F"
}
tmp.Href = string(href)
info[i] = tmp
}
err = dirHtmlTpl().Execute(w, map[string]any{
"sort": sortNum,
"info": info,
})
if err != nil {
return err
}
case ht == typeGzip:
gw, _ := gzip.NewWriterLevel(w, gzip.BestCompression)
pw := &progressBar{w: gw, b: newMpbBar(fs.pBar, http.MethodGet, fr.Name(), size)}
_, err = io.CopyBuffer(pw, fr, buf)
pw.Close()
_ = gw.Close()
if err != nil {
return err
}
default:
pb := newMpbBar(fs.pBar, http.MethodGet, fr.Name(), 0)
pw := &progressBar{ResponseWriter: w, w: w, b: pb, fn: func() {
t, e := parseInt64(w.Header().Get(headerLength))
pb.SetTotal(t, e != nil) // 延迟设置进度条,适应分片下载逻辑
}}
http.ServeContent(pw, r, fr.Name(), fi.ModTime(), fr)
pw.Close()
}
return nil
}
func newMpbProgress() *mpb.Progress {
return mpb.New(
mpb.WithWidth(30),
mpb.PopCompletedMode(), // 进度条完成后不再渲染
mpb.WithAutoRefresh(),
)
}
func newMpbBar(bar *mpb.Progress, mode, path string, size int64) *mpb.Bar {
return bar.AddBar(size, mpb.AppendDecorators(
decor.Any(func(st decor.Statistics) string {
var p int64
switch {
case st.Total <= 0:
case st.Current >= st.Total:
p = 100
default:
p = 100 * st.Current / st.Total
}
cur := convertByte(st.Current, true)
if st.Completed {
return fmt.Sprintf("done %s %s %s", cur, mode, path)
}
return fmt.Sprintf("%3d%% %s %s %s", p, cur, mode, path)
}, decor.WCSyncWidth),
))
}
type progressBar struct {
http.ResponseWriter
fn func()
w io.Writer
r io.Reader
b *mpb.Bar
}
func (pb *progressBar) Write(p []byte) (n int, err error) {
if pb.fn != nil {
pb.fn() // execute only once
pb.fn = nil
}
n, err = pb.w.Write(p)
pb.b.IncrBy(n)
return
}
func (pb *progressBar) Read(p []byte) (n int, err error) {
n, err = pb.r.Read(p)
pb.b.IncrBy(n)
return
}
func (pb *progressBar) Close() {
pb.b.SetCurrent(math.MaxInt64) // 使进度达到100%
pb.b.EnableTriggerComplete() // 确保处于完成状态
}
const (
sortDirTypeByNameAsc = iota
sortDirTypeByNameDesc
sortDirTypeByTimeAsc
sortDirTypeByTimeDesc
sortDirTypeBySizeAsc
sortDirTypeBySizeDesc
sortDirTypeByExtAsc
sortDirTypeByExtDesc
)
type dirSort struct {
fi []os.FileInfo
st int
}
func (d *dirSort) Len() int {
return len(d.fi)
}
func (d *dirSort) defaultSort(x, y int) bool {
lx, ly := len(d.fi[x].Name()), len(d.fi[y].Name())
if lx == ly {
return d.fi[x].Name() < d.fi[y].Name()
}
return lx < ly
}
func (d *dirSort) Less(x, y int) bool {
if d.fi[x].IsDir() != d.fi[y].IsDir() {
return d.fi[x].IsDir() // 文件夹永远排在文件前面
}
switch d.st {
default:
fallthrough // 默认使用文件名升序
case sortDirTypeByNameAsc:
return d.defaultSort(x, y)
case sortDirTypeByNameDesc:
lx, ly := len(d.fi[x].Name()), len(d.fi[y].Name())
if lx == ly {
return d.fi[x].Name() > d.fi[y].Name()
}
return lx > ly
case sortDirTypeByTimeAsc:
tx, ty := d.fi[x].ModTime(), d.fi[y].ModTime()
if tx.Unix() == ty.Unix() {
return d.defaultSort(x, y)
}
return tx.Before(ty)
case sortDirTypeByTimeDesc:
tx, ty := d.fi[x].ModTime(), d.fi[y].ModTime()
if tx.Unix() == ty.Unix() {
return d.defaultSort(x, y)
}
return tx.After(ty)
case sortDirTypeBySizeAsc:
sx, sy := d.fi[x].Size(), d.fi[y].Size()
if sx == sy {
return d.defaultSort(x, y)
}
return sx < sy
case sortDirTypeBySizeDesc:
sx, sy := d.fi[x].Size(), d.fi[y].Size()
if sx == sy {
return d.defaultSort(x, y)
}
return sx > sy
case sortDirTypeByExtAsc:
if !d.fi[x].IsDir() && !d.fi[y].IsDir() {
return filepath.Ext(d.fi[x].Name()) < filepath.Ext(d.fi[y].Name())
}
return d.defaultSort(x, y)
case sortDirTypeByExtDesc:
if !d.fi[x].IsDir() && !d.fi[y].IsDir() {
return filepath.Ext(d.fi[x].Name()) > filepath.Ext(d.fi[y].Name())
}
return d.defaultSort(x, y)
}
}
func (d *dirSort) Swap(x, y int) {
d.fi[x], d.fi[y] = d.fi[y], d.fi[x]
}
func (fs *fileServer) sortDir(dir *os.File, s string) (list []os.FileInfo, st int, err error) {
st, _ = strconv.Atoi(s)
if st < sortDirTypeByNameAsc || st > sortDirTypeByExtDesc {
st = sortDirTypeByNameAsc
}
list, err = dir.Readdir(-1)
if err != nil {
return
}
sort.Sort(&dirSort{fi: list, st: st})
return
}
func (fs *fileServer) post(w io.Writer, r *http.Request, buf []byte) error {
if r.Body == nil {
return &webErr{
code: http.StatusBadRequest,
msg: "body is null",
}
}
var (
path = filepath.Join(fs.path, r.URL.Path)
fr io.ReadCloser
err error
size int64
fg = flagW
)
switch ht := r.Header.Get(headerType); ht {
case typeDefault: // curl,wget,cli 这三种方式上传
size, err = parseInt64(r.Header.Get(headerLength))
if err != nil {
return err
}
if r.Header.Get(offsetLength) == offsetAppend {
fg = flagA // 客户端告诉服务器断点上传
}
fr = r.Body
case typeGzip:
fr, err = gzip.NewReader(r.Body)
if err != nil {
return err
}
// 服务器解析gzip数据,直接使用自定义长度
size, err = parseInt64(r.Header.Get(offsetLength))
if err != nil {
return err
}
default:
// 浏览器上传 或 curl -F "file=@xx"
// 参考 r.FormFile("file") 的流程,避免生成临时文件
mr, err := r.MultipartReader()
if err != nil {
return err
}
np, err := mr.NextPart()
if err != nil {
return err
}
fn := np.FileName()
if fn == "" || np.FormName() != "file" {
return http.ErrMissingFile
}
fr, path = np, filepath.Join(path, fn)
size, err = parseInt64(r.Header.Get(headerLength))
if err != nil {
return err
}
}
//goland:noinspection GoUnhandledErrorResult
defer fr.Close()
fw, err := os.OpenFile(path, fg, fileMode)
if err != nil {
return err
}
pw := &progressBar{w: fw, b: newMpbBar(fs.pBar, http.MethodPost, path, size)}
_, err = io.CopyBuffer(pw, fr, buf)
pw.Close()
_ = fw.Close()
if err != nil {
return err
}
_, err = w.Write(respOk)
return err
}
func (fs *fileServer) put(w io.Writer, r *http.Request, buf []byte) error {
if r.Body == nil {
return &webErr{
code: http.StatusBadRequest,
msg: "body is null",
}
}
var (
fw *os.File
cur int64
size int64
path = filepath.Join(fs.path, r.URL.Path)
)
fi, err := os.Stat(path)
if err == nil {
// 文件存在,检查客户端断点续传Header
if cur, _, size, err = scanRangeSize(r.Header); err == nil {
fw, err = os.OpenFile(path, os.O_RDWR|os.O_CREATE, fileMode)
if err != nil {
return err
}
//goland:noinspection GoUnhandledErrorResult
defer fw.Close()
nSize := fi.Size()
if nSize >= size {
return &webErr{
code: http.StatusConflict,
msg: "file upload is complete",
}
}
// 需要返回客户端断点上传的命令,指定文件偏移
if (cur == 0 && nSize > 0) || cur > nSize {
return fs.offset(w, r, nSize)
}
if cur > 0 { // 从指定位置继续写文件
_, err = fw.Seek(cur, io.SeekStart)
if err != nil {
return err
}
}
}
}
if fw == nil {
fw, err = os.OpenFile(path, flagW, fileMode)
if err != nil {
return err
}
//goland:noinspection GoUnhandledErrorResult
defer fw.Close()
}
size, err = parseInt64(r.Header.Get(headerLength))
if err != nil {
return err
}
pw := &progressBar{w: fw, b: newMpbBar(fs.pBar, http.MethodPut, path, size)}
_, err = io.CopyBuffer(pw, r.Body, buf)
pw.Close()
if err != nil {
return err
}
_, err = w.Write(respOk)
return err
}
func (fs *fileServer) offset(w io.Writer, r *http.Request, size int64) (err error) {
var ( // 返回客户端断点上传的命令行参数
uri = &url.URL{Scheme: fs.scheme, Host: r.Host, Path: r.RequestURI}
name = filepath.Base(uri.Path)
)
if size == 0 {
_, err = fmt.Fprintf(w, "curl -T %s %s\n", name, uri)
} else {
_, err = fmt.Fprintf(w, "curl -C %d -T %s %s\n", size, name, uri)
}
return
}