]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/webtorrent.ts
Correctly fix octet stream fallback for video ext
[github/Chocobozzz/PeerTube.git] / server / helpers / webtorrent.ts
1 import { logger } from './logger'
2 import { generateVideoImportTmpPath } from './utils'
3 import * as WebTorrent from 'webtorrent'
4 import { createWriteStream, ensureDir, remove, writeFile } from 'fs-extra'
5 import { CONFIG } from '../initializers/config'
6 import { dirname, join } from 'path'
7 import * as createTorrent from 'create-torrent'
8 import { promisify2 } from './core-utils'
9 import { MVideo } from '@server/typings/models/video/video'
10 import { MVideoFile, MVideoFileRedundanciesOpt } from '@server/typings/models/video/video-file'
11 import { isStreamingPlaylist, MStreamingPlaylistVideo } from '@server/typings/models/video/video-streaming-playlist'
12 import { STATIC_PATHS, WEBSERVER } from '@server/initializers/constants'
13 import * as parseTorrent from 'parse-torrent'
14 import * as magnetUtil from 'magnet-uri'
15 import { isArray } from '@server/helpers/custom-validators/misc'
16 import { extractVideo } from '@server/lib/videos'
17 import { getTorrentFileName, getVideoFilename, getVideoFilePath } from '@server/lib/video-paths'
18
19 const createTorrentPromise = promisify2<string, any, any>(createTorrent)
20
21 async function downloadWebTorrentVideo (target: { magnetUri: string, torrentName?: string }, timeout: number) {
22 const id = target.magnetUri || target.torrentName
23 let timer
24
25 const path = generateVideoImportTmpPath(id)
26 logger.info('Importing torrent video %s', id)
27
28 const directoryPath = join(CONFIG.STORAGE.TMP_DIR, 'webtorrent')
29 await ensureDir(directoryPath)
30
31 return new Promise<string>((res, rej) => {
32 const webtorrent = new WebTorrent()
33 let file: WebTorrent.TorrentFile
34
35 const torrentId = target.magnetUri || join(CONFIG.STORAGE.TORRENTS_DIR, target.torrentName)
36
37 const options = { path: directoryPath }
38 const torrent = webtorrent.add(torrentId, options, torrent => {
39 if (torrent.files.length !== 1) {
40 if (timer) clearTimeout(timer)
41
42 for (let file of torrent.files) {
43 deleteDownloadedFile({ directoryPath, filepath: file.path })
44 }
45
46 return safeWebtorrentDestroy(webtorrent, torrentId, undefined, target.torrentName)
47 .then(() => rej(new Error('Cannot import torrent ' + torrentId + ': there are multiple files in it')))
48 }
49
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
57 return safeWebtorrentDestroy(webtorrent, torrentId, { directoryPath, filepath: file.path }, target.torrentName)
58 .then(() => res(path))
59 })
60
61 file.createReadStream().pipe(writeStream)
62 })
63
64 torrent.on('error', err => rej(err))
65
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)
70 })
71 }
72
73 async function createTorrentAndSetInfoHash (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, videoFile: MVideoFile) {
74 const video = extractVideo(videoOrPlaylist)
75 const { baseUrlHttp } = video.getBaseUrls()
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 ],
85 urlList: [ videoOrPlaylist.getVideoFileUrl(videoFile, baseUrlHttp) ]
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
99 function 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 }
126
127 // ---------------------------------------------------------------------------
128
129 export {
130 createTorrentPromise,
131 createTorrentAndSetInfoHash,
132 generateMagnetUri,
133 downloadWebTorrentVideo
134 }
135
136 // ---------------------------------------------------------------------------
137
138 function safeWebtorrentDestroy (
139 webtorrent: WebTorrent.Instance,
140 torrentId: string,
141 downloadedFile?: { directoryPath: string, filepath: string },
142 torrentName?: string
143 ) {
144 return new Promise(res => {
145 webtorrent.destroy(err => {
146 // Delete torrent file
147 if (torrentName) {
148 logger.debug('Removing %s torrent after webtorrent download.', torrentId)
149 remove(torrentId)
150 .catch(err => logger.error('Cannot remove torrent %s in webtorrent download.', torrentId, { err }))
151 }
152
153 // Delete downloaded file
154 if (downloadedFile) deleteDownloadedFile(downloadedFile)
155
156 if (err) logger.warn('Cannot destroy webtorrent in timeout.', { err })
157
158 return res()
159 })
160 })
161 }
162
163 function 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 }