summaryrefslogtreecommitdiffstats
path: root/deluge
diff options
context:
space:
mode:
authorCalum Lind <calumlind+deluge@gmail.com>2022-05-01 14:48:16 +0100
committerCalum Lind <calumlind+deluge@gmail.com>2022-05-01 20:38:09 +0100
commit7f0a38057649e03f5d67146e561ad04dc146a8f6 (patch)
tree100c810463f080eb18a1d15dd3dbcaa1f9197e22 /deluge
parent68c75ccc0540b435ade9a833ba5eadd5f0464f5f (diff)
downloaddeluge-7f0a38057649e03f5d67146e561ad04dc146a8f6.tar.gz
deluge-7f0a38057649e03f5d67146e561ad04dc146a8f6.tar.bz2
deluge-7f0a38057649e03f5d67146e561ad04dc146a8f6.zip
[Core] Cleanup temp files in add_torrent_url
Temporary torrent files are not deleted by add_torrent_url. Not as big a problem as with tracker icons pages but should be removed after use. Fixed by updating the method to use async and a try..finally cleanup block. Perhaps could be refactored to not require temporary files and instead store the downloaded torrent as object for passing to add_torrent_file. Trac: https://dev.deluge-torrent.org/ticket/3167
Diffstat (limited to 'deluge')
-rw-r--r--deluge/core/core.py33
-rw-r--r--deluge/tests/test_core.py4
2 files changed, 19 insertions, 18 deletions
diff --git a/deluge/core/core.py b/deluge/core/core.py
index fa862c30a..18fda68b2 100644
--- a/deluge/core/core.py
+++ b/deluge/core/core.py
@@ -518,7 +518,8 @@ class Core(component.Component):
return task.deferLater(reactor, 0, add_torrents)
@export
- def add_torrent_url(
+ @maybe_coroutine
+ async def add_torrent_url(
self, url: str, options: dict, headers: dict = None
) -> 'defer.Deferred[Optional[str]]':
"""Adds a torrent from a URL. Deluge will attempt to fetch the torrent
@@ -534,26 +535,24 @@ class Core(component.Component):
"""
log.info('Attempting to add URL %s', url)
- def on_download_success(filename):
- # We got the file, so add it to the session
+ tmp_fd, tmp_file = tempfile.mkstemp(prefix='deluge_url.', suffix='.torrent')
+ try:
+ filename = await download_file(
+ url, tmp_file, headers=headers, force_filename=True
+ )
+ except Exception:
+ log.error('Failed to add torrent from URL %s', url)
+ raise
+ else:
with open(filename, 'rb') as _file:
data = _file.read()
+ return self.add_torrent_file(filename, b64encode(data), options)
+ finally:
try:
- os.remove(filename)
+ os.close(tmp_fd)
+ os.remove(tmp_file)
except OSError as ex:
- log.warning('Could not remove temp file: %s', ex)
- return self.add_torrent_file(filename, b64encode(data), options)
-
- def on_download_fail(failure):
- # Log the error and pass the failure onto the client
- log.error('Failed to add torrent from URL %s', url)
- return failure
-
- tmp_fd, tmp_file = tempfile.mkstemp(prefix='deluge_url.', suffix='.torrent')
- os.close(tmp_fd)
- d = download_file(url, tmp_file, headers=headers, force_filename=True)
- d.addCallbacks(on_download_success, on_download_fail)
- return d
+ log.warning(f'Unable to delete temp file {tmp_file}: , {ex}')
@export
def add_torrent_magnet(self, uri: str, options: dict) -> str:
diff --git a/deluge/tests/test_core.py b/deluge/tests/test_core.py
index b0392c720..42457480d 100644
--- a/deluge/tests/test_core.py
+++ b/deluge/tests/test_core.py
@@ -4,6 +4,7 @@
# See LICENSE for more details.
#
+import os
from base64 import b64encode
from hashlib import sha1 as sha
@@ -175,7 +176,7 @@ class TestCore(BaseTestCase):
self.core.add_torrent_file(filename, False, options)
@pytest_twisted.inlineCallbacks
- def test_add_torrent_url(self):
+ def test_add_torrent_url(self, mock_mkstemp):
url = (
'http://localhost:%d/ubuntu-9.04-desktop-i386.iso.torrent'
% self.listen_port
@@ -185,6 +186,7 @@ class TestCore(BaseTestCase):
torrent_id = yield self.core.add_torrent_url(url, options)
assert torrent_id == info_hash
+ assert not os.path.isfile(mock_mkstemp[1])
@pytest_twisted.ensureDeferred
async def test_add_torrent_url_with_cookie(self):