Skip to content

Commit

Permalink
Return corrupt logic
Browse files Browse the repository at this point in the history
  • Loading branch information
drew2a committed Jul 16, 2022
1 parent 0e8eb2f commit a5c1408
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
from tribler.core.components.watch_folder.settings import WatchFolderSettings
from tribler.core.components.watch_folder.watch_folder import WatchFolder
from tribler.core.tests.tools.common import TESTS_DATA_DIR, TORRENT_UBUNTU_FILE


# pylint: disable=redefined-outer-name, protected-access
from tribler.core.utilities.path_util import Path


@pytest.fixture
Expand Down Expand Up @@ -55,9 +54,13 @@ async def test_watchfolder_torrent_file_corrupt(watch_folder: WatchFolder):
directory = watch_folder.settings.get_path_as_absolute('directory', watch_folder.state_dir)

shutil.copyfile(TORRENT_UBUNTU_FILE, directory / "test.torrent")
shutil.copyfile(TESTS_DATA_DIR / 'test_rss.xml', directory / "test2.torrent")
with pytest.raises(NoCrashException):
await watch_folder.check_watch_folder_handle_exceptions()
corrupted_torrent = directory / "test2.torrent"
shutil.copyfile(TESTS_DATA_DIR / 'test_rss.xml', corrupted_torrent)

await watch_folder.check_watch_folder_handle_exceptions()

assert not corrupted_torrent.exists()
assert Path(f'{corrupted_torrent}.corrupt').exists()


@patch.object(TorrentDef, 'get_metainfo', Mock(return_value=None))
Expand Down
54 changes: 35 additions & 19 deletions src/tribler/core/components/watch_folder/watch_folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,31 +51,47 @@ def check_watch_folder(self):
return

directory = self.settings.get_path_as_absolute('directory', self.state_dir)
self._logger.debug(f'Watch dir: {directory}')
if not directory.is_dir():
self._logger.debug(f'Cancelled. Is not directory: {directory}.')
return

# Make sure that we pass a str to os.walk
watch_dir = str(directory)
self._logger.debug(f'Watch dir: {watch_dir}')

for root, _, files in os.walk(watch_dir):
torrent_files = (file for file in files if file.endswith(".torrent"))
for name in torrent_files:
for root, _, files in os.walk(str(directory)):
for name in files:
path = Path(root) / name
self._logger.info(f'Torrent file found: {path}')
self.process_torrent_file(path)

self._logger.debug('Checking watch folder completed.')

def process_torrent_file(self, path: Path):
if not path.name.endswith(".torrent"):
return
self._logger.info(f'Torrent file found: {path}')
exception = None
try:
self.start_download(path)
except Exception as e:
self._logger.error(f'{e.__class__.__name__}: {e}')
exception = e

tdef = TorrentDef.load(path)
if not tdef.get_metainfo():
self._logger.warning(f'Missed metainfo: {path}')
continue
if exception:
self._logger.info(f'Corrupted: {path}')
try:
path.replace(f'{path}.corrupt')
except OSError as e:
self._logger.warning(f'{e.__class__.__name__}: {e}')

infohash = tdef.get_infohash()
def start_download(self, path: Path):

if not self.download_manager.download_exists(infohash):
self._logger.info("Starting download from torrent file %s", name)
download_config = DownloadConfig.from_defaults(self.download_manager.download_defaults)
download_config.state_dir = self.state_dir
self.download_manager.start_download(torrent_file=path, config=download_config)
tdef = TorrentDef.load(path)
if not tdef.get_metainfo():
self._logger.warning(f'Missed metainfo: {path}')
return

self._logger.debug('Checking watch folder completed.')
infohash = tdef.get_infohash()

if not self.download_manager.download_exists(infohash):
self._logger.info("Starting download from torrent file %s", path.name)
download_config = DownloadConfig.from_defaults(self.download_manager.download_defaults)
download_config.state_dir = self.state_dir
self.download_manager.start_download(torrent_file=path, config=download_config)

0 comments on commit a5c1408

Please sign in to comment.