Пример #1
0
func respRemoteImgHandler(w http.ResponseWriter, r *http.Request) {

	var body []byte
	vars := mux.Vars(r)
	imagePath := vars["path"]
	domain := vars["domain"]

	// Set up options
	opts := optimizer.Options{
		Mime: mime.TypeByExtension(path.Ext(imagePath)),
	}

	opts.SetFromRequest(r)
	opts.Optimize()

	// Try to fetch cached iamge.
	if err := serveCachedRespImg(w, r, domain, imagePath, opts); err == nil {
		return
	}

	body, statusCode, err := getImageFromUpstream(domain, imagePath)
	if err != nil {
		imgHeaders(&w, r)
		http.Error(w, http.StatusText(statusCode), statusCode)
		return
	}

	optimizeAndServeImg(w, r, opts, domain, imagePath, body)
	return

}
Пример #2
0
func respLocalImgHandler(w http.ResponseWriter, r *http.Request) {

	domain := r.Host
	imagePath := r.RequestURI
	opts := optimizer.Options{
		Mime: mime.TypeByExtension(path.Ext(imagePath)),
	}

	opts.SetFromRequest(r)
	opts.Optimize()

	// Try to fetch cached iamge.
	if err := serveCachedRespImg(w, r, domain, imagePath, opts); err == nil {
		imgHeaders(&w, r)
		return
	}

	body, err := getImageFromCache(fmt.Sprintf("%s/%s", documentRoot, imagePath))
	if err != nil {
		http.Error(w, http.StatusText(404), 404)
		return
	}

	optimizeAndServeImg(w, r, opts, domain, imagePath, body)

}