@@ -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
233236class 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 ()
0 commit comments