def upload(self): for destination, data, content_type, compressed in self.get_files(): key = Key(self.bucket) key.content_type = content_type if compressed: key.set_metadata('content-encoding', 'gzip') for header, value in self.headers: key.set_metadata(header, value) key.key = destination key.set_contents_from_string(data)
def createVideo(): req = flask.request.get_json()['params'] session = database.DBSession() # Check if the user is allowed to access this method allowed = authorized.authorized(req['user_id'], req['access_token'], session) if allowed is not True: session.close() return allowed from user import Connection # Create the video and send back a response video_date = int(req['date']) user_id = req['user_id'] timeline_id = req['timeline_id'] if timeline_id == '': timeline_id = string_constants.kServerVideoPublicFeedKey description = req['description'] public_feed = False if 'public_feed' in req: public_feed = bool(req['public_feed']) video_content = None if 'video_content' in req: video_content = base64.b64decode(req['video_content']) video_thumbnail = None if 'video_thumbnail' in req: video_thumbnail = base64.b64decode(req['video_thumbnail']) if video_date is None or user_id is None or timeline_id is None or description is None: session.close() return authorized.wrongParams() # Add video_id to the playlist in the relationship timeline = None if timeline_id != string_constants.kServerVideoPublicFeedKey: timeline = session.query(Timeline).filter(Timeline.timeline_id == timeline_id).join(Timeline.connection).filter(Connection.approved == 1).filter(Connection.disabled == 0).first() if timeline is None: response = jsonify(message=string_constants.kServerVideoTimelineIDDoesntExist, status=False, HTTP_CODE=200 ) response.status_code = 200 session.close() return response video_filename = hashlib.sha256(str(video_date) + user_id + timeline_id).hexdigest() # Check if video already exists video_check = session.query(VideoModel).filter(VideoModel.video_id == video_filename).first() if video_check is not None: response = jsonify(message=string_constants.kServerVideoAlreadyExistsError, status=False, HTTP_CODE=200 ) response.status_code = 200 session.close() return response try: video_path = str(Video.getVideoObjectPath(video_filename, user_id, timeline_id, str(video_date))+".m4v") thumbnail_path = str(Video.getVideoThumbnailObjectPath(video_filename, user_id, timeline_id, str(video_date))+".jpg") if app.config["AWS_S3"]: if video_content is not None and video_thumbnail is not None: aws_s3_connection = S3Connection(app.config['AWS_ACCESS_KEY'], app.config['AWS_SECRET_KEY']) aws_s3_bucket = Bucket(aws_s3_connection, app.config['AWS_BUCKET_NAME']) aws_s3_video_key = Key(aws_s3_bucket) aws_s3_video_key.key = video_path aws_s3_video_key.content_type = app.config['AWS_KEY_CONTENT_TYPE'] aws_s3_video_key.set_contents_from_string(video_content, replace=True) aws_s3_thumb_key = Key(aws_s3_bucket) aws_s3_thumb_key.key = thumbnail_path aws_s3_thumb_key.content_type = app.config['AWS_KEY_CONTENT_TYPE'] aws_s3_thumb_key.set_contents_from_string(video_thumbnail, replace=True) # Create new video object and save it to the database new_video = VideoModel(video_date, user_id, timeline_id, video_filename + '_thumb.jpg', video_filename, description, public_feed) if timeline is not None: timeline.video_count += 1 from user import UserModel userDisplayName = session.query(UserModel.display_name).filter(UserModel.user_id == int(user_id)).first() userDisplayName = userDisplayName[0] # Add the notification for the new video from notification import NotificationModel, RegisteredNotificationUserModel notification = NotificationModel( user_id, (int(timeline.connection.user1) == int(user_id)) and timeline.connection.user2 or timeline.connection.user1, { string_constants.kServerNotificationsType: string_constants.kServerNotificationsTypeNewVideo, string_constants.kServerNotificationsTimeline_idKey: timeline_id, string_constants.kServerNotificationsUser_NameKey: userDisplayName }, calendar.timegm(datetime.utcnow().timetuple())) session.add(notification) session.add(new_video) session.commit() except exc.SQLAlchemyError as e: response = jsonify(message=string_constants.kServerVideoIssueMakingVideo, status=False, HTTP_CODE=200 ) response.status_code = 200 session.close() return response else: response = jsonify(message=string_constants.kServerVideoCreatedVideoSuccess, status=True, HTTP_CODE=200, Video={ "video_path": video_path, "thumbnail_path": thumbnail_path } ) response.status_code = 200 session.close() return response