Exemplo n.º 1
0
func UserImageHandler(ctx *app.Context) {
	if ImageHandler != nil {
		ImageHandler(ctx)
		return
	}
	id := ctx.IndexValue(0)
	format := ctx.IndexValue(1)
	if lower := strings.ToLower(format); lower != format {
		ctx.MustRedirectReverse(true, ImageHandlerName, id, lower)
		return
	}
	ctx.SetHeader("Content-Type", "image/"+format)
	httpserve.NeverExpires(ctx)
	bs := ctx.Blobstore()
	if err := bs.Serve(ctx, id, nil); err != nil {
		panic(err)
	}
}
Exemplo n.º 2
0
// Handler returns an http.handlerFunc which serves the assets from this
// Manager. To avoid circular imports, this function returns an http.HandlerFunc
// rather than a gnd.la/app.Handler. To obtain a gnd.la/app.Handler use
// gnd.la/app.HandlerFromHTTPFunc.
func (m *Manager) Handler() http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		p := m.Path(r.URL)
		f, err := m.Load(p)
		if err != nil {
			log.Warningf("error serving %s: %s", r.URL, err)
			return
		}
		seeker, err := Seeker(f)
		if err != nil {
			log.Warningf("error serving %s: %s", r.URL, err)
			return
		}
		var modtime time.Time
		if st, err := m.VFS().Stat(p); err == nil {
			modtime = st.ModTime()
		}
		if r.URL.RawQuery != "" {
			httpserve.NeverExpires(w)
		}
		http.ServeContent(w, r, r.URL.Path, modtime, seeker)
		f.Close()
	}
}