Replace io.Copy w/ http.ServeContent for download

This commit is contained in:
Daan Sprenkels 2019-12-19 20:01:17 +04:00
parent 8dce4e8483
commit 5e6ce9c2be

View File

@ -3,7 +3,6 @@ package rushlink
import ( import (
"crypto/subtle" "crypto/subtle"
"fmt" "fmt"
"io"
"log" "log"
"mime/multipart" "mime/multipart"
"net/http" "net/http"
@ -95,12 +94,17 @@ func (rl *rushlink) uploadFileGetHandler(w http.ResponseWriter, r *http.Request)
// unexpected error // unexpected error
panic(err) panic(err)
} }
w.Header().Set("Content-Type", fu.ContentType) info, err := file.Stat()
w.WriteHeader(http.StatusOK) var modtime time.Time
if r.Method == "HEAD" { if err != nil {
return log.Printf("error: %v", errors.Wrapf(err, "could not stat file '%v'", filePath))
} else {
modtime = info.ModTime()
} }
io.Copy(w, file) // We use http.ServeContent (instead of http.ServeFile) because we cannot
// use http.ServeFile together with the assertion that the file exists,
// without introducing a TOCTOU flaw.
http.ServeContent(w, r, fu.FileName, modtime, file)
} }
func (rl *rushlink) viewPasteHandler(w http.ResponseWriter, r *http.Request) { func (rl *rushlink) viewPasteHandler(w http.ResponseWriter, r *http.Request) {