2019-11-15 03:03:56 +00:00
|
|
|
package responses
|
|
|
|
|
|
|
|
import (
|
|
|
|
"mime"
|
|
|
|
"net/http"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"git.kirsle.net/apps/gophertype/pkg/bundled"
|
2020-02-17 23:50:04 +00:00
|
|
|
"git.kirsle.net/apps/gophertype/pkg/console"
|
|
|
|
"git.kirsle.net/apps/gophertype/pkg/settings"
|
2019-11-15 03:03:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2020-02-17 23:50:04 +00:00
|
|
|
console.Debug("SendFile: http.ServeFile(%s)", filepath.Join(settings.UserRoot, path))
|
|
|
|
http.ServeFile(w, r, filepath.Join(settings.UserRoot, path))
|
2019-11-15 03:03:56 +00:00
|
|
|
}
|