]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/webtorrent.ts
Fix video import of some youtube videos
[github/Chocobozzz/PeerTube.git] / server / helpers / webtorrent.ts
1 import { logger } from './logger'
2 import { generateVideoTmpPath } from './utils'
3 import * as WebTorrent from 'webtorrent'
4 import { createWriteStream, ensureDir, remove } from 'fs-extra'
5 import { CONFIG } from '../initializers'
6 import { join } from 'path'
7
8 async function downloadWebTorrentVideo (target: { magnetUri: string, torrentName?: string }, timeout?: number) {
9 const id = target.magnetUri || target.torrentName
10 let timer
11
12 const path = generateVideoTmpPath(id)
13 logger.info('Importing torrent video %s', id)
14
15 const directoryPath = join(CONFIG.STORAGE.VIDEOS_DIR, 'import')
16 await ensureDir(directoryPath)
17
18 return new Promise<string>((res, rej) => {
19 const webtorrent = new WebTorrent()
20 let file: WebTorrent.TorrentFile
21
22 const torrentId = target.magnetUri || join(CONFIG.STORAGE.TORRENTS_DIR, target.torrentName)
23
24 const options = { path: directoryPath }
25 const torrent = webtorrent.add(torrentId, options, torrent => {
26 if (torrent.files.length !== 1) {
27 if (timer) clearTimeout(timer)
28
29 return safeWebtorrentDestroy(webtorrent, torrentId, join(directoryPath, file.name), target.torrentName)
30 .then(() => rej(new Error('Cannot import torrent ' + torrentId + ': there are multiple files in it')))
31 }
32
33 file = torrent.files[ 0 ]
34
35 // FIXME: avoid creating another stream when https://github.com/webtorrent/webtorrent/issues/1517 is fixed
36 const writeStream = createWriteStream(path)
37 writeStream.on('finish', () => {
38 if (timer) clearTimeout(timer)
39
40 return safeWebtorrentDestroy(webtorrent, torrentId, join(directoryPath, file.name), target.torrentName)
41 .then(() => res(path))
42 })
43
44 file.createReadStream().pipe(writeStream)
45 })
46
47 torrent.on('error', err => rej(err))
48
49 if (timeout) {
50 timer = setTimeout(async () => {
51 return safeWebtorrentDestroy(webtorrent, torrentId, file ? join(directoryPath, file.name) : undefined, target.torrentName)
52 .then(() => rej(new Error('Webtorrent download timeout.')))
53 }, timeout)
54 }
55 })
56 }
57
58 // ---------------------------------------------------------------------------
59
60 export {
61 downloadWebTorrentVideo
62 }
63
64 // ---------------------------------------------------------------------------
65
66 function safeWebtorrentDestroy (webtorrent: WebTorrent.Instance, torrentId: string, filepath?: string, torrentName?: string) {
67 return new Promise(res => {
68 webtorrent.destroy(err => {
69 // Delete torrent file
70 if (torrentName) {
71 remove(torrentId)
72 .catch(err => logger.error('Cannot remove torrent %s in webtorrent download.', torrentId, { err }))
73 }
74
75 // Delete downloaded file
76 if (filepath) {
77 remove(filepath)
78 .catch(err => logger.error('Cannot remove torrent file %s in webtorrent download.', filepath, { err }))
79 }
80
81 if (err) {
82 logger.warn('Cannot destroy webtorrent in timeout.', { err })
83 }
84
85 return res()
86 })
87 })
88 }