Skip to content

Commit

Permalink
save file-torrent association for file list
Browse files Browse the repository at this point in the history
  • Loading branch information
shyba committed Sep 16, 2022
1 parent 780ab0e commit 9236854
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
23 changes: 16 additions & 7 deletions lbry/extras/daemon/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,23 +211,26 @@ def delete_torrent(transaction: sqlite3.Connection, bt_infohash: str):
transaction.execute("delete from torrent where bt_infohash=?", (bt_infohash,)).fetchall()


def store_file(transaction: sqlite3.Connection, stream_hash: str, file_name: typing.Optional[str],
def store_file(transaction: sqlite3.Connection, identifier_value: str, file_name: typing.Optional[str],
download_directory: typing.Optional[str], data_payment_rate: float, status: str,
content_fee: typing.Optional[Transaction], added_on: typing.Optional[int] = None) -> int:
if not file_name and not download_directory:
encoded_file_name, encoded_download_dir = None, None
else:
encoded_file_name = binascii.hexlify(file_name.encode()).decode()
encoded_download_dir = binascii.hexlify(download_directory.encode()).decode()
is_torrent = len(identifier_value) == 40
time_added = added_on or int(time.time())
transaction.execute(
"insert or replace into file values (?, NULL, ?, ?, ?, ?, ?, ?, ?)",
(stream_hash, encoded_file_name, encoded_download_dir, data_payment_rate, status,
f"insert or replace into file values ({'NULL, ?' if is_torrent else '?, NULL'}, ?, ?, ?, ?, ?, ?, ?)",
(identifier_value, encoded_file_name, encoded_download_dir, data_payment_rate, status,
1 if (file_name and download_directory and os.path.isfile(os.path.join(download_directory, file_name))) else 0,
None if not content_fee else binascii.hexlify(content_fee.raw).decode(), time_added)
).fetchall()

return transaction.execute("select rowid from file where stream_hash=?", (stream_hash, )).fetchone()[0]
return transaction.execute(
f"select rowid from file where {'bt_infohash' if is_torrent else 'stream_hash'}=?",
(identifier_value, )).fetchone()[0]


class SQLiteStorage(SQLiteMixin):
Expand Down Expand Up @@ -872,11 +875,17 @@ async def save_content_claim(self, stream_hash, claim_outpoint):
if stream_hash in self.content_claim_callbacks:
await self.content_claim_callbacks[stream_hash]()

def _save_torrent(self, transaction, bt_infohash, length, name):
transaction.execute(
"insert or replace into torrent values (?, NULL, ?, ?)", (bt_infohash, length, name)
).fetchall()

async def add_torrent(self, bt_infohash, length, name):
return await self.db.run(self._save_torrent, bt_infohash, length, name)

async def save_torrent_content_claim(self, bt_infohash, claim_outpoint, length, name):
def _save_torrent(transaction):
transaction.execute(
"insert or replace into torrent values (?, NULL, ?, ?)", (bt_infohash, length, name)
).fetchall()
self._save_torrent(transaction, bt_infohash, length, name)
transaction.execute(
"insert or replace into content_claim values (NULL, ?, ?)", (bt_infohash, claim_outpoint)
).fetchall()
Expand Down
7 changes: 7 additions & 0 deletions lbry/torrent/torrent_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
import os
import typing
from pathlib import Path
from typing import Optional
from aiohttp.web import Request, StreamResponse, HTTPRequestRangeNotSatisfiable

Expand Down Expand Up @@ -57,6 +58,12 @@ def mime_type(self) -> Optional[str]:

async def start(self, timeout: Optional[float] = None, save_now: Optional[bool] = False):
await self.torrent_session.add_torrent(self.identifier, self.download_directory)
self.download_directory = self.torrent_session.save_path(self.identifier)
self._file_name = Path(self.torrent_session.full_path(self.identifier)).name
await self.storage.add_torrent(self.identifier, self.torrent_length, self.torrent_name)
self.rowid = await self.storage.save_downloaded_file(
self.identifier, self.file_name, self.download_directory, 0.0, added_on=self._added_on
)

async def stop(self, finished: bool = False):
await self.torrent_session.remove_torrent(self.identifier)
Expand Down

0 comments on commit 9236854

Please sign in to comment.