]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/webtorrent.ts
Fix advanced options in upload form
[github/Chocobozzz/PeerTube.git] / server / helpers / webtorrent.ts
CommitLineData
ce33919c 1import { logger } from './logger'
a71de50b 2import { generateVideoTmpPath } from './utils'
ce33919c 3import * as WebTorrent from 'webtorrent'
a71de50b 4import { createWriteStream, ensureDir, remove } from 'fs-extra'
990b6a0b 5import { CONFIG } from '../initializers'
d0b52b52 6import { dirname, join } from 'path'
ce33919c 7
a71de50b 8async function downloadWebTorrentVideo (target: { magnetUri: string, torrentName?: string }, timeout?: number) {
990b6a0b 9 const id = target.magnetUri || target.torrentName
c48e82b5 10 let timer
ce33919c 11
a71de50b 12 const path = generateVideoTmpPath(id)
990b6a0b 13 logger.info('Importing torrent video %s', id)
ce33919c 14
a71de50b
C
15 const directoryPath = join(CONFIG.STORAGE.VIDEOS_DIR, 'import')
16 await ensureDir(directoryPath)
17
ce33919c
C
18 return new Promise<string>((res, rej) => {
19 const webtorrent = new WebTorrent()
c48e82b5 20 let file: WebTorrent.TorrentFile
ce33919c 21
990b6a0b 22 const torrentId = target.magnetUri || join(CONFIG.STORAGE.TORRENTS_DIR, target.torrentName)
541006e3 23
a71de50b 24 const options = { path: directoryPath }
541006e3 25 const torrent = webtorrent.add(torrentId, options, torrent => {
c48e82b5
C
26 if (torrent.files.length !== 1) {
27 if (timer) clearTimeout(timer)
ce33919c 28
d0b52b52 29 return safeWebtorrentDestroy(webtorrent, torrentId, { directoryPath, filepath: file.path }, target.torrentName)
dae4a1c0 30 .then(() => rej(new Error('Cannot import torrent ' + torrentId + ': there are multiple files in it')))
c48e82b5
C
31 }
32
a71de50b
C
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
d0b52b52 40 return safeWebtorrentDestroy(webtorrent, torrentId, { directoryPath, filepath: file.path }, target.torrentName)
a71de50b 41 .then(() => res(path))
69fa54a0 42 })
a71de50b
C
43
44 file.createReadStream().pipe(writeStream)
3e17515e 45 })
ce33919c
C
46
47 torrent.on('error', err => rej(err))
c48e82b5
C
48
49 if (timeout) {
50 timer = setTimeout(async () => {
d0b52b52 51 return safeWebtorrentDestroy(webtorrent, torrentId, file ? { directoryPath, filepath: file.path } : undefined, target.torrentName)
c48e82b5
C
52 .then(() => rej(new Error('Webtorrent download timeout.')))
53 }, timeout)
54 }
ce33919c
C
55 })
56}
57
58// ---------------------------------------------------------------------------
59
60export {
61 downloadWebTorrentVideo
62}
c48e82b5
C
63
64// ---------------------------------------------------------------------------
65
d0b52b52
C
66function safeWebtorrentDestroy (
67 webtorrent: WebTorrent.Instance,
68 torrentId: string,
69 downloadedFile?: { directoryPath: string, filepath: string },
70 torrentName?: string
71) {
c48e82b5
C
72 return new Promise(res => {
73 webtorrent.destroy(err => {
74 // Delete torrent file
75 if (torrentName) {
d0b52b52 76 logger.debug('Removing %s torrent after webtorrent download.', torrentId)
c48e82b5
C
77 remove(torrentId)
78 .catch(err => logger.error('Cannot remove torrent %s in webtorrent download.', torrentId, { err }))
79 }
80
81 // Delete downloaded file
d0b52b52
C
82 if (downloadedFile) {
83 // We want to delete the base directory
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 }))
c48e82b5
C
92 }
93
94 if (err) {
95 logger.warn('Cannot destroy webtorrent in timeout.', { err })
96 }
97
98 return res()
99 })
100 })
101}