+
+// ---------------------------------------------------------------------------
+
+function safeWebtorrentDestroy (webtorrent: WebTorrent.Instance, torrentId: string, filename?: string, torrentName?: string) {
+ return new Promise(res => {
+ webtorrent.destroy(err => {
+ // Delete torrent file
+ if (torrentName) {
+ remove(torrentId)
+ .catch(err => logger.error('Cannot remove torrent %s in webtorrent download.', torrentId, { err }))
+ }
+
+ // Delete downloaded file
+ if (filename) {
+ remove(join(CONFIG.STORAGE.VIDEOS_DIR, filename))
+ .catch(err => logger.error('Cannot remove torrent file %s in webtorrent download.', filename, { err }))
+ }
+
+ if (err) {
+ logger.warn('Cannot destroy webtorrent in timeout.', { err })
+ }
+
+ return res()
+ })
+ })
+}