From 458ac95a2242c0f6f9fb8dd1d7568f3313efaa90 Mon Sep 17 00:00:00 2001 From: Jamie Hewitt Date: Tue, 7 Nov 2023 15:23:43 +0300 Subject: [PATCH 1/2] updating for IPFS cluster response --- server/oceandbs/utils.py | 72 ++++++++++++++++++++-------------------- 1 file changed, 36 insertions(+), 36 deletions(-) diff --git a/server/oceandbs/utils.py b/server/oceandbs/utils.py index 1f26959..42f228a 100644 --- a/server/oceandbs/utils.py +++ b/server/oceandbs/utils.py @@ -20,13 +20,16 @@ # This function is used to upload the files temporarily to IPFS def upload_files_to_ipfs(request_files, quote): files_reference = [] - url = getattr(settings, 'IPFS_SERVICE_ENDPOINT') or "http://127.0.0.1:5001/api/v0/add" + url = getattr(settings, 'IPFS_SERVICE_ENDPOINT', "http://127.0.0.1:5001/api/v0/add") print('IPFS URL: ', url) # Preparing files with appropriate content type file_data = {} content_types = {} # New dictionary for content types + if not request_files: + raise ValueError("No files provided for upload to IPFS.") + for field_name, uploaded_file in request_files.items(): print(f"Processing file '{uploaded_file}'.") content_type, _ = mimetypes.guess_type(uploaded_file.name) @@ -35,8 +38,10 @@ def upload_files_to_ipfs(request_files, quote): content_types[uploaded_file.name] = content_type # Save the content type in the new dictionary else: print(f"Could not guess MIME type for file '{uploaded_file.name}'. Using default.") - - file_data[field_name] = uploaded_file # Always store the uploaded_file in file_data + content_type = 'application/octet-stream' # Use a default MIME type + + # Update the file_data to include the content type + file_data[field_name] = (uploaded_file.name, uploaded_file, content_type) try: response = requests.post(url, files=file_data) @@ -45,37 +50,34 @@ def upload_files_to_ipfs(request_files, quote): print("Processing files from IPFS response...") print("Raw IPFS response:", response.text) # Print the raw response - files = response.text.splitlines() - for file in files: - added_file = {} - try: - json_version = json.loads(file) - print(f"JSON response for file: {json_version}") # Print the JSON response for each file - - # Check if 'Name' is in the json_version before proceeding - if 'Name' in json_version: - 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) - print(f"Content type for file '{added_file['title']}' is '{content_type_retrieved}'.") - 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": content_type_retrieved - }) - else: - print("Warning: 'Name' key not found in the IPFS response.") - except json.JSONDecodeError: - print(f"Error parsing IPFS response for file '{file}'. Invalid JSON received.") - continue - + # Parse the response as JSON once instead of splitting lines + json_response = response.json() + for json_version in json_response: + print(f"JSON response for file: {json_version}") # Print the JSON response for each file + + # Correct the key names according to the IPFS response + file_name = json_version.get('name') + if file_name: + added_file = { + 'title': file_name, + 'cid': json_version.get('cid'), + 'public_url': f"https://ipfs.io/ipfs/{json_version.get('cid')}?filename={file_name}", + 'length': json_version.get('size'), + } + + content_type_retrieved = content_types.get(file_name, 'application/octet-stream') + print(f"Content type for file '{file_name}' is '{content_type_retrieved}'.") + print(f"Saving file '{file_name}' to the database...") + File.objects.create(quote=quote, **added_file) + print(f"File '{file_name}' saved successfully to the database.") + + files_reference.append({ + "ipfs_uri": "ipfs://" + json_version.get('cid'), + "content_type": content_type_retrieved + }) + else: + print("Warning: 'name' key not found in the IPFS response for a file.") + except requests.RequestException as e: print(f"HTTP error uploading to IPFS: {e}") raise ValueError(f"HTTP error uploading to IPFS: {e}") @@ -87,8 +89,6 @@ def upload_files_to_ipfs(request_files, quote): print(f"files_reference: {files_reference}") return files_reference - - # The function below is used to generate an allowance for the file upload def create_allowance(quote, user_private_key, abi): if not all([quote, user_private_key, abi]): From 80071f68f5c981bfc868f2566f7a68f1bab2804b Mon Sep 17 00:00:00 2001 From: Jamie Hewitt Date: Tue, 7 Nov 2023 15:28:03 +0300 Subject: [PATCH 2/2] Updating IPFS url --- server/oceandbs/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/oceandbs/utils.py b/server/oceandbs/utils.py index 42f228a..589bde2 100644 --- a/server/oceandbs/utils.py +++ b/server/oceandbs/utils.py @@ -20,7 +20,7 @@ # This function is used to upload the files temporarily to IPFS def upload_files_to_ipfs(request_files, quote): files_reference = [] - url = getattr(settings, 'IPFS_SERVICE_ENDPOINT', "http://127.0.0.1:5001/api/v0/add") + url = getattr(settings, 'IPFS_SERVICE_ENDPOINT') or "http://127.0.0.1:5001/api/v0/add" print('IPFS URL: ', url) # Preparing files with appropriate content type