aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-10-01 10:52:58 +0200
committerChocobozzz <me@florianbigard.com>2018-10-01 10:52:58 +0200
commite37c85e933b320173c271fd1f6471679b759bc39 (patch)
tree3a7d34306ea55606e3b8c5694809cc30d850e529
parente95e0463d8ef0d0a690e716df93c3c78b74737af (diff)
downloadPeerTube-e37c85e933b320173c271fd1f6471679b759bc39.tar.gz
PeerTube-e37c85e933b320173c271fd1f6471679b759bc39.tar.zst
PeerTube-e37c85e933b320173c271fd1f6471679b759bc39.zip
Delete each file on failed import
-rw-r--r--server/helpers/webtorrent.ts34
1 files changed, 19 insertions, 15 deletions
diff --git a/server/helpers/webtorrent.ts b/server/helpers/webtorrent.ts
index b4629a094..924d630e7 100644
--- a/server/helpers/webtorrent.ts
+++ b/server/helpers/webtorrent.ts
@@ -26,7 +26,11 @@ async function downloadWebTorrentVideo (target: { magnetUri: string, torrentName
26 if (torrent.files.length !== 1) { 26 if (torrent.files.length !== 1) {
27 if (timer) clearTimeout(timer) 27 if (timer) clearTimeout(timer)
28 28
29 return safeWebtorrentDestroy(webtorrent, torrentId, { directoryPath, filepath: file.path }, target.torrentName) 29 for (let file of torrent.files) {
30 deleteDownloadedFile({ directoryPath, filepath: file.path })
31 }
32
33 return safeWebtorrentDestroy(webtorrent, torrentId, undefined, target.torrentName)
30 .then(() => rej(new Error('Cannot import torrent ' + torrentId + ': there are multiple files in it'))) 34 .then(() => rej(new Error('Cannot import torrent ' + torrentId + ': there are multiple files in it')))
31 } 35 }
32 36
@@ -79,23 +83,23 @@ function safeWebtorrentDestroy (
79 } 83 }
80 84
81 // Delete downloaded file 85 // Delete downloaded file
82 if (downloadedFile) { 86 if (downloadedFile) deleteDownloadedFile(downloadedFile)
83 // We want to delete the base directory
84 let pathToDelete = dirname(downloadedFile.filepath)
85 if (pathToDelete === '.') pathToDelete = downloadedFile.filepath
86
87 const toRemovePath = join(downloadedFile.directoryPath, pathToDelete)
88
89 logger.debug('Removing %s after webtorrent download.', toRemovePath)
90 remove(toRemovePath)
91 .catch(err => logger.error('Cannot remove torrent file %s in webtorrent download.', toRemovePath, { err }))
92 }
93 87
94 if (err) { 88 if (err) logger.warn('Cannot destroy webtorrent in timeout.', { err })
95 logger.warn('Cannot destroy webtorrent in timeout.', { err })
96 }
97 89
98 return res() 90 return res()
99 }) 91 })
100 }) 92 })
101} 93}
94
95function deleteDownloadedFile (downloadedFile: { directoryPath: string, filepath: string }) {
96 // We want to delete the base directory
97 let pathToDelete = dirname(downloadedFile.filepath)
98 if (pathToDelete === '.') pathToDelete = downloadedFile.filepath
99
100 const toRemovePath = join(downloadedFile.directoryPath, pathToDelete)
101
102 logger.debug('Removing %s after webtorrent download.', toRemovePath)
103 remove(toRemovePath)
104 .catch(err => logger.error('Cannot remove torrent file %s in webtorrent download.', toRemovePath, { err }))
105}