mirror of
https://github.com/dutchcoders/transfer.sh.git
synced 2024-12-25 03:40:19 +01:00
commit
6653cfd251
2 changed files with 37 additions and 14 deletions
|
@ -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,11 +915,18 @@ func (s *Server) getHandler(w http.ResponseWriter, r *http.Request) {
|
|||
defer cleanTmpFile(file)
|
||||
|
||||
tee := io.TeeReader(reader, file)
|
||||
_, err = ioutil.ReadAll(tee)
|
||||
if err != nil {
|
||||
log.Printf("%s", err.Error())
|
||||
http.Error(w, "Error occurred copying to output stream", 500)
|
||||
return
|
||||
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)
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
Loading…
Reference in a new issue