Skip to content

Commit

Permalink
Merge pull request #7843 from drew2a/refactoring/get_torrent_info
Browse files Browse the repository at this point in the history
Refactor TorrentInfoEndpoint to fix JSON encoding issue
  • Loading branch information
drew2a authored Jan 24, 2024
2 parents 7ca5ecc + b63f14c commit 998b445
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import random
from binascii import unhexlify
from datetime import datetime
import random
from struct import unpack
from typing import Optional
from typing import Dict, Optional

from lz4.frame import LZ4FrameCompressor
from pony import orm
Expand All @@ -11,8 +11,8 @@
from tribler.core import notifications
from tribler.core.components.database.category_filter.category import Category, default_category_filter
from tribler.core.components.database.category_filter.family_filter import default_xxx_filter
from tribler.core.components.database.db.serialization import EPOCH, REGULAR_TORRENT, TorrentMetadataPayload, \
HealthItemsPayload, time2int
from tribler.core.components.database.db.serialization import EPOCH, HealthItemsPayload, REGULAR_TORRENT, \
TorrentMetadataPayload, time2int
from tribler.core.utilities.notifier import Notifier
from tribler.core.utilities.tracker_utils import get_uniformed_tracker_url
from tribler.core.utilities.unicode import ensure_unicode, hexlify
Expand Down Expand Up @@ -42,7 +42,7 @@ def infohash_to_id(infohash):
return abs(unpack(">q", infohash[:8])[0])


def tdef_to_metadata_dict(tdef, category_filter: Category = None):
def tdef_to_metadata_dict(tdef, category_filter: Category = None) -> Dict:
"""
Helper function to create a TorrentMetadata-compatible dict from TorrentDef
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,24 +139,18 @@ async def get_torrent_info(self, request):
self._logger.warning("Received metainfo is not a valid dictionary")
return RESTResponse({"error": "invalid response"}, status=HTTP_INTERNAL_SERVER_ERROR)

# Add the torrent to GigaChannel as a free-for-all entry, so others can search it
self.download_manager.notifier[notifications.torrent_metadata_added](
tdef_to_metadata_dict(TorrentDef.load_from_dict(metainfo)))

# TODO(Martijn): store the stuff in a database!!!
# TODO(Vadim): this means cache the downloaded torrent in a binary storage, like LevelDB
infohash = hashlib.sha1(lt.bencode(metainfo[b'info'])).digest()
# Add the torrent to metadata.db
metadata_dict = tdef_to_metadata_dict(TorrentDef.load_from_dict(metainfo))
self.download_manager.notifier[notifications.torrent_metadata_added](metadata_dict)

infohash = metadata_dict['infohash']
download = self.download_manager.downloads.get(infohash)
metainfo_request = self.download_manager.metainfo_requests.get(infohash, [None])[0]
download_is_metainfo_request = download == metainfo_request

# Check if the torrent is already in the downloads
encoded_metainfo = deepcopy(metainfo)

# FIXME: json.dumps garbles binary data that is used by the 'pieces' field
# However, this is fine as long as the GUI does not use this field.
encoded_metainfo[b'info'][b'pieces'] = hexlify(encoded_metainfo[b'info'][b'pieces']).encode('utf-8')
encoded_metainfo = hexlify(json.dumps(recursive_unicode(
encoded_metainfo, ignore_errors=True), ensure_ascii=False).encode('utf-8'))
return RESTResponse({"metainfo": encoded_metainfo,
Expand Down

0 comments on commit 998b445

Please sign in to comment.