コード例 #1
0
ファイル: main.go プロジェクト: shawshark/minotar
func fetchImageProcessThen(callback func(minecraft.Skin) (image.Image, error)) func(w http.ResponseWriter, r *http.Request) {
	return func(w http.ResponseWriter, r *http.Request) {
		timeReqStart := time.Now()

		vars := mux.Vars(r)

		username := vars["username"]
		size := rationalizeSize(vars["size"])
		ok := true

		var skin minecraft.Skin
		var err error

		// Fetch image using username
		skin, err = minecraft.GetSkin(minecraft.User{Name: username})
		if err != nil {
			// Problem with the returned image, probably means we have an incorrect username
			// Hit the accounts api
			user, err := minecraft.GetUser(username)

			if err != nil {
				// There's no account for this person, serve char
				skin, _ = minecraft.GetSkin(minecraft.User{Name: "char"})
			} else {
				// Get valid skin
				skin, err = minecraft.GetSkin(user)
				if err != nil {
					// Their skin somehow errored, fallback
					skin, _ = minecraft.GetSkin(minecraft.User{Name: "char"})
				}
			}
		}

		timeFetch := time.Now()

		img, err := callback(skin)
		if err != nil {
			serverErrorPage(w, r)
			return
		}
		timeProcess := time.Now()

		imgResized := Resize(size, size, img)
		timeResize := time.Now()

		w.Header().Add("Content-Type", "image/png")
		w.Header().Add("X-Requested", "processed")
		var timeout uint
		if ok {
			w.Header().Add("X-Result", "ok")
			timeout = TimeoutActualSkin
		} else {
			w.Header().Add("X-Result", "failed")
			timeout = TimeoutFailedFetch
		}
		w.Header().Add("X-Timing", fmt.Sprintf("%d+%d+%d=%dms", timeBetween(timeReqStart, timeFetch), timeBetween(timeFetch, timeProcess), timeBetween(timeProcess, timeResize), timeBetween(timeReqStart, timeResize)))
		addCacheTimeoutHeader(w, timeout)
		WritePNG(w, imgResized)
	}
}
コード例 #2
0
ファイル: main.go プロジェクト: shawshark/minotar
func skinPage(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)

	username := vars["username"]

	user, _ := minecraft.GetUser(username)
	skin, _ := minecraft.GetSkin(user)

	w.Header().Add("Content-Type", "image/png")
	w.Header().Add("X-Requested", "skin")
	w.Header().Add("X-Result", "ok")

	WritePNG(w, skin.Image)

	/*
		userSkinURL := minotar.URLForUser(username)
		resp, err := http.Get(userSkinURL)
		if err != nil {
			notFoundPage(w, r)
			return
		}
		w.Header().Add("Content-Type", "image/png")
		w.Header().Add("X-Requested", "skin")
		w.Header().Add("X-Result", "ok")
		addCacheTimeoutHeader(w, TIMEOUT_ACTUAL_SKIN)
		defer resp.Body.Close()
		io.Copy(w, resp.Body)
	*/
}
コード例 #3
0
ファイル: main.go プロジェクト: GetWorldTeam/minotar
func fetchSkin(username string) minecraft.Skin {
	skin, err := minecraft.GetSkin(minecraft.User{Name: username})
	if err != nil {
		// Problem with the returned image, probably means we have an incorrect username
		// Hit the accounts api
		user, err := minecraft.GetUser(username)

		if err != nil {
			// There's no account for this person, serve char
			skin, _ = minecraft.FetchSkinForChar()
		} else {
			// Get valid skin
			skin, err = minecraft.GetSkin(user)
			if err != nil {
				// Their skin somehow errored, fallback
				skin, _ = minecraft.FetchSkinForChar()
			}
		}
	}

	return skin
}