def post(self, upload_token_id): if not KalturaAPI.enabled(): abort(400, title="File Not Uploaded", message="Please use a valid upload method to attach files. You are not able to upload with this method based on the current settings.") kaltura_media = KalturaAPI.complete_upload_for_token(upload_token_id) on_save_kaltura_file.send( self, event_name=on_save_kaltura_file.name, user=current_user, data={'upload_token_id': upload_token_id}) try: db_file = File( user_id=current_user.id, name='', alias=kaltura_media.file_name, kaltura_media=kaltura_media ) db.session.add(db_file) db.session.commit() # use uuid generated by file model for name name = db_file.uuid + '.' + kaltura_media.extension # update file record with name db_file.name = name db.session.commit() except Exception as e: db.session.rollback() raise e return {'file': marshal(db_file, dataformat.get_file())}
def file_retrieve(file_type, file_name): file_dirs = { 'attachment': app.config['ATTACHMENT_UPLOAD_FOLDER'], 'report': app.config['REPORT_FOLDER'] } file_path = '{}/{}'.format(file_dirs[file_type], file_name) params = attachment_download_parser.parse_args() if file_type == 'attachment': attachment = File.get_by_file_name_or_404( file_name, joinedloads=['answers', 'assignments'] ) for answer in attachment.answers: require(READ, answer, title="Attachment Unavailable", message="Sorry, your role does not allow you to view the attachment.") for assignment in attachment.assignments: require(READ, assignment, title="Attachment Unavailable", message="Sorry, your role does not allow you to view the attachment.") # If attachment is in Kaltura, redirect the user if attachment.kaltura_media and KalturaAPI.enabled(): entry_id = attachment.kaltura_media.entry_id download_url = attachment.kaltura_media.download_url if entry_id: # Short-lived session of 60 seconds for user to start the media download kaltura_url = KalturaAPI.get_direct_access_url(entry_id, download_url, 60) return redirect(kaltura_url) if not os.path.exists(file_path): return make_response('invalid file name', 404) # TODO: add bouncer for reports mimetype, encoding = mimetypes.guess_type(file_name) attachment_filename = None as_attachment = False if file_type == 'attachment' and mimetype != "application/pdf": attachment_filename = params.get('name') #optionally set the download file name as_attachment = True on_get_file.send( current_app._get_current_object(), event_name=on_get_file.name, user=current_user, file_type=file_type, file_name=file_name, data={'file_path': file_path, 'mimetype': mimetype}) return send_file(file_path, mimetype=mimetype, attachment_filename=attachment_filename, as_attachment=as_attachment)
def get(self): if not KalturaAPI.enabled(): abort(400, title="File Not Uploaded", message="Please use a valid upload method to attach files. You are not able to upload with this method based on the current settings.") upload_url = KalturaAPI.generate_new_upload_token() on_get_kaltura_token.send( self, event_name=on_get_kaltura_token.name, user=current_user, data={'upload_url': upload_url}) return {'upload_url': upload_url}