Skip to content

Commit

Permalink
[Core] Don't always write create_torrent metafile to disk
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rcarpa committed Aug 21, 2023
1 parent 8001110 commit 12cbebd
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 25 deletions.
37 changes: 24 additions & 13 deletions deluge/core/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
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
Expand Down Expand Up @@ -992,13 +992,13 @@ def create_torrent(
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(
Expand Down Expand Up @@ -1032,24 +1032,35 @@ def _create_torrent_thread(
):
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:
Expand Down
62 changes: 50 additions & 12 deletions deluge/metafile.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def __call__(self, piece_count, num_pieces):
)


def make_meta_file(
def make_meta_file_content(
path,
url,
piece_length,
Expand All @@ -60,7 +60,6 @@ def make_meta_file(
comment=None,
safe=None,
content_type=None,
target=None,
webseeds=None,
name=None,
private=False,
Expand All @@ -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
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 12cbebd

Please sign in to comment.