1
0
mirror of https://github.com/dutchcoders/transfer.sh.git synced 2020-11-18 19:53:40 -08:00

ISSUE-217

This commit is contained in:
Andrea Spacca 2019-04-28 21:36:45 +02:00
parent cd602b9256
commit b5b58f9b3b
2 changed files with 37 additions and 14 deletions

View File

@ -126,7 +126,8 @@ func (s *Server) previewHandler(w http.ResponseWriter, r *http.Request) {
} }
var data []byte var data []byte
if data, err = ioutil.ReadAll(reader); err != nil { data = make([]byte, _5M)
if _, err = reader.Read(data); err != io.EOF && err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
return return
} }
@ -145,11 +146,6 @@ func (s *Server) previewHandler(w http.ResponseWriter, r *http.Request) {
templatePath = "download.html" templatePath = "download.html"
} }
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
resolvedUrl := resolveUrl(r, getURL(r).ResolveReference(r.URL), true) resolvedUrl := resolveUrl(r, getURL(r).ResolveReference(r.URL), true)
var png []byte var png []byte
png, err = qrcode.Encode(resolvedUrl, qrcode.High, 150) png, err = qrcode.Encode(resolvedUrl, qrcode.High, 150)
@ -336,8 +332,15 @@ func (s *Server) postHandler(w http.ResponseWriter, r *http.Request) {
func cleanTmpFile(f *os.File) { func cleanTmpFile(f *os.File) {
if f != nil { if f != nil {
f.Close() err := f.Close()
os.Remove(f.Name()) if err != nil {
log.Printf("Error closing tmpfile: %s (%s)", err, f.Name())
}
err = os.Remove(f.Name())
if err != nil {
log.Printf("Error removing tmpfile: %s (%s)", err, f.Name())
}
} }
} }
@ -892,6 +895,16 @@ func (s *Server) getHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Disposition", fmt.Sprintf("%s; filename=\"%s\"", disposition, filename)) w.Header().Set("Content-Disposition", fmt.Sprintf("%s; filename=\"%s\"", disposition, filename))
w.Header().Set("Connection", "keep-alive") w.Header().Set("Connection", "keep-alive")
if w.Header().Get("Range") == "" {
if _, err = io.Copy(w, reader); err != nil {
log.Printf("%s", err.Error())
http.Error(w, "Error occurred copying to output stream", 500)
return
}
return
}
file, err := ioutil.TempFile(s.tempPath, "range-") file, err := ioutil.TempFile(s.tempPath, "range-")
if err != nil { if err != nil {
log.Printf("%s", err.Error()) log.Printf("%s", err.Error())
@ -902,11 +915,18 @@ func (s *Server) getHandler(w http.ResponseWriter, r *http.Request) {
defer cleanTmpFile(file) defer cleanTmpFile(file)
tee := io.TeeReader(reader, file) tee := io.TeeReader(reader, file)
_, err = ioutil.ReadAll(tee) for {
if err != nil { b := make([]byte, _5M)
log.Printf("%s", err.Error()) _, err = tee.Read(b)
http.Error(w, "Error occurred copying to output stream", 500) if err == io.EOF {
return break
}
if err != nil {
log.Printf("%s", err.Error())
http.Error(w, "Error occurred copying to output stream", 500)
return
}
} }
http.ServeContent(w, r, filename, time.Now(), file) http.ServeContent(w, r, filename, time.Now(), file)

View File

@ -59,7 +59,10 @@ import (
const SERVER_INFO = "transfer.sh" const SERVER_INFO = "transfer.sh"
// parse request with maximum memory of _24Kilobits // parse request with maximum memory of _24Kilobits
const _24K = (1 << 10) * 24 const _24K = (1 << 3) * 24
// parse request with maximum memory of _5Megabytes
const _5M = (1 << 20) * 5
type OptionFn func(*Server) type OptionFn func(*Server)