Exemple #1
0
// ---------------------------------------------------------------------------
// privates
//
func respondWithTemplate(ctx appengine.Context, w http.ResponseWriter, id string) {
	filename := templateFile(id)
	_, err := file.Stat(ctx, gcsFilesApiPath(bucket, filename))
	if err != nil {
		w.WriteHeader(http.StatusNotFound)
		return
	}

	location := gcsDirectPublicUrl(ctx, bucket, filename)
	w.Header().Set("Location", location)
	w.WriteHeader(http.StatusMovedPermanently)
}
Exemple #2
0
// ---------------------------------------------------------------------------
// GET /meme/[id]/[top-text]/[bottom-text]
//
func MemeHandler(w http.ResponseWriter, r *http.Request) {
	ctx := appengine.NewContext(r)

	var id string
	var topText string
	var botText string

	u, _ := url.Parse(r.RequestURI)

	path := u.Path[1:]
	if strings.LastIndex(path, ".jpg") == len(path)-4 {
		path = path[:len(path)-4]
	}

	parts := strings.Split(path, "/")
	if len(parts) > 1 {
		id = parts[1]
		if len(parts) > 2 {
			topText = parts[2]
			if len(parts) > 3 {
				botText = parts[3]
			}
		}
	}

	topText = strings.Replace(topText, "_", " ", -1)
	botText = strings.Replace(botText, "_", " ", -1)

	filename := cacheFilename(id, topText, botText)
	_, err := file.Stat(ctx, gcsFilesApiPath(bucket, filename))
	if err != nil {
		fileWriter, _, err := file.Create(ctx, filename, &file.CreateOptions{
			MIMEType:   "image/jpeg",
			BucketName: bucket,
		})
		defer fileWriter.Close()

		err = renderMeme(ctx, fileWriter, id, topText, botText)
		if err != nil {
			w.WriteHeader(http.StatusNotFound)
			w.Write([]byte(err.Error()))
			return
		}
	}

	location := gcsDirectPublicUrl(ctx, bucket, filename)
	w.Header().Set("Content-Type", "image/jpeg")
	w.Header().Set("Location", location)
	w.WriteHeader(http.StatusTemporaryRedirect)
}