コード例 #1
0
ファイル: handlers.go プロジェクト: juztin/wombat-articles
func ThumbHandler(ctx wombat.Context, a *articles.Article, path, filename string) {
	ctx.Response.Header().Set("Content-Type", "application/json")

	// Save & resize the image to a thumbnail
	img, err := web.SaveImage(ctx.Request, ctx.Response, path, filename)
	if err == nil {
		// Resize the image and save it
		if img, err = imagery.ResizeWidth(img, 200); err == nil {
			err = imagery.WriteTo(web.ImageType(ctx.Request), img, path, filename)
		}
	}
	if err != nil {
		ctx.HttpError(http.StatusInternalServerError, GetError(ctx.Request, err))
		return
	}

	oldThumb := filepath.Join(path, a.TitlePath, a.Img.Src)
	s := img.Bounds().Size()

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