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)