]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/webtorrent.ts
Upgrade server dep
[github/Chocobozzz/PeerTube.git] / server / helpers / webtorrent.ts
CommitLineData
ce33919c 1import { logger } from './logger'
6040f87d 2import { generateVideoImportTmpPath } from './utils'
ce33919c 3import * as WebTorrent from 'webtorrent'
d7a25329 4import { createWriteStream, ensureDir, remove, writeFile } from 'fs-extra'
6dd9de95 5import { CONFIG } from '../initializers/config'
d0b52b52 6import { dirname, join } from 'path'
30ff39e7
C
7import * as createTorrent from 'create-torrent'
8import { promisify2 } from './core-utils'
d7a25329
C
9import { MVideo } from '@server/typings/models/video/video'
10import { MVideoFile, MVideoFileRedundanciesOpt } from '@server/typings/models/video/video-file'
11import { isStreamingPlaylist, MStreamingPlaylistVideo } from '@server/typings/models/video/video-streaming-playlist'
b764380a 12import { WEBSERVER } from '@server/initializers/constants'
d7a25329
C
13import * as parseTorrent from 'parse-torrent'
14import * as magnetUtil from 'magnet-uri'
15import { isArray } from '@server/helpers/custom-validators/misc'
16import { extractVideo } from '@server/lib/videos'
b764380a 17import { getTorrentFileName, getVideoFilePath } from '@server/lib/video-paths'
d7a25329
C
18
19const createTorrentPromise = promisify2<string, any, any>(createTorrent)
ce33919c 20
cf9166cf 21async function downloadWebTorrentVideo (target: { magnetUri: string, torrentName?: string }, timeout: number) {
990b6a0b 22 const id = target.magnetUri || target.torrentName
c48e82b5 23 let timer
ce33919c 24
6040f87d 25 const path = generateVideoImportTmpPath(id)
990b6a0b 26 logger.info('Importing torrent video %s', id)
ce33919c 27
6040f87d 28 const directoryPath = join(CONFIG.STORAGE.TMP_DIR, 'webtorrent')
a71de50b
C
29 await ensureDir(directoryPath)
30
ce33919c
C
31 return new Promise<string>((res, rej) => {
32 const webtorrent = new WebTorrent()
c48e82b5 33 let file: WebTorrent.TorrentFile
ce33919c 34
990b6a0b 35 const torrentId = target.magnetUri || join(CONFIG.STORAGE.TORRENTS_DIR, target.torrentName)
541006e3 36
a71de50b 37 const options = { path: directoryPath }
541006e3 38 const torrent = webtorrent.add(torrentId, options, torrent => {
c48e82b5
C
39 if (torrent.files.length !== 1) {
40 if (timer) clearTimeout(timer)
ce33919c 41
e37c85e9
C
42 for (let file of torrent.files) {
43 deleteDownloadedFile({ directoryPath, filepath: file.path })
44 }
45
46 return safeWebtorrentDestroy(webtorrent, torrentId, undefined, target.torrentName)
dae4a1c0 47 .then(() => rej(new Error('Cannot import torrent ' + torrentId + ': there are multiple files in it')))
c48e82b5
C
48 }
49
a71de50b
C
50 file = torrent.files[ 0 ]
51
52 // FIXME: avoid creating another stream when https://github.com/webtorrent/webtorrent/issues/1517 is fixed
53 const writeStream = createWriteStream(path)
54 writeStream.on('finish', () => {
55 if (timer) clearTimeout(timer)
56
d0b52b52 57 return safeWebtorrentDestroy(webtorrent, torrentId, { directoryPath, filepath: file.path }, target.torrentName)
a71de50b 58 .then(() => res(path))
69fa54a0 59 })
a71de50b
C
60
61 file.createReadStream().pipe(writeStream)
3e17515e 62 })
ce33919c
C
63
64 torrent.on('error', err => rej(err))
c48e82b5 65
cf9166cf
C
66 timer = setTimeout(async () => {
67 return safeWebtorrentDestroy(webtorrent, torrentId, file ? { directoryPath, filepath: file.path } : undefined, target.torrentName)
68 .then(() => rej(new Error('Webtorrent download timeout.')))
69 }, timeout)
ce33919c
C
70 })
71}
72
d7a25329
C
73async function createTorrentAndSetInfoHash (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, videoFile: MVideoFile) {
74 const video = extractVideo(videoOrPlaylist)
d441f2ed 75 const { baseUrlHttp } = video.getBaseUrls()
d7a25329
C
76
77 const options = {
78 // Keep the extname, it's used by the client to stream the file inside a web browser
79 name: `${video.name} ${videoFile.resolution}p${videoFile.extname}`,
80 createdBy: 'PeerTube',
81 announceList: [
82 [ WEBSERVER.WS + '://' + WEBSERVER.HOSTNAME + ':' + WEBSERVER.PORT + '/tracker/socket' ],
83 [ WEBSERVER.URL + '/tracker/announce' ]
84 ],
d441f2ed 85 urlList: [ videoOrPlaylist.getVideoFileUrl(videoFile, baseUrlHttp) ]
d7a25329
C
86 }
87
88 const torrent = await createTorrentPromise(getVideoFilePath(videoOrPlaylist, videoFile), options)
89
90 const filePath = join(CONFIG.STORAGE.TORRENTS_DIR, getTorrentFileName(videoOrPlaylist, videoFile))
91 logger.info('Creating torrent %s.', filePath)
92
93 await writeFile(filePath, torrent)
94
95 const parsedTorrent = parseTorrent(torrent)
96 videoFile.infoHash = parsedTorrent.infoHash
97}
98
99function generateMagnetUri (
100 videoOrPlaylist: MVideo | MStreamingPlaylistVideo,
101 videoFile: MVideoFileRedundanciesOpt,
102 baseUrlHttp: string,
103 baseUrlWs: string
104) {
105 const video = isStreamingPlaylist(videoOrPlaylist)
106 ? videoOrPlaylist.Video
107 : videoOrPlaylist
108
109 const xs = videoOrPlaylist.getTorrentUrl(videoFile, baseUrlHttp)
110 const announce = videoOrPlaylist.getTrackerUrls(baseUrlHttp, baseUrlWs)
111 let urlList = [ videoOrPlaylist.getVideoFileUrl(videoFile, baseUrlHttp) ]
112
113 const redundancies = videoFile.RedundancyVideos
114 if (isArray(redundancies)) urlList = urlList.concat(redundancies.map(r => r.fileUrl))
115
116 const magnetHash = {
117 xs,
118 announce,
119 urlList,
120 infoHash: videoFile.infoHash,
121 name: video.name
122 }
123
124 return magnetUtil.encode(magnetHash)
125}
30ff39e7 126
ce33919c
C
127// ---------------------------------------------------------------------------
128
129export {
d441f2ed 130 createTorrentPromise,
d7a25329
C
131 createTorrentAndSetInfoHash,
132 generateMagnetUri,
ce33919c
C
133 downloadWebTorrentVideo
134}
c48e82b5
C
135
136// ---------------------------------------------------------------------------
137
d0b52b52
C
138function safeWebtorrentDestroy (
139 webtorrent: WebTorrent.Instance,
140 torrentId: string,
141 downloadedFile?: { directoryPath: string, filepath: string },
142 torrentName?: string
143) {
c48e82b5
C
144 return new Promise(res => {
145 webtorrent.destroy(err => {
146 // Delete torrent file
147 if (torrentName) {
d0b52b52 148 logger.debug('Removing %s torrent after webtorrent download.', torrentId)
c48e82b5
C
149 remove(torrentId)
150 .catch(err => logger.error('Cannot remove torrent %s in webtorrent download.', torrentId, { err }))
151 }
152
153 // Delete downloaded file
e37c85e9 154 if (downloadedFile) deleteDownloadedFile(downloadedFile)
c48e82b5 155
e37c85e9 156 if (err) logger.warn('Cannot destroy webtorrent in timeout.', { err })
c48e82b5
C
157
158 return res()
159 })
160 })
161}
e37c85e9
C
162
163function deleteDownloadedFile (downloadedFile: { directoryPath: string, filepath: string }) {
164 // We want to delete the base directory
165 let pathToDelete = dirname(downloadedFile.filepath)
166 if (pathToDelete === '.') pathToDelete = downloadedFile.filepath
167
168 const toRemovePath = join(downloadedFile.directoryPath, pathToDelete)
169
170 logger.debug('Removing %s after webtorrent download.', toRemovePath)
171 remove(toRemovePath)
172 .catch(err => logger.error('Cannot remove torrent file %s in webtorrent download.', toRemovePath, { err }))
173}