diff options
author | Chocobozzz <me@florianbigard.com> | 2018-08-07 17:18:35 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-08-08 09:30:31 +0200 |
commit | 541006e355c927a866d58cced016f48e139670d5 (patch) | |
tree | 78a658dfc8cb1e552ee0204c824358d988c49297 | |
parent | 3e17515e2996b79e23f569c296051a91af3fcbe4 (diff) | |
download | PeerTube-541006e355c927a866d58cced016f48e139670d5.tar.gz PeerTube-541006e355c927a866d58cced016f48e139670d5.tar.zst PeerTube-541006e355c927a866d58cced016f48e139670d5.zip |
Correct webtorrent download cleanup
-rw-r--r-- | client/src/app/videos/+video-edit/video-add.component.html | 2 | ||||
-rw-r--r-- | server/helpers/webtorrent.ts | 21 | ||||
-rw-r--r-- | server/lib/job-queue/handlers/video-import.ts | 2 |
3 files changed, 21 insertions, 4 deletions
diff --git a/client/src/app/videos/+video-edit/video-add.component.html b/client/src/app/videos/+video-edit/video-add.component.html index 340820180..4c30ddefc 100644 --- a/client/src/app/videos/+video-edit/video-add.component.html +++ b/client/src/app/videos/+video-edit/video-add.component.html | |||
@@ -1,6 +1,6 @@ | |||
1 | <div class="margin-content"> | 1 | <div class="margin-content"> |
2 | <div class="title-page title-page-single"> | 2 | <div class="title-page title-page-single"> |
3 | <ng-container *ngIf="secondStepType === 'import'" i18n>Import {{ videoName }}</ng-container> | 3 | <ng-container *ngIf="secondStepType === 'import-url' || secondStepType === 'import-torrent'" i18n>Import {{ videoName }}</ng-container> |
4 | <ng-container *ngIf="secondStepType === 'upload'" i18n>Upload {{ videoName }}</ng-container> | 4 | <ng-container *ngIf="secondStepType === 'upload'" i18n>Upload {{ videoName }}</ng-container> |
5 | </div> | 5 | </div> |
6 | 6 | ||
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' | |||
4 | import { createWriteStream } from 'fs' | 4 | import { createWriteStream } from 'fs' |
5 | import { CONFIG } from '../initializers' | 5 | import { CONFIG } from '../initializers' |
6 | import { join } from 'path' | 6 | import { join } from 'path' |
7 | import { unlinkPromise } from './core-utils' | ||
7 | 8 | ||
8 | function downloadWebTorrentVideo (target: { magnetUri: string, torrentName: string }) { | 9 | function 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 |