Beispiel #1
0
def delete_profile_photo():
	current_user = g.user

	if current_user.portrait != None:
		old_hash = filesystem_storage_service.get_cropped_hash(current_user.portrait, current_user.user_id)
		try:
			filesystem_storage_service.delete_cropped(old_hash)
		except OSError:
			pass

	current_user.portrait = None
	updated_user = user_service.update_user(current_user)

	updated_user_json_dict = updated_user.to_json_dict(private=True)

	return { "success" : True, "user" : updated_user_json_dict }
Beispiel #2
0
def upload_profile_photo():
	current_user = g.user
	current_location = g.user_location

	picture_id_str = request.form.get("profile-picture-id", None)
	crop_x_str = request.form["crop-x"]
	crop_y_str = request.form["crop-y"]
	crop_width_str = request.form["width"]
	crop_height_str = request.form["height"]
	privacy = request.form["privacy"]
	uploaded_photo = request.files.get("image", None)

	if picture_id_str == None and uploaded_photo == None:
		return { "success" : False }
	try:
		if picture_id_str != None:
			picture_id = int(picture_id_str)
		else:
			picture_id = None

		crop_x = int(crop_x_str)
		crop_y = int(crop_y_str)
		crop_width = int(crop_width_str)
		crop_height = int(crop_height_str)
	except ValueError as e:
		return { "success" : False }

	if picture_id == None:
		secured_name = secure_filename(uploaded_photo.filename)

		full_picture = UploadedPicture(current_user.user_id, datetime.now(), secured_name, current_location, None, None, privacy)
		picture_meta_service.create_picture(full_picture)
		picture_id = full_picture.picture_id

		# Temporary local filesystem storage
		hashed_filename = filesystem_storage_service.get_image_hash(full_picture.picture_id, full_picture.author_id)
		try:
			filesystem_storage_service.save_image(uploaded_photo, hashed_filename)
		except ServiceException as se:
			picture_meta_service.delete_picture_by_id(full_picture.picture_id)
			return { "error" : True, "message" : "Error saving new image" }, 500
	else:
		full_picture = picture_meta_service.get_picture_by_id(picture_id, current_user.user_id)
		hashed_filename = filesystem_storage_service.get_image_hash(full_picture.picture_id, full_picture.author_id)
		uploaded_photo = filesystem_storage_service.get_image(hashed_filename)


	profile_picture = ProfilePicture(picture_id, datetime.now())
	picture_meta_service.create_profile_picture(profile_picture)

	# Attempt to delete previous portrait
	if current_user.portrait != None:
		old_hash = filesystem_storage_service.get_cropped_hash(current_user.portrait, current_user.user_id)
		try:
			filesystem_storage_service.delete_cropped(old_hash)
		except OSError:
			pass


	# Temporary local filesystem storae
	hashed_cropped_filename = filesystem_storage_service.get_cropped_hash(profile_picture.profile_picture_id, current_user.user_id)

	try:
		crop = PictureSection(crop_x, crop_y, crop_width, crop_height)
		filesystem_storage_service.save_cropped_image(uploaded_photo, hashed_cropped_filename, crop)
	except ServiceException as se:
		print se
		return { "error" : True, "message" : "Error saving cropped image" }, 500

	current_user.portrait = profile_picture.profile_picture_id
	updated_user = user_service.update_user(current_user)

	updated_user_json_dict = updated_user.to_json_dict(private=True)

	return { "error" : False, "user" : updated_user_json_dict }