1
0
Fork 0

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

View file

@ -26,7 +26,11 @@ async function downloadWebTorrentVideo (target: { magnetUri: string, torrentName
if (torrent.files.length !== 1) {
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')))
}
@ -79,7 +83,16 @@ function safeWebtorrentDestroy (
}
// Delete downloaded file
if (downloadedFile) {
if (downloadedFile) deleteDownloadedFile(downloadedFile)
if (err) logger.warn('Cannot destroy webtorrent in timeout.', { err })
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
@ -90,12 +103,3 @@ function safeWebtorrentDestroy (
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()
})
})
}