Example #1
0
func (place *Place) handleImage(request *restful.Request, response *restful.Response) {
	errors := api.Error{}

	request.Request.ParseMultipartForm(32 << 20)
	mpf, hdr, _ := request.Request.FormFile("image")

	ext := filepath.Ext(hdr.Filename)
	if helpers.CheckFileExtension(ext) == false {
		errors.ListErrors = append(errors.ListErrors, "Only .jpg, .jpeg or .png are authorized.")
		response.WriteHeaderAndEntity(406, errors)
		return
	}
	if strings.Compare(place.Image, "") != 0 {
		helpers.RemoveFile("./assets/Images/" + place.Image)
	}
	place.Image = helpers.GenerateHash(50) + ext
	go helpers.PutFile("assets/Images/"+place.Image, mpf)

	_, err := r.Table("places").Get(place.Id).Update(place).RunWrite(api.Sess)
	if err != nil {
		response.WriteHeaderAndEntity(http.StatusConflict, err.Error())
		return
	}
	response.WriteHeaderAndEntity(http.StatusOK, place)

	helpers.PrintLog(request, response, place.user.Name)
}
Example #2
0
func (place *Place) createPlace(request *restful.Request, response *restful.Response) {
	place.Lat = 0
	place.Lng = 0
	place.PostNb = 0
	place.ValidatedAt = *new(time.Time)
	place.Validated = false
	place.CreateAt = time.Now()
	place.UserId = place.user.Id
	place.Id = *new(string)

	request.Request.ParseMultipartForm(32 << 20)
	mpf, hdr, _ := request.Request.FormFile("image")
	ext := filepath.Ext(hdr.Filename)

	if helpers.CheckFileExtension(ext) == false {
		errors := api.Error{}
		errors.ListErrors = append(errors.ListErrors, "Only .jpg, .jpeg or .png are authorized.")
		response.WriteHeaderAndEntity(406, errors)
		helpers.PrintLog(request, response, place.user.Name)
		return
	}
	place.Image = helpers.GenerateHash(50) + ext
	go helpers.PutFile("assets/Images/"+place.Image, mpf)

	resp, err := r.Table("places").Insert(place).RunWrite(api.Sess)
	if err != nil {
		response.WriteHeaderAndEntity(http.StatusConflict, err.Error())
		return
	}
	place.Id = resp.GeneratedKeys[0]
	response.WriteHeaderAndEntity(http.StatusCreated, place)

	helpers.PrintLog(request, response, place.user.Name)
}
Example #3
0
func (user *User) updateProfilePicture(request *restful.Request, response *restful.Response) {
	errors := api.Error{}

	if strings.Compare(user.Email, "") == 0 {
		errors.ListErrors = append(errors.ListErrors, "You cannot update your profile picture if your account has been created with facebook")
		response.WriteHeaderAndEntity(406, errors)
		helpers.PrintLog(request, response, user.Name)
		return
	}

	request.Request.ParseMultipartForm(32 << 20)
	mpf, hdr, _ := request.Request.FormFile("image")

	ext := filepath.Ext(hdr.Filename)

	if helpers.CheckFileExtension(ext) == false {
		errors.ListErrors = append(errors.ListErrors, "Only .jpg, .jpeg or .png are authorized.")
		response.WriteHeaderAndEntity(406, errors)
		helpers.PrintLog(request, response, user.Name)
		return
	}
	if strings.Compare(user.UrlPicture, "") != 0 {
		helpers.RemoveFile("./assets/Images/" + user.UrlPicture)
	}

	user.UrlPicture = helpers.GenerateHash(50) + ext
	go helpers.PutFile("assets/Images/"+user.UrlPicture, mpf)

	_, err := r.Table("users").Get(user.Id).Update(user).RunWrite(api.Sess)
	if err != nil {
		response.WriteHeaderAndEntity(http.StatusConflict, err.Error())
		helpers.PrintLog(request, response, user.Name)
		return
	}

	response.WriteHeaderAndEntity(http.StatusOK, user)
	helpers.PrintLog(request, response, user.Name)
}
Example #4
0
func (post *Post) createPostWithImage(request *restful.Request, response *restful.Response) {
	newPost := new(Post)

	newPost.Content = post.Content
	newPost.Created = time.Now()
	newPost.PlaceId = post.place.Id
	newPost.UserId = post.user.Id
	newPost.PlaceName = post.place.Name
	newPost.UserName = post.user.Name
	newPost.Validated = false

	request.Request.ParseMultipartForm(32 << 20)
	mpf, hdr, _ := request.Request.FormFile("image")
	defer mpf.Close()

	ext := filepath.Ext(hdr.Filename)

	if helpers.CheckFileExtension(ext) == false {
		errors := api.Error{}
		errors.ListErrors = append(errors.ListErrors, "Only .jpg, .jpeg or .png are authorized.")
		response.WriteHeaderAndEntity(406, errors)
		helpers.PrintLog(request, response, post.user.Name)
		return
	}

	newPost.Image = helpers.GenerateHash(50) + ext
	go helpers.PutFile("assets/Images/"+newPost.Image, mpf)

	resp, err := r.Table("posts").Insert(newPost).RunWrite(api.Sess)
	if err != nil {
		response.WriteHeaderAndEntity(http.StatusConflict, err.Error())
		helpers.PrintLog(request, response, post.user.Name)
		return
	}
	newPost.Id = resp.GeneratedKeys[0]

	response.WriteHeaderAndEntity(http.StatusCreated, newPost)
}
Example #5
0
func (message *Message) createMessageWithImage(request *restful.Request, response *restful.Response) {
	msg := new(Message)
	msg.CreatedAt = time.Now()
	msg.FromUserId = message.user.Id
	msg.TalkId = message.talk.Id
	msg.Read = false

	if message.talk.UserIdX == message.user.Id {
		msg.ToUserId = message.talk.UserIdY
	} else {
		msg.ToUserId = message.talk.UserIdX
	}

	request.Request.ParseMultipartForm(32 << 20)
	mpf, hdr, _ := request.Request.FormFile("image")
	ext := filepath.Ext(hdr.Filename)
	if helpers.CheckFileExtension(ext) == false {
		errors := api.Error{}
		errors.ListErrors = append(errors.ListErrors, "Only .jpg, .jpeg or .png are authorized.")
		response.WriteHeaderAndEntity(http.StatusNotAcceptable, errors)
		helpers.PrintLog(request, response, message.user.Name)
		return
	}
	msg.Image = helpers.GenerateHash(50) + ext
	go helpers.PutFile("assets/Images/"+msg.Image, mpf)

	resp, err := r.Table("messages").Insert(msg).RunWrite(api.Sess)
	if err != nil {
		helpers.PrintLog(request, response, message.user.Name)
		response.WriteHeaderAndEntity(http.StatusConflict, err.Error())
		return
	}
	msg.Id = resp.GeneratedKeys[0]
	message.talk.UpdateLastMessageDate(message.user.Id, "")
	response.WriteHeaderAndEntity(http.StatusCreated, msg)
	helpers.PrintLog(request, response, message.user.Name)
}