Skip to content

Commit

Permalink
Add torrent 'error' handler
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolay-borzov committed Aug 31, 2019
1 parent e34a2dd commit a3c1e06
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 47 deletions.
79 changes: 40 additions & 39 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
18 changes: 10 additions & 8 deletions src/parse-torrent.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down

0 comments on commit a3c1e06

Please sign in to comment.