Skip to content

Commit 9236854

Browse files
committed
save file-torrent association for file list
1 parent 780ab0e commit 9236854

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

lbry/extras/daemon/storage.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -211,23 +211,26 @@ def delete_torrent(transaction: sqlite3.Connection, bt_infohash: str):
211211
transaction.execute("delete from torrent where bt_infohash=?", (bt_infohash,)).fetchall()
212212

213213

214-
def store_file(transaction: sqlite3.Connection, stream_hash: str, file_name: typing.Optional[str],
214+
def store_file(transaction: sqlite3.Connection, identifier_value: str, file_name: typing.Optional[str],
215215
download_directory: typing.Optional[str], data_payment_rate: float, status: str,
216216
content_fee: typing.Optional[Transaction], added_on: typing.Optional[int] = None) -> int:
217217
if not file_name and not download_directory:
218218
encoded_file_name, encoded_download_dir = None, None
219219
else:
220220
encoded_file_name = binascii.hexlify(file_name.encode()).decode()
221221
encoded_download_dir = binascii.hexlify(download_directory.encode()).decode()
222+
is_torrent = len(identifier_value) == 40
222223
time_added = added_on or int(time.time())
223224
transaction.execute(
224-
"insert or replace into file values (?, NULL, ?, ?, ?, ?, ?, ?, ?)",
225-
(stream_hash, encoded_file_name, encoded_download_dir, data_payment_rate, status,
225+
f"insert or replace into file values ({'NULL, ?' if is_torrent else '?, NULL'}, ?, ?, ?, ?, ?, ?, ?)",
226+
(identifier_value, encoded_file_name, encoded_download_dir, data_payment_rate, status,
226227
1 if (file_name and download_directory and os.path.isfile(os.path.join(download_directory, file_name))) else 0,
227228
None if not content_fee else binascii.hexlify(content_fee.raw).decode(), time_added)
228229
).fetchall()
229230

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

232235

233236
class SQLiteStorage(SQLiteMixin):
@@ -872,11 +875,17 @@ async def save_content_claim(self, stream_hash, claim_outpoint):
872875
if stream_hash in self.content_claim_callbacks:
873876
await self.content_claim_callbacks[stream_hash]()
874877

878+
def _save_torrent(self, transaction, bt_infohash, length, name):
879+
transaction.execute(
880+
"insert or replace into torrent values (?, NULL, ?, ?)", (bt_infohash, length, name)
881+
).fetchall()
882+
883+
async def add_torrent(self, bt_infohash, length, name):
884+
return await self.db.run(self._save_torrent, bt_infohash, length, name)
885+
875886
async def save_torrent_content_claim(self, bt_infohash, claim_outpoint, length, name):
876887
def _save_torrent(transaction):
877-
transaction.execute(
878-
"insert or replace into torrent values (?, NULL, ?, ?)", (bt_infohash, length, name)
879-
).fetchall()
888+
self._save_torrent(transaction, bt_infohash, length, name)
880889
transaction.execute(
881890
"insert or replace into content_claim values (NULL, ?, ?)", (bt_infohash, claim_outpoint)
882891
).fetchall()

lbry/torrent/torrent_manager.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import logging
44
import os
55
import typing
6+
from pathlib import Path
67
from typing import Optional
78
from aiohttp.web import Request, StreamResponse, HTTPRequestRangeNotSatisfiable
89

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

5859
async def start(self, timeout: Optional[float] = None, save_now: Optional[bool] = False):
5960
await self.torrent_session.add_torrent(self.identifier, self.download_directory)
61+
self.download_directory = self.torrent_session.save_path(self.identifier)
62+
self._file_name = Path(self.torrent_session.full_path(self.identifier)).name
63+
await self.storage.add_torrent(self.identifier, self.torrent_length, self.torrent_name)
64+
self.rowid = await self.storage.save_downloaded_file(
65+
self.identifier, self.file_name, self.download_directory, 0.0, added_on=self._added_on
66+
)
6067

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

0 commit comments

Comments
 (0)