aboutsummaryrefslogtreecommitdiffhomepage
path: root/server
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
parent3e17515e2996b79e23f569c296051a91af3fcbe4 (diff)
downloadPeerTube-541006e355c927a866d58cced016f48e139670d5.tar.gz
PeerTube-541006e355c927a866d58cced016f48e139670d5.tar.zst
PeerTube-541006e355c927a866d58cced016f48e139670d5.zip
Correct webtorrent download cleanup
Diffstat (limited to 'server')
-rw-r--r--server/helpers/webtorrent.ts21
-rw-r--r--server/lib/job-queue/handlers/video-import.ts2
2 files changed, 20 insertions, 3 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 })
diff --git a/server/lib/job-queue/handlers/video-import.ts b/server/lib/job-queue/handlers/video-import.ts
index 2d19b82a4..d6984ef92 100644
--- a/server/lib/job-queue/handlers/video-import.ts
+++ b/server/lib/job-queue/handlers/video-import.ts
@@ -190,7 +190,7 @@ async function processFile (downloader: () => Promise<string>, videoImport: Vide
190 videoImport.state = VideoImportState.SUCCESS 190 videoImport.state = VideoImportState.SUCCESS
191 const videoImportUpdated = await videoImport.save({ transaction: t }) 191 const videoImportUpdated = await videoImport.save({ transaction: t })
192 192
193 logger.info('Video %s imported.', videoImport.targetUrl) 193 logger.info('Video %s imported.', video.uuid)
194 194
195 videoImportUpdated.Video = videoUpdated 195 videoImportUpdated.Video = videoUpdated
196 return videoImportUpdated 196 return videoImportUpdated