Beispiel #1
0
func editPhotoTitle(c *Context) *Result {

	photo, result := getPhotoToEdit(c)

	if result != nil {
		return result
	}

	s := &struct {
		Title string `json:"title"`
	}{}

	if err := c.ParseJSON(s); err != nil {
		return c.Error(err)
	}

	photo.Title = s.Title

	validator := validation.NewPhotoValidator(photo)

	if result, err := validator.Validate(); err != nil || !result.OK {
		if err != nil {
			return c.Error(err)
		}
		return c.BadRequest(result)
	}

	if err := photoMgr.Update(photo); err != nil {
		return c.Error(err)
	}

	return c.OK("Photo Updated")
}
Beispiel #2
0
func upload(c *Context) *Result {

	title := c.FormValue("title")
	taglist := c.FormValue("taglist")
	tags := strings.Split(taglist, " ")

	src, hdr, err := c.FormFile("photo")
	if err != nil {
		if err == http.ErrMissingFile || err == http.ErrNotMultipart {
			return c.BadRequest("No image was posted")
		}
		return c.Error(err)
	}
	contentType := hdr.Header["Content-Type"][0]

	if !isAllowedContentType(contentType) {
		return c.BadRequest("Not a valid image")
	}

	defer src.Close()

	processor := storage.NewImageProcessor()
	filename, err := processor.Process(src, contentType)

	if err != nil {
		return c.Error(err)
	}

	photo := &models.Photo{Title: title,
		OwnerID: c.User.ID, Filename: filename, Tags: tags}

	validator := validation.NewPhotoValidator(photo)

	if result, err := validator.Validate(); err != nil || !result.OK {
		if err != nil {
			return c.Error(err)
		}
		return c.BadRequest(result)
	}

	if err := photoMgr.Insert(photo); err != nil {
		return c.Error(err)
	}

	return c.OK(photo)
}