]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/commitdiff
Delete each file on failed import
authorChocobozzz <me@florianbigard.com>
Mon, 1 Oct 2018 08:52:58 +0000 (10:52 +0200)
committerChocobozzz <me@florianbigard.com>
Mon, 1 Oct 2018 08:52:58 +0000 (10:52 +0200)
server/helpers/webtorrent.ts

index b4629a0947221b485cc4c5ca3c558819c21c0f9a..924d630e761752e35e3a9408232ae6c58c9c1ca3 100644 (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,23 +83,23 @@ function safeWebtorrentDestroy (
       }
 
       // Delete downloaded file
-      if (downloadedFile) {
-        // 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 }))
-      }
+      if (downloadedFile) deleteDownloadedFile(downloadedFile)
 
-      if (err) {
-        logger.warn('Cannot destroy webtorrent in timeout.', { err })
-      }
+      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
+
+  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 }))
+}