Skip to content

Commit

Permalink
just rename for small file
Browse files Browse the repository at this point in the history
  • Loading branch information
sjqzhang committed Sep 19, 2024
1 parent 41f81c9 commit a906ad2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 20 deletions.
7 changes: 4 additions & 3 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#!/bin/bash

rm -rf src

git pull

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

if [[ ! -d src ]];then

cp -r vendor src
cp -rf vendor src

fi


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

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

echo $?
if [[ $? -ne 0 ]];then
echo "test fail"
exit 1
Expand Down
19 changes: 3 additions & 16 deletions server/http_download.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"io"
"net/http"
"net/url"
"os"
"strconv"
"strings"
Expand All @@ -15,18 +14,6 @@ import (
log "github.com/sjqzhang/seelog"
)

func (c *Server) SetDownloadHeader(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/octet-stream")
if name, ok := r.URL.Query()["name"]; ok {
if v, err := url.QueryUnescape(name[0]); err == nil {
name[0] = c.TrimFileNameSpecialChar(v)
}
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment;filename=%s", name[0]))
} else {
w.Header().Set("Content-Disposition", "attachment")
}
}

func (c *Server) ConsumerDownLoad() {
ConsumerFunc := func() {
for {
Expand Down Expand Up @@ -324,7 +311,7 @@ func (c *Server) DownloadSmallFileByURI(w http.ResponseWriter, r *http.Request)
_ = notFound
if data != nil && string(data[0]) == "1" {
if isDownload {
c.SetDownloadHeader(w, r)
c.SetDownloadHeader(w, r, true)
}
if imgWidth != 0 || imgHeight != 0 {
c.ResizeImageByBytes(w, data[1:], uint(imgWidth), uint(imgHeight))
Expand Down Expand Up @@ -374,7 +361,7 @@ func (c *Server) DownloadNormalFileByURI(w http.ResponseWriter, r *http.Request)
}
}
if isDownload {
c.SetDownloadHeader(w, r)
c.SetDownloadHeader(w, r, false)
}
fullpath, _ := c.GetFilePathFromRequest(w, r)
if imgWidth != 0 || imgHeight != 0 {
Expand Down Expand Up @@ -417,7 +404,7 @@ func (c *Server) DownloadNotFound(w http.ResponseWriter, r *http.Request) {
go c.DownloadFromPeer(peer, fileInfo)
//http.Redirect(w, r, peer+r.RequestURI, 302)
if isDownload {
c.SetDownloadHeader(w, r)
c.SetDownloadHeader(w, r, false)
}
c.DownloadFileToResponse(peer+r.RequestURI, w, r)
return
Expand Down
22 changes: 21 additions & 1 deletion server/http_header.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package server

import "net/http"
import (
"fmt"
"net/http"
"net/url"
)

func (c *Server) CrossOrigin(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Origin") != "" {
Expand All @@ -15,3 +19,19 @@ func (c *Server) CrossOrigin(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Expose-Headers", "Authorization")
//https://blog.csdn.net/yanzisu_congcong/article/details/80552155
}

func (c *Server) SetDownloadHeader(w http.ResponseWriter, r *http.Request, isSmall bool) {
w.Header().Set("Content-Type", "application/octet-stream")
if name, ok := r.URL.Query()["name"]; ok {
if v, err := url.QueryUnescape(name[0]); err == nil {
if isSmall {
name[0] = c.TrimFileNameSpecialChar(v)
} else {
name[0] = v
}
}
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment;filename=%s", name[0]))
} else {
w.Header().Set("Content-Disposition", "attachment")
}
}

0 comments on commit a906ad2

Please sign in to comment.