+
+// ---------------------------------------------------------------------------
+
+function safeWebtorrentDestroy (
+ webtorrent: Instance,
+ torrentId: string,
+ downloadedFile?: { directoryPath: string, filepath: string },
+ torrentName?: string
+) {
+ return new Promise<void>(res => {
+ webtorrent.destroy(err => {
+ // Delete torrent file
+ if (torrentName) {
+ logger.debug('Removing %s torrent after webtorrent download.', torrentId)
+ remove(torrentId)
+ .catch(err => logger.error('Cannot remove torrent %s in webtorrent download.', torrentId, { err }))
+ }
+
+ // Delete downloaded file
+ 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
+
+ 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 }))
+}
+
+function buildAnnounceList () {
+ return [
+ [ WEBSERVER.WS + '://' + WEBSERVER.HOSTNAME + ':' + WEBSERVER.PORT + '/tracker/socket' ],
+ [ WEBSERVER.URL + '/tracker/announce' ]
+ ]
+}
+
+function buildUrlList (video: MVideo, videoFile: MVideoFile) {
+ return [ videoFile.getFileUrl(video) ]
+}
+
+function buildInfoName (video: MVideo, videoFile: MVideoFile) {
+ return `${video.name} ${videoFile.resolution}p${videoFile.extname}`
+}