From 4b2b2fc8ed9ad4892f708296e169f13ba20b27fb Mon Sep 17 00:00:00 2001 From: Jamie Hewitt Date: Tue, 10 Oct 2023 13:16:19 +0300 Subject: [PATCH 1/2] Adding logs and exception handling --- server/oceandbs/utils.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/server/oceandbs/utils.py b/server/oceandbs/utils.py index 3246e45..f22cd35 100644 --- a/server/oceandbs/utils.py +++ b/server/oceandbs/utils.py @@ -44,14 +44,24 @@ def upload_files_to_ipfs(request_files, quote): files = response.text.splitlines() for file in files: + print("Processing files from IPFS response...") added_file = {} - json_version = json.loads(file) + try: + json_version = json.loads(file) + except json.JSONDecodeError: + print(f"Error parsing IPFS response for file '{file}'. Invalid JSON received.") + continue added_file['title'] = json_version['Name'] print(f"File '{added_file['title']}' uploaded successfully to IPFS. {json_version['Name']}") added_file['cid'] = json_version['Hash'] added_file['public_url'] = f"https://ipfs.io/ipfs/{added_file['cid']}?filename={added_file['title']}" added_file['length'] = json_version['Size'] - added_file['content_type'] = content_types.get(json_version['Name'], "application/octet-stream") # Retrieve the content type using the filename as key + content_type_retrieved = content_types.get(json_version['Name'], None) + if content_type_retrieved: + added_file['content_type'] = content_type_retrieved + else: + print(f"Warning: No content type found for file '{json_version['Name']}'. Using default.") + added_file['content_type'] = "application/octet-stream" File.objects.create(quote=quote, **added_file) files_reference.append({ "ipfs_uri": "ipfs://" + str(added_file['cid']), From 864df01b1054b60ba71f24a14988c9569e2e17d3 Mon Sep 17 00:00:00 2001 From: Jamie Hewitt Date: Tue, 10 Oct 2023 13:17:58 +0300 Subject: [PATCH 2/2] Adding additional logs --- server/oceandbs/utils.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/server/oceandbs/utils.py b/server/oceandbs/utils.py index f22cd35..95d7fb0 100644 --- a/server/oceandbs/utils.py +++ b/server/oceandbs/utils.py @@ -42,31 +42,40 @@ def upload_files_to_ipfs(request_files, quote): response = requests.post(url, files=file_data) response.raise_for_status() # This will raise an error for HTTP error responses + print("Processing files from IPFS response...") + files = response.text.splitlines() for file in files: - print("Processing files from IPFS response...") added_file = {} try: json_version = json.loads(file) except json.JSONDecodeError: print(f"Error parsing IPFS response for file '{file}'. Invalid JSON received.") continue + added_file['title'] = json_version['Name'] print(f"File '{added_file['title']}' uploaded successfully to IPFS. {json_version['Name']}") added_file['cid'] = json_version['Hash'] added_file['public_url'] = f"https://ipfs.io/ipfs/{added_file['cid']}?filename={added_file['title']}" added_file['length'] = json_version['Size'] + content_type_retrieved = content_types.get(json_version['Name'], None) if content_type_retrieved: added_file['content_type'] = content_type_retrieved else: print(f"Warning: No content type found for file '{json_version['Name']}'. Using default.") added_file['content_type'] = "application/octet-stream" + + print(f"Saving file '{added_file['title']}' to the database...") File.objects.create(quote=quote, **added_file) + print(f"File '{added_file['title']}' saved successfully to the database.") + files_reference.append({ "ipfs_uri": "ipfs://" + str(added_file['cid']), "content_type": added_file['content_type'] }) + print(f"Processed file '{added_file['title']}' with CID '{added_file['cid']}' and content type '{added_file['content_type']}'.") + except requests.RequestException as e: print(f"HTTP error uploading to IPFS: {e}")