summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRadu Carpa <radu.carpa@cern.ch>2023-08-02 10:47:40 +0200
committerCalum Lind <calumlind+deluge@gmail.com>2023-11-20 11:05:33 +0100
commitb63699c6deec1e26c0d80070ff53fa13bd863d8a (patch)
tree381211834a09054a0544c2bcd231d402b0802fad
parent[Lint] Fixup files (diff)
downloaddeluge-b63699c6deec1e26c0d80070ff53fa13bd863d8a.tar.xz
deluge-b63699c6deec1e26c0d80070ff53fa13bd863d8a.zip
[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.
-rw-r--r--deluge/core/core.py37
-rw-r--r--deluge/metafile.py62
2 files changed, 74 insertions, 25 deletions
diff --git a/deluge/core/core.py b/deluge/core/core.py
index 198410e31..c7428baa2 100644
--- a/deluge/core/core.py
+++ b/deluge/core/core.py
@@ -17,7 +17,7 @@ from base64 import b64decode, b64encode
from typing import Any, Dict, List, Optional, Tuple, Union
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
import deluge.common
@@ -992,13 +992,13 @@ class Core(component.Component):
path,
tracker,
piece_length,
- comment,
- target,
- webseeds,
- private,
- created_by,
- trackers,
- add_to_session,
+ comment=None,
+ target=None,
+ webseeds=None,
+ private=False,
+ created_by=None,
+ trackers=None,
+ add_to_session=False,
):
log.debug('creating torrent..')
threading.Thread(
@@ -1032,24 +1032,35 @@ class Core(component.Component):
):
from deluge import metafile
- metafile.make_meta_file(
+ filecontent = metafile.make_meta_file_content(
path,
tracker,
piece_length,
comment=comment,
- target=target,
webseeds=webseeds,
private=private,
created_by=created_by,
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!')
if add_to_session:
options = {}
options['download_location'] = os.path.split(path)[0]
- with open(target, 'rb') as _file:
- filedump = b64encode(_file.read())
- self.add_torrent_file(os.path.split(target)[1], filedump, options)
+ filedump = b64encode(filecontent)
+ self.add_torrent_file(filename, filedump, options)
@export
def upload_plugin(self, filename: str, filedump: Union[str, bytes]) -> None:
diff --git a/deluge/metafile.py b/deluge/metafile.py
index cd6545a75..906cc81e3 100644
--- a/deluge/metafile.py
+++ b/deluge/metafile.py
@@ -51,7 +51,7 @@ class RemoteFileProgress:
)
-def make_meta_file(
+def make_meta_file_content(
path,
url,
piece_length,
@@ -60,7 +60,6 @@ def make_meta_file(
comment=None,
safe=None,
content_type=None,
- target=None,
webseeds=None,
name=None,
private=False,
@@ -70,14 +69,6 @@ def make_meta_file(
data = {'creation date': int(gmtime())}
if url:
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:
progress = dummy
@@ -121,8 +112,55 @@ def make_meta_file(
data['announce-list'] = trackers
data['encoding'] = 'UTF-8'
- with open(f, 'wb') as file_:
- file_.write(bencode(utf8_encode_structure(data)))
+ return 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):