Delete each file on failed import

This commit is contained in:
Chocobozzz 2018-10-01 10:52:58 +02:00
parent e95e0463d8
commit e37c85e933
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
1 changed files with 19 additions and 15 deletions

View File

@ -26,7 +26,11 @@ async function downloadWebTorrentVideo (target: { magnetUri: string, torrentName
if (torrent.files.length !== 1) { if (torrent.files.length !== 1) {
if (timer) clearTimeout(timer) if (timer) clearTimeout(timer)
return safeWebtorrentDestroy(webtorrent, torrentId, { directoryPath, filepath: file.path }, target.torrentName) for (let file of torrent.files) {
deleteDownloadedFile({ directoryPath, filepath: file.path })
}
return safeWebtorrentDestroy(webtorrent, torrentId, undefined, target.torrentName)
.then(() => rej(new Error('Cannot import torrent ' + torrentId + ': there are multiple files in it'))) .then(() => rej(new Error('Cannot import torrent ' + torrentId + ': there are multiple files in it')))
} }
@ -79,23 +83,23 @@ function safeWebtorrentDestroy (
} }
// Delete downloaded file // Delete downloaded file
if (downloadedFile) { if (downloadedFile) deleteDownloadedFile(downloadedFile)
// We want to delete the base directory
let pathToDelete = dirname(downloadedFile.filepath)
if (pathToDelete === '.') pathToDelete = downloadedFile.filepath
const toRemovePath = join(downloadedFile.directoryPath, pathToDelete) if (err) logger.warn('Cannot destroy webtorrent in timeout.', { err })
logger.debug('Removing %s after webtorrent download.', toRemovePath)
remove(toRemovePath)
.catch(err => logger.error('Cannot remove torrent file %s in webtorrent download.', toRemovePath, { err }))
}
if (err) {
logger.warn('Cannot destroy webtorrent in timeout.', { err })
}
return res() return res()
}) })
}) })
} }
function deleteDownloadedFile (downloadedFile: { directoryPath: string, filepath: string }) {
// We want to delete the base directory
let pathToDelete = dirname(downloadedFile.filepath)
if (pathToDelete === '.') pathToDelete = downloadedFile.filepath
const toRemovePath = join(downloadedFile.directoryPath, pathToDelete)
logger.debug('Removing %s after webtorrent download.', toRemovePath)
remove(toRemovePath)
.catch(err => logger.error('Cannot remove torrent file %s in webtorrent download.', toRemovePath, { err }))
}