def _create_download_response(request, datafile_id, disposition='attachment'): # Get datafile (and return 404 if absent) try: datafile = Dataset_File.objects.get(pk=datafile_id) except Dataset_File.DoesNotExist: return return_response_not_found(request) # Check users has access to datafile if not has_datafile_download_access(request=request, dataset_file_id=datafile.id): return return_response_error(request) # Send an image that can be seen in the browser if disposition == 'inline' and datafile.is_image(): from tardis.tardis_portal.iiif import download_image args = (request, datafile.id, 'full', 'full', '0', 'native') # Send unconverted image if web-compatible if datafile.get_mimetype() in ('image/gif', 'image/jpeg', 'image/png'): return download_image(*args) # Send converted image return download_image(*args, format='png') # Send local file try: # Get file object for datafile file_obj = datafile.get_file() if not file_obj: # If file path doesn't resolve, return not found return return_response_not_found(request) wrapper = FileWrapper(file_obj) response = HttpResponse(wrapper, mimetype=datafile.get_mimetype()) response['Content-Disposition'] = \ '%s; filename="%s"' % (disposition, datafile.filename) return response except IOError: # If we can't read the file, return not found return return_response_not_found(request)
def _create_download_response(request, datafile_id, disposition='attachment'): # too complex # noqa # Get datafile (and return 404 if absent) try: datafile = DataFile.objects.get(pk=datafile_id) except DataFile.DoesNotExist: return return_response_not_found(request) # Check users has access to datafile if not has_datafile_download_access(request=request, datafile_id=datafile.id): return return_response_error(request) # Send an image that can be seen in the browser if disposition == 'inline' and datafile.is_image(): from tardis.tardis_portal.iiif import download_image args = (request, datafile.id, 'full', 'full', '0', 'native') # Send unconverted image if web-compatible if datafile.get_mimetype() in ('image/gif', 'image/jpeg', 'image/png'): return download_image(*args) # Send converted image return download_image(*args, format='png') # Send local file try: verified_only = True # Query parameter to allow download of unverified files ignore_verif = request.GET.get('ignore_verification_status', '0') # Ensure ignore_verification_status=0 etc works as expected # a bare ?ignore_verification_status is True if ignore_verif.lower() in [u'', u'1', u'true']: verified_only = False # Get file object for datafile file_obj = datafile.get_file(verified_only=verified_only) if not file_obj: # If file path doesn't resolve, return not found if verified_only: return render_error_message(request, "File is unverified, " "please try again later.", status=503) else: return return_response_not_found(request) wrapper = FileWrapper(file_obj, blksize=65535) response = StreamingHttpResponse(wrapper, content_type=datafile.get_mimetype()) response['Content-Disposition'] = \ '%s; filename="%s"' % (disposition, datafile.filename) return response except IOError: # If we can't read the file, return not found return return_response_not_found(request) except ValueError: # raised when replica not verified TODO: custom excptn redirect = request.META.get('HTTP_REFERER', 'http://%s/' % request.META.get('HTTP_HOST')) message = """The file you are trying to access has not yet been verified. Verification is an automated background process. Please try again later or contact the system administrator if the issue persists.""" message = ' '.join(message.split()) # removes spaces redirect = redirect + '#error:' + message return HttpResponseRedirect(redirect)
def _create_download_response(request, datafile_id, disposition='attachment'): #import ipdb; ipdb.set_trace() # Get datafile (and return 404 if absent) try: datafile = Dataset_File.objects.get(pk=datafile_id) except Dataset_File.DoesNotExist: return return_response_not_found(request) # Check users has access to datafile if not has_datafile_download_access(request=request, dataset_file_id=datafile.id): return return_response_error(request) # Send an image that can be seen in the browser if disposition == 'inline' and datafile.is_image(): from tardis.tardis_portal.iiif import download_image args = (request, datafile.id, 'full', 'full', '0', 'native') # Send unconverted image if web-compatible if datafile.get_mimetype() in ('image/gif', 'image/jpeg', 'image/png'): return download_image(*args) # Send converted image return download_image(*args, format='png') # Send local file try: # Get file object for datafile file_obj = datafile.get_file() if not file_obj: # If file path doesn't resolve, return not found return return_response_not_found(request) wrapper = FileWrapper(file_obj) response = HttpResponse(wrapper, mimetype=datafile.get_mimetype()) response['Content-Disposition'] = \ '%s; filename="%s"' % (disposition, datafile.filename) return response except IOError: # If we can't read the file, return not found return return_response_not_found(request) except ValueError: # raised when replica not verified TODO: custom excptn redirect = request.META.get('HTTP_REFERER', 'http://%s/' % request.META.get('HTTP_HOST')) message = """The file you are trying to access has not yet been verified. Verification is an automated background process. Please try again later or contact the system administrator if the issue persists.""" message = ' '.join(message.split()) # removes spaces redirect = redirect + '#error:' + message return HttpResponseRedirect(redirect)
def _create_download_response(request, datafile_id, disposition='attachment'): #import ipdb; ipdb.set_trace() # Get datafile (and return 404 if absent) try: datafile = Dataset_File.objects.get(pk=datafile_id) except Dataset_File.DoesNotExist: return return_response_not_found(request) # Check users has access to datafile if not has_datafile_download_access(request=request, dataset_file_id=datafile.id): return return_response_error(request) # Send an image that can be seen in the browser if disposition == 'inline' and datafile.is_image(): from tardis.tardis_portal.iiif import download_image args = (request, datafile.id, 'full', 'full', '0', 'native') # Send unconverted image if web-compatible if datafile.get_mimetype() in ('image/gif', 'image/jpeg', 'image/png'): return download_image(*args) # Send converted image return download_image(*args, format='png') # Send local file try: # Get file object for datafile file_obj = datafile.get_file() if not file_obj: # If file path doesn't resolve, return not found return return_response_not_found(request) wrapper = FileWrapper(file_obj, blksize=65535) response = StreamingHttpResponse(wrapper, mimetype=datafile.get_mimetype()) response['Content-Disposition'] = \ '%s; filename="%s"' % (disposition, datafile.filename) return response except IOError: # If we can't read the file, return not found return return_response_not_found(request) except ValueError: # raised when replica not verified TODO: custom excptn redirect = request.META.get('HTTP_REFERER', 'http://%s/' % request.META.get('HTTP_HOST')) message = """The file you are trying to access has not yet been verified. Verification is an automated background process. Please try again later or contact the system administrator if the issue persists.""" message = ' '.join(message.split()) # removes spaces redirect = redirect + '#error:' + message return HttpResponseRedirect(redirect)