gophertype/pkg/responses/static_file.go
Noah 383b5d7591 Age Gate, Legacy kirsle/blog Migration Program
* Add the Age Gate middleware for NSFW sites.
* Cache thumbnail images from blog entries.
* Implement the user-root properly for loading web assets.
2020-02-17 15:50:04 -08:00

30 rindas
706 B
Go

package responses
import (
"mime"
"net/http"
"path/filepath"
"git.kirsle.net/apps/gophertype/pkg/bundled"
"git.kirsle.net/apps/gophertype/pkg/console"
"git.kirsle.net/apps/gophertype/pkg/settings"
)
// SendFile sends a file from bindata or the user root.
func SendFile(w http.ResponseWriter, r *http.Request, path string) {
mimeType := mime.TypeByExtension(filepath.Ext(path))
if mimeType == "" {
mimeType = "text/plain"
}
w.Header().Set("Content-Type", mimeType)
if b, err := bundled.Asset(path); err == nil {
w.Write(b)
return
}
console.Debug("SendFile: http.ServeFile(%s)", filepath.Join(settings.UserRoot, path))
http.ServeFile(w, r, filepath.Join(settings.UserRoot, path))
}