Пример #1
0
func ImageHandler(ctx wombat.Context, a *articles.Article, path, filename string) {
	ctx.Response.Header().Set("Content-Type", "application/json")

	// Save the image
	img, err := web.SaveImage(ctx.Request, ctx.Response, path, filename)
	if err != nil {
		ctx.HttpError(http.StatusInternalServerError, GetError(ctx.Request, err))
		return
	}

	s := img.Bounds().Size()
	exists := false
	// Add or Update the image
	for _, v := range a.Imgs {
		if v.Src == filename {
			v.W, v.H = s.X, s.Y
			exists = true
		}
	}
	if !exists {
		a.Imgs = append(a.Imgs, articles.Img{filename, "", s.X, s.Y})
	}

	// Update the article's images
	if err = a.SetImgs(a.Imgs); err != nil {
		log.Println("Failed to persit new image: ", filename, " for article: ", a.TitlePath)
		ctx.HttpError(http.StatusInternalServerError, GetError(ctx.Request, err))
	} else {
		j := fmt.Sprintf(`{"image":"%s", "w":%d,"h":%d}`, filename, s.X, s.Y)
		ctx.Response.Write([]byte(j))
	}
}