def download(request, uuid): """ Download a package from a requested backlog transfer. :param request: The Django request object :param uuid: UUID for the transfer we're downloading the package from :return: Respond with a TAR'd version of the requested package """ return helpers.stream_file_from_storage_service( storage_service.download_file_url(uuid) )
def test_stream_pointer_from_storage_unsuccessful(mocker): pointer_url = "http://archivematica-storage-service:8000/api/v2/file/44444444-4444-4444-4444-444444444444/pointer_file" custom_error_message = "Unable to retrieve AIP pointer file from Storage Service" mock_response = requests.Response() mock_response.status_code = RESPONSE_503 mocker.patch("requests.get", return_value=mock_response) response = helpers.stream_file_from_storage_service( pointer_url, error_message=custom_error_message) assert response[CONTENT_TYPE] == CONTENT_JSON err = json.loads(response.content) assert err.get(SUCCESS) is False assert custom_error_message in err.get(MESSAGE) assert response.status_code == RESPONSE_400
def test_stream_pointer_from_storage_no_content_type(mocker, tmpdir, mets_hdr): sip_uuid = "66666666-6666-6666-6666-666666666666" mock_response = requests.Response() pointer_url, pointer_file, content_disposition = setup_ptr_info(sip_uuid) mock_response.headers = {CONTENT_DISPOSITION: content_disposition} mock_response.status_code = RESPONSE_200 mock_response.raw = mets_stream(tmpdir, mets_hdr) mocker.patch.object(requests, "get", return_value=mock_response) response = helpers.stream_file_from_storage_service(pointer_url) assert response.status_code == RESPONSE_200 # The content type will be set to text/plain according to the function. assert response.get(CONTENT_TYPE) == CONTENT_TEXT # Other parts of the response should remain the same. assert response.get(CONTENT_DISPOSITION) == content_disposition response_text = get_streaming_response(response.streaming_content) assert response_text == mets_hdr
def download_ss(request): filepath = base64.b64decode(request.GET.get('filepath', '')).lstrip('/') logger.info('download filepath: %s', filepath) if not filepath.startswith(DEFAULT_BACKLOG_PATH): return django.http.HttpResponseBadRequest() filepath = filepath.replace(DEFAULT_BACKLOG_PATH, '', 1) # Get UUID uuid_regex = r'[\w]{8}(-[\w]{4}){3}-[\w]{12}' transfer_uuid = re.search(uuid_regex, filepath).group() # Get relative path # Find first /, should be at the end of the transfer name/uuid, rest is relative ptah relative_path = filepath[filepath.find('/') + 1:] redirect_url = storage_service.extract_file_url(transfer_uuid, relative_path) return helpers.stream_file_from_storage_service(redirect_url, 'Storage service returned {}; check logs?')
def download_by_uuid(request, uuid): """ Download a file from the Storage Service, given its UUID. This view will stream the response directly from the storage service, so, unlike download_ss, this will work even if the Storage Service is not accessible to the requestor. Returns 404 if a file with the requested UUID cannot be found, and 400 if the storage service fails to retrieve the record. """ try: f = models.File.objects.get(uuid=uuid) except models.File.DoesNotExist: response = { 'success': False, 'message': 'File with UUID ' + uuid + ' could not be found', } return helpers.json_response(response, status_code=404) relative_path = f.currentlocation.replace('%transferDirectory%', '') redirect_url = storage_service.extract_file_url(f.transfer_id, relative_path) return helpers.stream_file_from_storage_service( redirect_url, 'Storage service returned {}; check logs?')
def aip_pointer_file_download(request, uuid): redirect_url = storage_service.pointer_file_url(uuid) return helpers.stream_file_from_storage_service( redirect_url, "Storage service returned {}; check logs?" )