use http.ServeFile
This commit is contained in:
parent
86d3df51b7
commit
c422436a29
2 changed files with 11 additions and 20 deletions
|
|
@ -76,7 +76,7 @@ func FetchMediaApi(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// just take the first one
|
// 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) {
|
func getMediaResults(r *http.Request) (MediaResults, error) {
|
||||||
|
|
|
||||||
|
|
@ -2,11 +2,9 @@ package media
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
"io"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -25,25 +23,25 @@ func ServeMedia(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
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)
|
filename, err := getFileFromId(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
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
|
// Check if file exists and open
|
||||||
Openfile, err := os.Open(filename)
|
Openfile, err := os.Open(filename)
|
||||||
defer Openfile.Close() //Close after function return
|
defer Openfile.Close() //Close after function return
|
||||||
if err != nil {
|
if err != nil {
|
||||||
//File not found, send 404
|
//File not found, send 404
|
||||||
http.Error(writer, "File not found.", 404)
|
http.Error(w, "File not found.", 404)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -53,30 +51,23 @@ func streamFileToClient(writer http.ResponseWriter, filename string) {
|
||||||
//Copy the headers into the FileHeader buffer
|
//Copy the headers into the FileHeader buffer
|
||||||
if _, err = Openfile.Read(fileHeader); err != nil {
|
if _, err = Openfile.Read(fileHeader); err != nil {
|
||||||
log.Error().Msgf("File not found, couldn't open for reading at %s %v", filename, err)
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get content type of file
|
// Get content type of file
|
||||||
fileContentType := http.DetectContentType(fileHeader)
|
fileContentType := http.DetectContentType(fileHeader)
|
||||||
|
|
||||||
// Get the file size as a string
|
|
||||||
fileStat, _ := Openfile.Stat()
|
|
||||||
fileSize := strconv.FormatInt(fileStat.Size(), 10)
|
|
||||||
|
|
||||||
// Send the headers
|
// Send the headers
|
||||||
writer.Header().Set("Content-Disposition", "filename="+filepath.Base(filename))
|
w.Header().Set("Content-Disposition", "filename="+filepath.Base(filename))
|
||||||
writer.Header().Set("Content-Type", fileContentType)
|
w.Header().Set("Content-Type", fileContentType)
|
||||||
writer.Header().Set("Content-Length", fileSize)
|
|
||||||
|
|
||||||
// Send the file
|
// Send the file
|
||||||
// We read n bytes from the file already, so we reset the offset back to 0
|
// We read n bytes from the file already, so we reset the offset back to 0
|
||||||
if _, err = Openfile.Seek(0, 0); err != nil {
|
if _, err = Openfile.Seek(0, 0); err != nil {
|
||||||
log.Error().Msgf("Error seeking into file %s %v", filename, err)
|
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
|
return
|
||||||
}
|
}
|
||||||
|
http.ServeFile(w, r, filename)
|
||||||
// Copy the file to the client
|
|
||||||
_, _ = io.Copy(writer, Openfile)
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue