Skip to content

Commit a906ad2

Browse files
author
sjqzhang
committed
just rename for small file
1 parent 41f81c9 commit a906ad2

File tree

3 files changed

+28
-20
lines changed

3 files changed

+28
-20
lines changed

build.sh

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
#!/bin/bash
22

3+
rm -rf src
34

5+
git pull
46

57
BIN_VERSION="go-fastdfs:${1-$(git describe --tags `git rev-parse HEAD`)}"
68

7-
if [[ ! -d src ]];then
89

9-
cp -r vendor src
10+
cp -rf vendor src
1011

11-
fi
1212

1313

1414
export GO111MODULE="off"
@@ -19,6 +19,7 @@ cp -rf cmd doc server main.go src/github.com/sjqzhang/go-fastdfs
1919

2020
GOPATH=`pwd` go test -v server/*.go
2121

22+
echo $?
2223
if [[ $? -ne 0 ]];then
2324
echo "test fail"
2425
exit 1

server/http_download.go

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"io"
77
"net/http"
8-
"net/url"
98
"os"
109
"strconv"
1110
"strings"
@@ -15,18 +14,6 @@ import (
1514
log "github.com/sjqzhang/seelog"
1615
)
1716

18-
func (c *Server) SetDownloadHeader(w http.ResponseWriter, r *http.Request) {
19-
w.Header().Set("Content-Type", "application/octet-stream")
20-
if name, ok := r.URL.Query()["name"]; ok {
21-
if v, err := url.QueryUnescape(name[0]); err == nil {
22-
name[0] = c.TrimFileNameSpecialChar(v)
23-
}
24-
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment;filename=%s", name[0]))
25-
} else {
26-
w.Header().Set("Content-Disposition", "attachment")
27-
}
28-
}
29-
3017
func (c *Server) ConsumerDownLoad() {
3118
ConsumerFunc := func() {
3219
for {
@@ -324,7 +311,7 @@ func (c *Server) DownloadSmallFileByURI(w http.ResponseWriter, r *http.Request)
324311
_ = notFound
325312
if data != nil && string(data[0]) == "1" {
326313
if isDownload {
327-
c.SetDownloadHeader(w, r)
314+
c.SetDownloadHeader(w, r, true)
328315
}
329316
if imgWidth != 0 || imgHeight != 0 {
330317
c.ResizeImageByBytes(w, data[1:], uint(imgWidth), uint(imgHeight))
@@ -374,7 +361,7 @@ func (c *Server) DownloadNormalFileByURI(w http.ResponseWriter, r *http.Request)
374361
}
375362
}
376363
if isDownload {
377-
c.SetDownloadHeader(w, r)
364+
c.SetDownloadHeader(w, r, false)
378365
}
379366
fullpath, _ := c.GetFilePathFromRequest(w, r)
380367
if imgWidth != 0 || imgHeight != 0 {
@@ -417,7 +404,7 @@ func (c *Server) DownloadNotFound(w http.ResponseWriter, r *http.Request) {
417404
go c.DownloadFromPeer(peer, fileInfo)
418405
//http.Redirect(w, r, peer+r.RequestURI, 302)
419406
if isDownload {
420-
c.SetDownloadHeader(w, r)
407+
c.SetDownloadHeader(w, r, false)
421408
}
422409
c.DownloadFileToResponse(peer+r.RequestURI, w, r)
423410
return

server/http_header.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package server
22

3-
import "net/http"
3+
import (
4+
"fmt"
5+
"net/http"
6+
"net/url"
7+
)
48

59
func (c *Server) CrossOrigin(w http.ResponseWriter, r *http.Request) {
610
if r.Header.Get("Origin") != "" {
@@ -15,3 +19,19 @@ func (c *Server) CrossOrigin(w http.ResponseWriter, r *http.Request) {
1519
w.Header().Set("Access-Control-Expose-Headers", "Authorization")
1620
//https://blog.csdn.net/yanzisu_congcong/article/details/80552155
1721
}
22+
23+
func (c *Server) SetDownloadHeader(w http.ResponseWriter, r *http.Request, isSmall bool) {
24+
w.Header().Set("Content-Type", "application/octet-stream")
25+
if name, ok := r.URL.Query()["name"]; ok {
26+
if v, err := url.QueryUnescape(name[0]); err == nil {
27+
if isSmall {
28+
name[0] = c.TrimFileNameSpecialChar(v)
29+
} else {
30+
name[0] = v
31+
}
32+
}
33+
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment;filename=%s", name[0]))
34+
} else {
35+
w.Header().Set("Content-Disposition", "attachment")
36+
}
37+
}

0 commit comments

Comments
 (0)