GoBlog/media.go

23 lines
427 B
Go
Raw Normal View History

2021-01-10 14:59:43 +00:00
package main
import (
"net/http"
"os"
"path/filepath"
2021-03-03 17:19:55 +00:00
"github.com/go-chi/chi/v5"
2021-01-10 14:59:43 +00:00
)
const mediaFilePath = "data/media"
func (a *goBlog) serveMediaFile(w http.ResponseWriter, r *http.Request) {
2021-01-10 14:59:43 +00:00
f := filepath.Join(mediaFilePath, chi.URLParam(r, "file"))
_, err := os.Stat(f)
if err != nil {
a.serve404(w, r)
2021-01-10 14:59:43 +00:00
return
}
w.Header().Add("Cache-Control", "public,max-age=31536000,immutable")
http.ServeFile(w, r, f)
}