[Core] Don't always write create_torrent metafile to disk

If target=None and add_to_session is True, the torrent will be
directly added to the session without writing the torrent file
to disk. This will allow to programmatically control deluge via
RPC without creating .torrent files all over the place.

Also, make most create_torrent parameters optional.
This commit is contained in:
Radu Carpa 2023-08-02 10:47:40 +02:00 committed by Calum Lind
parent 8dba0efa85
commit b63699c6de
No known key found for this signature in database
GPG Key ID: 90597A687B836BA3
2 changed files with 74 additions and 25 deletions

View File

@ -17,7 +17,7 @@ from base64 import b64decode, b64encode
from typing import Any, Dict, List, Optional, Tuple, Union from typing import Any, Dict, List, Optional, Tuple, Union
from urllib.request import URLError, urlopen from urllib.request import URLError, urlopen
from twisted.internet import defer, reactor, task from twisted.internet import defer, reactor, task, threads
from twisted.web.client import Agent, readBody from twisted.web.client import Agent, readBody
import deluge.common import deluge.common
@ -992,13 +992,13 @@ class Core(component.Component):
path, path,
tracker, tracker,
piece_length, piece_length,
comment, comment=None,
target, target=None,
webseeds, webseeds=None,
private, private=False,
created_by, created_by=None,
trackers, trackers=None,
add_to_session, add_to_session=False,
): ):
log.debug('creating torrent..') log.debug('creating torrent..')
threading.Thread( threading.Thread(
@ -1032,24 +1032,35 @@ class Core(component.Component):
): ):
from deluge import metafile from deluge import metafile
metafile.make_meta_file( filecontent = metafile.make_meta_file_content(
path, path,
tracker, tracker,
piece_length, piece_length,
comment=comment, comment=comment,
target=target,
webseeds=webseeds, webseeds=webseeds,
private=private, private=private,
created_by=created_by, created_by=created_by,
trackers=trackers, trackers=trackers,
) )
write_file = False
if target or not add_to_session:
write_file = True
if not target:
target = metafile.default_meta_file_path(path)
filename = os.path.split(target)[-1]
if write_file:
with open(target, 'wb') as _file:
_file.write(filecontent)
log.debug('torrent created!') log.debug('torrent created!')
if add_to_session: if add_to_session:
options = {} options = {}
options['download_location'] = os.path.split(path)[0] options['download_location'] = os.path.split(path)[0]
with open(target, 'rb') as _file: filedump = b64encode(filecontent)
filedump = b64encode(_file.read()) self.add_torrent_file(filename, filedump, options)
self.add_torrent_file(os.path.split(target)[1], filedump, options)
@export @export
def upload_plugin(self, filename: str, filedump: Union[str, bytes]) -> None: def upload_plugin(self, filename: str, filedump: Union[str, bytes]) -> None:

View File

@ -51,7 +51,7 @@ class RemoteFileProgress:
) )
def make_meta_file( def make_meta_file_content(
path, path,
url, url,
piece_length, piece_length,
@ -60,7 +60,6 @@ def make_meta_file(
comment=None, comment=None,
safe=None, safe=None,
content_type=None, content_type=None,
target=None,
webseeds=None, webseeds=None,
name=None, name=None,
private=False, private=False,
@ -70,14 +69,6 @@ def make_meta_file(
data = {'creation date': int(gmtime())} data = {'creation date': int(gmtime())}
if url: if url:
data['announce'] = url.strip() data['announce'] = url.strip()
a, b = os.path.split(path)
if not target:
if b == '':
f = a + '.torrent'
else:
f = os.path.join(a, b + '.torrent')
else:
f = target
if progress is None: if progress is None:
progress = dummy progress = dummy
@ -121,8 +112,55 @@ def make_meta_file(
data['announce-list'] = trackers data['announce-list'] = trackers
data['encoding'] = 'UTF-8' data['encoding'] = 'UTF-8'
with open(f, 'wb') as file_: return bencode(utf8_encode_structure(data))
file_.write(bencode(utf8_encode_structure(data)))
def default_meta_file_path(content_path):
a, b = os.path.split(content_path)
if b == '':
f = a + '.torrent'
else:
f = os.path.join(a, b + '.torrent')
return f
def make_meta_file(
path,
url,
piece_length,
progress=None,
title=None,
comment=None,
safe=None,
content_type=None,
target=None,
webseeds=None,
name=None,
private=False,
created_by=None,
trackers=None,
):
if not target:
target = default_meta_file_path(path)
file_content = make_meta_file_content(
path,
url,
piece_length,
progress=progress,
title=title,
comment=comment,
safe=safe,
content_type=content_type,
webseeds=webseeds,
name=name,
private=private,
created_by=created_by,
trackers=trackers,
)
with open(target, 'wb') as file_:
file_.write(file_content)
def calcsize(path): def calcsize(path):