Example #1
0
func iconsHandler(w http.ResponseWriter, r *http.Request) {
	url := r.FormValue(urlParam)
	if len(url) == 0 {
		http.Redirect(w, r, "/", 302)
		return
	}

	finder := besticon.IconFinder{}

	formats := r.FormValue("formats")
	if formats != "" {
		finder.FormatsAllowed = strings.Split(r.FormValue("formats"), ",")
	}

	e, icons := finder.FetchIcons(url)
	switch {
	case e != nil:
		renderHTMLTemplate(w, 404, iconsHTML, pageInfo{URL: url, Error: e})
	case len(icons) == 0:
		errNoIcons := errors.New("this poor site has no icons at all :-(")
		renderHTMLTemplate(w, 404, iconsHTML, pageInfo{URL: url, Error: errNoIcons})
	default:
		renderHTMLTemplate(w, 200, iconsHTML, pageInfo{Icons: icons, URL: url})
	}
}
Example #2
0
func alliconsHandler(w http.ResponseWriter, r *http.Request) {
	url := r.FormValue(urlParam)
	if len(url) == 0 {
		errMissingURL := errors.New("need url query parameter")
		writeAPIError(w, 400, errMissingURL, true)
		return
	}

	finder := besticon.IconFinder{}
	formats := r.FormValue("formats")
	if formats != "" {
		finder.FormatsAllowed = strings.Split(r.FormValue("formats"), ",")
	}

	e, icons := finder.FetchIcons(url)
	if e != nil {
		writeAPIError(w, 404, e, true)
		return
	}

	pretty, err := strconv.ParseBool(r.FormValue(prettyParam))
	prettyPrint := (err == nil) && pretty

	writeAPIIcons(w, url, icons, prettyPrint)
}
Example #3
0
func iconHandler(w http.ResponseWriter, r *http.Request) {
	url := r.FormValue("url")
	if len(url) == 0 {
		writeAPIError(w, 400, errors.New("need url parameter"), true)
		return
	}

	size := r.FormValue("size")
	if size == "" {
		writeAPIError(w, 400, errors.New("need size parameter"), true)
		return
	}
	minSize, err := strconv.Atoi(size)
	if err != nil || minSize < 0 || minSize > 500 {
		writeAPIError(w, 400, errors.New("bad size parameter"), true)
		return
	}

	finder := besticon.IconFinder{}
	formats := r.FormValue("formats")
	if formats != "" {
		finder.FormatsAllowed = strings.Split(r.FormValue("formats"), ",")
	}

	err, _ = finder.FetchIcons(url)
	if err != nil {
		writeAPIError(w, 404, err, true)
		return
	}

	icon := finder.IconWithMinSize(minSize)
	if icon != nil {
		http.Redirect(w, r, icon.URL, 302)
		return
	}

	fallbackIconURL := r.FormValue("fallback_icon_url")
	if fallbackIconURL != "" {
		http.Redirect(w, r, fallbackIconURL, 302)
		return
	}

	iconColor := finder.MainColorForIcons()
	letter := lettericon.MainLetterFromURL(url)
	redirectPath := lettericon.IconPath(letter, size, iconColor)
	http.Redirect(w, r, redirectPath, 302)
}