aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/webtorrent.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/helpers/webtorrent.ts')
-rw-r--r--server/helpers/webtorrent.ts29
1 files changed, 21 insertions, 8 deletions
diff --git a/server/helpers/webtorrent.ts b/server/helpers/webtorrent.ts
index f6469ef32..b4629a094 100644
--- a/server/helpers/webtorrent.ts
+++ b/server/helpers/webtorrent.ts
@@ -3,7 +3,7 @@ import { generateVideoTmpPath } from './utils'
3import * as WebTorrent from 'webtorrent' 3import * as WebTorrent from 'webtorrent'
4import { createWriteStream, ensureDir, remove } from 'fs-extra' 4import { createWriteStream, ensureDir, remove } from 'fs-extra'
5import { CONFIG } from '../initializers' 5import { CONFIG } from '../initializers'
6import { join } from 'path' 6import { dirname, join } from 'path'
7 7
8async function downloadWebTorrentVideo (target: { magnetUri: string, torrentName?: string }, timeout?: number) { 8async function downloadWebTorrentVideo (target: { magnetUri: string, torrentName?: string }, timeout?: number) {
9 const id = target.magnetUri || target.torrentName 9 const id = target.magnetUri || target.torrentName
@@ -26,7 +26,7 @@ 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, join(directoryPath, file.name), target.torrentName) 29 return safeWebtorrentDestroy(webtorrent, torrentId, { directoryPath, filepath: file.path }, target.torrentName)
30 .then(() => rej(new Error('Cannot import torrent ' + torrentId + ': there are multiple files in it'))) 30 .then(() => rej(new Error('Cannot import torrent ' + torrentId + ': there are multiple files in it')))
31 } 31 }
32 32
@@ -37,7 +37,7 @@ async function downloadWebTorrentVideo (target: { magnetUri: string, torrentName
37 writeStream.on('finish', () => { 37 writeStream.on('finish', () => {
38 if (timer) clearTimeout(timer) 38 if (timer) clearTimeout(timer)
39 39
40 return safeWebtorrentDestroy(webtorrent, torrentId, join(directoryPath, file.name), target.torrentName) 40 return safeWebtorrentDestroy(webtorrent, torrentId, { directoryPath, filepath: file.path }, target.torrentName)
41 .then(() => res(path)) 41 .then(() => res(path))
42 }) 42 })
43 43
@@ -48,7 +48,7 @@ async function downloadWebTorrentVideo (target: { magnetUri: string, torrentName
48 48
49 if (timeout) { 49 if (timeout) {
50 timer = setTimeout(async () => { 50 timer = setTimeout(async () => {
51 return safeWebtorrentDestroy(webtorrent, torrentId, file ? join(directoryPath, file.name) : undefined, target.torrentName) 51 return safeWebtorrentDestroy(webtorrent, torrentId, file ? { directoryPath, filepath: file.path } : undefined, target.torrentName)
52 .then(() => rej(new Error('Webtorrent download timeout.'))) 52 .then(() => rej(new Error('Webtorrent download timeout.')))
53 }, timeout) 53 }, timeout)
54 } 54 }
@@ -63,19 +63,32 @@ export {
63 63
64// --------------------------------------------------------------------------- 64// ---------------------------------------------------------------------------
65 65
66function safeWebtorrentDestroy (webtorrent: WebTorrent.Instance, torrentId: string, filepath?: string, torrentName?: string) { 66function safeWebtorrentDestroy (
67 webtorrent: WebTorrent.Instance,
68 torrentId: string,
69 downloadedFile?: { directoryPath: string, filepath: string },
70 torrentName?: string
71) {
67 return new Promise(res => { 72 return new Promise(res => {
68 webtorrent.destroy(err => { 73 webtorrent.destroy(err => {
69 // Delete torrent file 74 // Delete torrent file
70 if (torrentName) { 75 if (torrentName) {
76 logger.debug('Removing %s torrent after webtorrent download.', torrentId)
71 remove(torrentId) 77 remove(torrentId)
72 .catch(err => logger.error('Cannot remove torrent %s in webtorrent download.', torrentId, { err })) 78 .catch(err => logger.error('Cannot remove torrent %s in webtorrent download.', torrentId, { err }))
73 } 79 }
74 80
75 // Delete downloaded file 81 // Delete downloaded file
76 if (filepath) { 82 if (downloadedFile) {
77 remove(filepath) 83 // We want to delete the base directory
78 .catch(err => logger.error('Cannot remove torrent file %s in webtorrent download.', filepath, { err })) 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 }))
79 } 92 }
80 93
81 if (err) { 94 if (err) {