From a3c1e063231686b6f44d56925b9d3067cf16197e Mon Sep 17 00:00:00 2001 From: nikolay-borzov Date: Sat, 31 Aug 2019 18:43:52 +0400 Subject: [PATCH] Add torrent 'error' handler --- src/index.js | 79 ++++++++++++++++++++++---------------------- src/parse-torrent.js | 18 +++++----- 2 files changed, 50 insertions(+), 47 deletions(-) diff --git a/src/index.js b/src/index.js index 4890e6e..91ffb03 100644 --- a/src/index.js +++ b/src/index.js @@ -50,55 +50,56 @@ const directoryPath = path.resolve(argv.dir) const verbose = argv.verbose console.log(logColor.info('Parsing torrent file...')) -Promise.all([ - parseTorrent(torrentId), - recursive(directoryPath, IGNORE_GLOBS) -]).then(async ([parseResult, dirFiles]) => { - if (!parseResult) { - return - } +Promise.all([parseTorrent(torrentId), recursive(directoryPath, IGNORE_GLOBS)]) + .then(async ([parseResult, dirFiles]) => { + if (!parseResult) { + return + } - const { name, files } = parseResult + const { name, files } = parseResult - console.log(`Parsed ${chalk.bold(name)}.`, os.EOL) + console.log(`Parsed ${chalk.bold(name)}.`, os.EOL) - const rootDir = `${name}${path.sep}` - const torrentFiles = files.map(file => - path.join(directoryPath, file.replace(rootDir, '')) - ) + const rootDir = `${name}${path.sep}` + const torrentFiles = files.map(file => + path.join(directoryPath, file.replace(rootDir, '')) + ) - const outdated = dirFiles.reduce((result, filename) => { - if (torrentFiles.indexOf(filename) === -1) { - result.push(filename) - } + const outdated = dirFiles.reduce((result, filename) => { + if (torrentFiles.indexOf(filename) === -1) { + result.push(filename) + } - return result - }, []) + return result + }, []) - if (outdated.length) { - console.log(`Found ${chalk.bold(outdated.length)} extra file(s).`) + if (outdated.length) { + console.log(`Found ${chalk.bold(outdated.length)} extra file(s).`) - const dirRoot = `${directoryPath}${path.sep}` - const filenames = outdated.map(filename => filename.replace(dirRoot, '')) - outputFilenames(filenames, verbose) + const dirRoot = `${directoryPath}${path.sep}` + const filenames = outdated.map(filename => filename.replace(dirRoot, '')) + outputFilenames(filenames, verbose) - const deleteConfirm = new Confirm({ - name: 'delete', - message: 'Delete extra files?', - initial: true - }) + const deleteConfirm = new Confirm({ + name: 'delete', + message: 'Delete extra files?', + initial: true + }) - const deleteFilesAnswer = await deleteConfirm.run() + const deleteFilesAnswer = await deleteConfirm.run() - if (deleteFilesAnswer) { - console.log() - console.log(logColor.info('Deleting extra files...')) + if (deleteFilesAnswer) { + console.log() + console.log(logColor.info('Deleting extra files...')) - await deleteFilesAndEmptyFolders(outdated, directoryPath) + await deleteFilesAndEmptyFolders(outdated, directoryPath) - console.log('Files deleted.') + console.log('Files deleted.') + } + } else { + console.log('No extra files found!') } - } else { - console.log('No extra files found!') - } -}) + }) + .catch(error => { + console.log(logColor.error('Error ocurred'), error) + }) diff --git a/src/parse-torrent.js b/src/parse-torrent.js index 58f0823..c36ea97 100644 --- a/src/parse-torrent.js +++ b/src/parse-torrent.js @@ -1,24 +1,26 @@ const WebTorrent = require('webtorrent') const memoryChunkStore = require('memory-chunk-store') -const logColor = require('./log-color') - async function getTorrentMetadata(torrentId) { - return new Promise(resolve => { - parseTorrent(torrentId, resolve) - }).catch(error => { - console.log(logColor.error('Unable to parse torrent'), error) + return new Promise((resolve, reject) => { + parseTorrent(torrentId, resolve, reject) }) } -function parseTorrent(torrentId, onDone) { +function parseTorrent(torrentId, onDone, onError) { const client = new WebTorrent() - // Use memory-chunk-store to avoid creating directories inside tmp/webtorrent(https://github.com/webtorrent/webtorrent/issues/1562) + // Use memory-chunk-store to avoid creating directories inside tmp/webtorrent (https://github.com/webtorrent/webtorrent/issues/1562) const torrent = client.add(torrentId, { store: memoryChunkStore }) + torrent.on('error', error => { + onError(error) + + client.destroy() + }) + torrent.on('metadata', () => { onDone({ name: torrent.name,