From 8683bd82ede79cd02ea318d3a334b6d98e1a0a9d Mon Sep 17 00:00:00 2001 From: Faris Ansari Date: Wed, 29 Aug 2018 16:18:36 +0530 Subject: [PATCH] fix: Base64 extraction - use requests to fetch the image without storing it anywhere - only read local files --- erpnext/hub_node/api.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/erpnext/hub_node/api.py b/erpnext/hub_node/api.py index c46e6dce51..441d30b10d 100644 --- a/erpnext/hub_node/api.py +++ b/erpnext/hub_node/api.py @@ -137,25 +137,29 @@ def item_sync_postprocess(sync_details): def load_base64_image_from_items(items): - import io, base64, urllib, os + import io, base64, urllib, os, requests, tempfile from frappe.utils.file_manager import get_file_path for item in items: file_path = item['image'] file_name = os.path.basename(file_path) + base64content = None if file_path.startswith('http'): + # fetch content and then base64 it url = file_path - file_path = os.path.join('/tmp', file_name) - urllib.urlretrieve(url, file_path) + response = requests.get(url) + base64content = base64.b64encode(response.content) else: + # read file then base64 it file_path = os.path.abspath(get_file_path(file_path)) + with io.open(file_path, 'rb') as f: + base64content = base64.b64encode(f.read()) - with io.open(file_path, 'rb') as f: - image_data = json.dumps({ - 'file_name': file_name, - 'base64': base64.b64encode(f.read()) - }) + image_data = json.dumps({ + 'file_name': file_name, + 'base64': base64content + }) item['image'] = image_data