예제 #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))
	}
}
예제 #2
0
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))
	}
}
예제 #3
0
func JSONHandler(ctx wombat.Context, a *articles.Article, imagePath string, data []byte) {
	// Get the JSONMessage from the request
	var msg JSONMessage
	err := json.Unmarshal(data, &msg)
	if err != nil {
		ctx.HttpError(http.StatusBadRequest, GetError(ctx.Request, err))
		return
	}

	// Perform the given action
	switch msg.Action {
	default:
		// Invalid/missing action
		err = errors.New("Invalid Action")
	case "setSynopsis":
		err = a.SetSynopsis(msg.Data)
	case "setContent":
		err = a.SetContent(msg.Data)
	case "setActive":
		// Toggle
		err = a.Publish(!a.IsPublished)
	case "deleteImage":
		err = RemoveImage(a, msg.Data, imagePath)
	}

	// Report if the action resulted in an error
	if err != nil {
		ctx.HttpError(http.StatusInternalServerError, GetError(ctx.Request, err))
	}
}
예제 #4
0
func RemoveImage(a *articles.Article, src, imagePath string) (err error) {
	// New slice, minus the `src` image
	imgs := make([]articles.Img, 0, len(a.Imgs))
	//imgs := []articles.Img{}
	found := false
	for _, i := range a.Imgs {
		if i.Src == src {
			found = true
		} else {
			imgs = append(imgs, i)
		}
	}

	// If the `src` image was found, remove it from the Article
	if found {
		// Remove the `src` image from the filesystem
		if err = a.SetImgs(imgs); err == nil {
			p := filepath.Join(imagePath, a.TitlePath)
			os.Remove(filepath.Join(p, src))
		}
	}
	return
}