Пример #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)
}
Пример #2
0
func (post *Post) refusePost(request *restful.Request, response *restful.Response) {

	_, err := r.Table("posts").Get(post.Id).Delete().Run(api.Sess)
	if err != nil {
		response.WriteHeaderAndEntity(http.StatusInternalServerError, err.Error())
		helpers.PrintLog(request, response, post.user.Name)
		return
	}
	if post.Image != "" {
		helpers.RemoveFile("./assets/Images/" + post.Image)
	}
	if post.user.IsUserIsAdmin() == true {
		go func() {
			notif := notification.SetPostRefused()
			notif.UserId = post.UserId
			notif.UserIdFrom = post.user.Id
			notif.IdThing = post.Id
			notif.Name = post.PlaceName
			notif.IdLink = post.Id
			notif.CreateNotification()
		}()
	}
	response.WriteHeader(http.StatusOK)
	helpers.PrintLog(request, response, post.user.Name)
}
Пример #3
0
func (post *Post) deletePostImage(request *restful.Request, response *restful.Response) {

	if post.Image == "" {
		return
	}

	nameImage := post.Image
	post.Image = ""

	_, err := r.Table("posts").Get(post.Id).Update(post).RunWrite(api.Sess)
	if err != nil {
		response.WriteHeaderAndEntity(http.StatusInternalServerError, err.Error())
		helpers.PrintLog(request, response, post.user.Name)
		return
	}
	helpers.RemoveFile("./assets/Images/" + nameImage)
	response.WriteHeader(http.StatusOK)
	helpers.PrintLog(request, response, post.user.Name)
}
Пример #4
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)
}