Skip to content

Commit

Permalink
Adding additional error handling into the upload_files_to_ipfs functi…
Browse files Browse the repository at this point in the history
…on + updating .env.example
  • Loading branch information
jamiehewitt15 committed Jul 31, 2023
1 parent f44cd85 commit 79ad36f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
APP_NAME=dbs
PYTHONUNBUFFERED=1
DJANGO_LOG_LEVEL=DEBUG
IPFS_SERVICE_ENDPOINT="http://127.0.0.1:5001/api/v0/add"

# FILECOIN RELATED CONFIGS
DBS_URL='http://127.0.0.1:8000'
Expand Down
37 changes: 24 additions & 13 deletions server/oceandbs/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,34 @@

from .models import File

# This function is used to upload the files temporary to IPFS
# 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)

response = requests.post(url, files=request_files)
files = response.text.splitlines()
for file in files:
added_file = {}
json_version = json.loads(file)
added_file['title'] = 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']
File.objects.create(quote=quote, **added_file)
files_reference.append("ipfs://" + str(added_file['cid']))
try:
response = requests.post(url, files=request_files)
response.raise_for_status() # This will raise an error for HTTP error responses

files = response.text.splitlines()
for file in files:
added_file = {}
json_version = json.loads(file)
added_file['title'] = 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']
File.objects.create(quote=quote, **added_file)
files_reference.append("ipfs://" + str(added_file['cid']))

except requests.RequestException as e:
print(f"HTTP error uploading to IPFS: {e}")
raise ValueError(f"HTTP error uploading to IPFS: {e}")

except Exception as e:
print(f"Error processing the uploaded files: {e}")
raise ValueError(f"Error processing the uploaded files: {e}")

return files_reference

Expand Down

0 comments on commit 79ad36f

Please sign in to comment.