Merge pull request #218 from dutchcoders/ISSUE-217

ISSUE-217
This commit is contained in:
Andrea Spacca 2019-04-29 19:00:34 +02:00 committed by GitHub
commit 6653cfd251
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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
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)
return
}
@ -145,11 +146,6 @@ func (s *Server) previewHandler(w http.ResponseWriter, r *http.Request) {
templatePath = "download.html"
}
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
resolvedUrl := resolveUrl(r, getURL(r).ResolveReference(r.URL), true)
var png []byte
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) {
if f != nil {
f.Close()
os.Remove(f.Name())
err := f.Close()
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("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-")
if err != nil {
log.Printf("%s", err.Error())
@ -902,12 +915,19 @@ func (s *Server) getHandler(w http.ResponseWriter, r *http.Request) {
defer cleanTmpFile(file)
tee := io.TeeReader(reader, file)
_, err = ioutil.ReadAll(tee)
for {
b := make([]byte, _5M)
_, err = tee.Read(b)
if err == io.EOF {
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)
}

View file

@ -59,7 +59,10 @@ import (
const SERVER_INFO = "transfer.sh"
// 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)