Skip to content

Commit

Permalink
Fix for invalid cid while checking directory_is_multihash (#721)
Browse files Browse the repository at this point in the history
* Fix for invalid cid while checking directory_is_multihash

* Newline

* lint

* print multihash in exception

* Change IPLD in favor if CID
  • Loading branch information
dmanjunath committed Aug 4, 2020
1 parent 508ae6a commit ec938de
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
16 changes: 12 additions & 4 deletions discovery-provider/src/tasks/tracks.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,18 @@ def parse_track_event(
# if cover_art CID is of a dir, store under _sizes field instead
if track_record.cover_art:
logger.warning(f"tracks.py | Processing track cover art {track_record.cover_art}")
is_directory = update_task.ipfs_client.multihash_is_directory(track_record.cover_art)
if is_directory:
track_record.cover_art_sizes = track_record.cover_art
track_record.cover_art = None
try:
is_directory = update_task.ipfs_client.multihash_is_directory(track_record.cover_art)
if is_directory:
track_record.cover_art_sizes = track_record.cover_art
track_record.cover_art = None
except Exception as e:
# we are unable to get the cover art
if 'invalid multihash' in str(e):
track_record.cover_art_sizes = None
track_record.cover_art = None
else:
raise e

update_remixes_table(session, track_record, track_metadata)

Expand Down
11 changes: 11 additions & 0 deletions discovery-provider/src/utils/ipfs_lib.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import json
import time
import re
from urllib.parse import urlparse, urljoin
import requests
from requests.exceptions import ReadTimeout
Expand Down Expand Up @@ -164,6 +165,10 @@ def cat(self, multihash):
raise # error is of type ipfshttpclient.exceptions.TimeoutError

def multihash_is_directory(self, multihash):
# Check if the multihash is valid
if not self.cid_is_valid(multihash):
raise Exception(f'invalid multihash {multihash}')

# First attempt to cat multihash locally.
try:
# If cat successful, multihash is not directory.
Expand Down Expand Up @@ -232,3 +237,9 @@ def update_cnode_urls(self, cnode_endpoints):

def ipfs_id_multiaddr(self):
return self._multiaddr

def cid_is_valid(self, cid):
if cid and re.match(r"^Qm[a-zA-Z0-9]{44}$", cid):
return True

return False

0 comments on commit ec938de

Please sign in to comment.