From 68c4be0f038443d300dd1481abc70db9887eeb2f Mon Sep 17 00:00:00 2001 From: Wambere Date: Fri, 8 Mar 2024 18:09:52 +0300 Subject: [PATCH 1/2] save image as binary resource --- importer/main.py | 53 +++++++++++++++++++++++++++++++++++++++ importer/sample_config.py | 1 + 2 files changed, 54 insertions(+) diff --git a/importer/main.py b/importer/main.py index ed92c021..fc6dfeef 100644 --- a/importer/main.py +++ b/importer/main.py @@ -7,6 +7,7 @@ import logging import logging.config import backoff +import base64 from datetime import datetime from oauthlib.oauth2 import LegacyApplicationClient from requests_oauthlib import OAuth2Session @@ -1102,6 +1103,58 @@ def export_resources_to_csv(resource_type, parameter, value, limit): logging.error(f"Failed to retrieve resource. Status code: {response[1]} response: {response[0]}") +def encode_image(image_file): + with open(image_file, 'rb') as image: + image_b64_data = base64.b64encode(image.read()) + return image_b64_data + + +# This function takes in the source url of an image, downloads it, encodes it, +# and saves it as a Binary resource. It returns the id of the Binary resource if +# successful and 0 if failed +def save_image(image_source_url): + headers = {"Authorization": "Bearer " + config.product_access_token} + data = requests.get(url=image_source_url, headers=headers) + if data.status_code == 200: + with open('images/image_file', 'wb') as image_file: + image_file.write(data.content) + + encoded_image = encode_image('images/image_file') + resource_id = str(uuid.uuid5(uuid.NAMESPACE_DNS, image_source_url)) + payload = { + "resourceType": "Bundle", + "type": "transaction", + "entry": [{ + "request": { + "method": "PUT", + "url": "Binary/" + resource_id, + "ifMatch": "1" + }, + "resource": { + "resourceType": "Binary", + "id": resource_id, + "contentType": "image/png", + "data": str(encoded_image) + } + }] + } + payload_string = json.dumps(payload, indent=4) + response = handle_request("POST", payload_string, get_base_url()) + if response.status_code == 200: + logging.info("Binary resource created successfully") + logging.info(response.text) + return resource_id + else: + logging.error("Error while creating Binary resource") + logging.error(response.text) + return 0 + else: + logging.error("Error while attempting to retrieve image") + logging.error(data) + return 0 + + + class ResponseFilter(logging.Filter): def __init__(self, param=None): self.param = param diff --git a/importer/sample_config.py b/importer/sample_config.py index d46cc9ac..b8432523 100644 --- a/importer/sample_config.py +++ b/importer/sample_config.py @@ -6,3 +6,4 @@ fhir_base_url = 'https://example.smartregister.org/fhir' keycloak_url = 'https://keycloak.smartregister.org/auth/admin/realms/example-realm' access_token = 'example-access-token' +product_access_token = 'example-product-access-token' From a080028640ce50115cfbeb7c8fb1e8453bb0fb40 Mon Sep 17 00:00:00 2001 From: Wambere Date: Tue, 12 Mar 2024 18:16:29 +0300 Subject: [PATCH 2/2] Get image file type --- importer/main.py | 7 ++++++- importer/requirements.txt | 3 ++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/importer/main.py b/importer/main.py index fc6dfeef..acf8e5e9 100644 --- a/importer/main.py +++ b/importer/main.py @@ -8,6 +8,7 @@ import logging.config import backoff import base64 +import magic from datetime import datetime from oauthlib.oauth2 import LegacyApplicationClient from requests_oauthlib import OAuth2Session @@ -1119,6 +1120,10 @@ def save_image(image_source_url): with open('images/image_file', 'wb') as image_file: image_file.write(data.content) + # get file type + mime = magic.Magic(mime=True) + file_type = mime.from_file('images/image_file') + encoded_image = encode_image('images/image_file') resource_id = str(uuid.uuid5(uuid.NAMESPACE_DNS, image_source_url)) payload = { @@ -1133,7 +1138,7 @@ def save_image(image_source_url): "resource": { "resourceType": "Binary", "id": resource_id, - "contentType": "image/png", + "contentType": file_type, "data": str(encoded_image) } }] diff --git a/importer/requirements.txt b/importer/requirements.txt index ac3b213f..4c748647 100644 --- a/importer/requirements.txt +++ b/importer/requirements.txt @@ -6,4 +6,5 @@ urllib3==2.0.3 backoff==2.2.1 pytest==7.4.2 jsonschema==4.21.1 -mock==5.1.0 \ No newline at end of file +mock==5.1.0 +python-magic==0.4.27 \ No newline at end of file