Skip to content

Commit

Permalink
🐛 close #558 fix local file can't download
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Feb 18, 2022
1 parent 192d0f2 commit 9778880
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions server/common/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ func Proxy(w http.ResponseWriter, r *http.Request, link *base.Link, file *model.
defer func() {
_ = link.Data.Close()
}()
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/octet-stream")
w.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename=%s`, url.QueryEscape(file.Name)))
w.Header().Set("Content-Length", strconv.FormatInt(file.Size, 10))
w.WriteHeader(http.StatusOK)
_, err = io.Copy(w, link.Data)
if err != nil {
return err
Expand All @@ -47,6 +47,8 @@ func Proxy(w http.ResponseWriter, r *http.Request, link *base.Link, file *model.
if err != nil {
return err
}
w.Header().Set("Content-Type", "application/octet-stream")
w.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename=%s`, url.QueryEscape(file.Name)))
http.ServeContent(w, r, file.Name, fileStat.ModTime(), f)
return nil
} else {
Expand All @@ -68,16 +70,16 @@ func Proxy(w http.ResponseWriter, r *http.Request, link *base.Link, file *model.
_ = res.Body.Close()
}()
log.Debugf("proxy status: %d", res.StatusCode)
for h, v := range res.Header {
w.Header()[h] = v
}
w.WriteHeader(res.StatusCode)
if res.StatusCode >= 400 {
all, _ := ioutil.ReadAll(res.Body)
msg := string(all)
log.Debugln(msg)
return errors.New(msg)
}
for h, v := range res.Header {
w.Header()[h] = v
}
_, err = io.Copy(w, res.Body)
if err != nil {
return err
Expand Down

0 comments on commit 9778880

Please sign in to comment.