Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Core] implement synchronous and v2 metafile creation #430

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 46 additions & 30 deletions deluge/core/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,16 @@
import os
import shutil
import tempfile
import threading
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
import deluge.component as component
from deluge import path_chooser_common
from deluge import metafile, path_chooser_common
from deluge._libtorrent import LT_VERSION, lt
from deluge.configmanager import ConfigManager, get_config_dir
from deluge.core.alertmanager import AlertManager
Expand Down Expand Up @@ -992,30 +991,33 @@ 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,
torrent_format=metafile.TorrentFormat.V1,
):
if isinstance(torrent_format, str):
torrent_format = metafile.TorrentFormat(torrent_format)

log.debug('creating torrent..')
threading.Thread(
target=self._create_torrent_thread,
args=(
path,
tracker,
piece_length,
comment,
target,
webseeds,
private,
created_by,
trackers,
add_to_session,
),
).start()
return threads.deferToThread(
self._create_torrent_thread,
path,
tracker,
piece_length,
comment=comment,
target=target,
webseeds=webseeds,
private=private,
created_by=created_by,
trackers=trackers,
add_to_session=add_to_session,
torrent_format=torrent_format,
)

def _create_torrent_thread(
self,
Expand All @@ -1029,27 +1031,41 @@ def _create_torrent_thread(
created_by,
trackers,
add_to_session,
torrent_format,
):
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,
torrent_format=torrent_format,
)

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)

filedump = b64encode(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)
self.add_torrent_file(filename, filedump, options)
return filename, filedump

@export
def upload_plugin(self, filename: str, filedump: Union[str, bytes]) -> None:
Expand Down
Loading