aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/webtorrent.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-08-07 17:18:35 +0200
committerChocobozzz <me@florianbigard.com>2018-08-08 09:30:31 +0200
commit541006e355c927a866d58cced016f48e139670d5 (patch)
tree78a658dfc8cb1e552ee0204c824358d988c49297 /server/helpers/webtorrent.ts
parent3e17515e2996b79e23f569c296051a91af3fcbe4 (diff)
downloadPeerTube-541006e355c927a866d58cced016f48e139670d5.tar.gz
PeerTube-541006e355c927a866d58cced016f48e139670d5.tar.zst
PeerTube-541006e355c927a866d58cced016f48e139670d5.zip
Correct webtorrent download cleanup
Diffstat (limited to 'server/helpers/webtorrent.ts')
-rw-r--r--server/helpers/webtorrent.ts21
1 files changed, 19 insertions, 2 deletions
diff --git a/server/helpers/webtorrent.ts b/server/helpers/webtorrent.ts
index 121cd0b41..6f2adb3cb 100644
--- a/server/helpers/webtorrent.ts
+++ b/server/helpers/webtorrent.ts
@@ -4,6 +4,7 @@ import * as WebTorrent from 'webtorrent'
4import { createWriteStream } from 'fs' 4import { createWriteStream } from 'fs'
5import { CONFIG } from '../initializers' 5import { CONFIG } from '../initializers'
6import { join } from 'path' 6import { join } from 'path'
7import { unlinkPromise } from './core-utils'
7 8
8function downloadWebTorrentVideo (target: { magnetUri: string, torrentName: string }) { 9function downloadWebTorrentVideo (target: { magnetUri: string, torrentName: string }) {
9 const id = target.magnetUri || target.torrentName 10 const id = target.magnetUri || target.torrentName
@@ -15,13 +16,29 @@ function downloadWebTorrentVideo (target: { magnetUri: string, torrentName: stri
15 const webtorrent = new WebTorrent() 16 const webtorrent = new WebTorrent()
16 17
17 const torrentId = target.magnetUri || join(CONFIG.STORAGE.TORRENTS_DIR, target.torrentName) 18 const torrentId = target.magnetUri || join(CONFIG.STORAGE.TORRENTS_DIR, target.torrentName)
18 const torrent = webtorrent.add(torrentId, torrent => { 19
20 const options = { path: CONFIG.STORAGE.VIDEOS_DIR }
21 const torrent = webtorrent.add(torrentId, options, torrent => {
19 if (torrent.files.length !== 1) return rej(new Error('The number of files is not equal to 1 for ' + torrentId)) 22 if (torrent.files.length !== 1) return rej(new Error('The number of files is not equal to 1 for ' + torrentId))
20 23
21 const file = torrent.files[ 0 ] 24 const file = torrent.files[ 0 ]
22 25
23 const writeStream = createWriteStream(path) 26 const writeStream = createWriteStream(path)
24 writeStream.on('finish', () => res(path)) 27 writeStream.on('finish', () => {
28 webtorrent.destroy(async err => {
29 if (err) return rej(err)
30
31 if (target.torrentName) {
32 unlinkPromise(torrentId)
33 .catch(err => logger.error('Cannot remove torrent %s in webtorrent download.', torrentId, { err }))
34 }
35
36 unlinkPromise(join(CONFIG.STORAGE.VIDEOS_DIR, file.name))
37 .catch(err => logger.error('Cannot remove torrent file %s in webtorrent download.', file.name, { err }))
38
39 res(path)
40 })
41 })
25 42
26 file.createReadStream().pipe(writeStream) 43 file.createReadStream().pipe(writeStream)
27 }) 44 })