From c422436a295dd6ad709521c0c0828369e2c84597 Mon Sep 17 00:00:00 2001 From: Ronnie Roller Date: Sat, 8 Feb 2020 17:56:29 -0800 Subject: [PATCH] use http.ServeFile --- src/media/fetch.go | 2 +- src/media/serve.go | 29 ++++++++++------------------- 2 files changed, 11 insertions(+), 20 deletions(-) diff --git a/src/media/fetch.go b/src/media/fetch.go index d5e2147..0ba05f5 100644 --- a/src/media/fetch.go +++ b/src/media/fetch.go @@ -76,7 +76,7 @@ func FetchMediaApi(w http.ResponseWriter, r *http.Request) { } // just take the first one - streamFileToClientById(w, response.Medias[0].Id) + streamFileToClientById(w, r, response.Medias[0].Id) } func getMediaResults(r *http.Request) (MediaResults, error) { diff --git a/src/media/serve.go b/src/media/serve.go index 7acffb0..2e88d42 100644 --- a/src/media/serve.go +++ b/src/media/serve.go @@ -2,11 +2,9 @@ package media import ( "github.com/rs/zerolog/log" - "io" "net/http" "os" "path/filepath" - "strconv" ) /** @@ -25,25 +23,25 @@ func ServeMedia(w http.ResponseWriter, r *http.Request) { return } - streamFileToClientById(w, id) + streamFileToClientById(w, r, id) } -func streamFileToClientById(w http.ResponseWriter, id string) { +func streamFileToClientById(w http.ResponseWriter, r *http.Request, id string) { filename, err := getFileFromId(id) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) } - streamFileToClient(w, filename) + streamFileToClient(w, r, filename) } -func streamFileToClient(writer http.ResponseWriter, filename string) { +func streamFileToClient(w http.ResponseWriter, r *http.Request, filename string) { // Check if file exists and open Openfile, err := os.Open(filename) defer Openfile.Close() //Close after function return if err != nil { //File not found, send 404 - http.Error(writer, "File not found.", 404) + http.Error(w, "File not found.", 404) return } @@ -53,30 +51,23 @@ func streamFileToClient(writer http.ResponseWriter, filename string) { //Copy the headers into the FileHeader buffer if _, err = Openfile.Read(fileHeader); err != nil { log.Error().Msgf("File not found, couldn't open for reading at %s %v", filename, err) - http.Error(writer, "File not found", 404) + http.Error(w, "File not found", 404) return } // Get content type of file fileContentType := http.DetectContentType(fileHeader) - // Get the file size as a string - fileStat, _ := Openfile.Stat() - fileSize := strconv.FormatInt(fileStat.Size(), 10) - // Send the headers - writer.Header().Set("Content-Disposition", "filename="+filepath.Base(filename)) - writer.Header().Set("Content-Type", fileContentType) - writer.Header().Set("Content-Length", fileSize) + w.Header().Set("Content-Disposition", "filename="+filepath.Base(filename)) + w.Header().Set("Content-Type", fileContentType) // Send the file // We read n bytes from the file already, so we reset the offset back to 0 if _, err = Openfile.Seek(0, 0); err != nil { log.Error().Msgf("Error seeking into file %s %v", filename, err) - http.Error(writer, "File not found", 404) + http.Error(w, "File not found", 404) return } - - // Copy the file to the client - _, _ = io.Copy(writer, Openfile) + http.ServeFile(w, r, filename) }