]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/webtorrent.ts
Remove useless condition
[github/Chocobozzz/PeerTube.git] / server / helpers / webtorrent.ts
CommitLineData
1f6125be 1import * as bencode from 'bencode'
30ff39e7 2import * as createTorrent from 'create-torrent'
1f6125be 3import { createWriteStream, ensureDir, readFile, remove, writeFile } from 'fs-extra'
d7a25329 4import * as magnetUtil from 'magnet-uri'
90a8bd30
C
5import * as parseTorrent from 'parse-torrent'
6import { dirname, join } from 'path'
7import * as WebTorrent from 'webtorrent'
d7a25329 8import { isArray } from '@server/helpers/custom-validators/misc'
90a8bd30 9import { WEBSERVER } from '@server/initializers/constants'
0305db28
JB
10import { generateTorrentFileName } from '@server/lib/paths'
11import { VideoPathManager } from '@server/lib/video-path-manager'
8efc27bf 12import { MVideo } from '@server/types/models/video/video'
90a8bd30
C
13import { MVideoFile, MVideoFileRedundanciesOpt } from '@server/types/models/video/video-file'
14import { MStreamingPlaylistVideo } from '@server/types/models/video/video-streaming-playlist'
15import { CONFIG } from '../initializers/config'
16import { promisify2 } from './core-utils'
17import { logger } from './logger'
18import { generateVideoImportTmpPath } from './utils'
8efc27bf 19import { extractVideo } from './video'
d7a25329
C
20
21const createTorrentPromise = promisify2<string, any, any>(createTorrent)
ce33919c 22
cf9166cf 23async function downloadWebTorrentVideo (target: { magnetUri: string, torrentName?: string }, timeout: number) {
990b6a0b 24 const id = target.magnetUri || target.torrentName
c48e82b5 25 let timer
ce33919c 26
6040f87d 27 const path = generateVideoImportTmpPath(id)
990b6a0b 28 logger.info('Importing torrent video %s', id)
ce33919c 29
6040f87d 30 const directoryPath = join(CONFIG.STORAGE.TMP_DIR, 'webtorrent')
a71de50b
C
31 await ensureDir(directoryPath)
32
ce33919c
C
33 return new Promise<string>((res, rej) => {
34 const webtorrent = new WebTorrent()
c48e82b5 35 let file: WebTorrent.TorrentFile
ce33919c 36
990b6a0b 37 const torrentId = target.magnetUri || join(CONFIG.STORAGE.TORRENTS_DIR, target.torrentName)
541006e3 38
a71de50b 39 const options = { path: directoryPath }
541006e3 40 const torrent = webtorrent.add(torrentId, options, torrent => {
c48e82b5
C
41 if (torrent.files.length !== 1) {
42 if (timer) clearTimeout(timer)
ce33919c 43
a1587156 44 for (const file of torrent.files) {
e37c85e9
C
45 deleteDownloadedFile({ directoryPath, filepath: file.path })
46 }
47
48 return safeWebtorrentDestroy(webtorrent, torrentId, undefined, target.torrentName)
dae4a1c0 49 .then(() => rej(new Error('Cannot import torrent ' + torrentId + ': there are multiple files in it')))
c48e82b5
C
50 }
51
a1587156 52 file = torrent.files[0]
a71de50b
C
53
54 // FIXME: avoid creating another stream when https://github.com/webtorrent/webtorrent/issues/1517 is fixed
55 const writeStream = createWriteStream(path)
56 writeStream.on('finish', () => {
57 if (timer) clearTimeout(timer)
58
a1587156 59 safeWebtorrentDestroy(webtorrent, torrentId, { directoryPath, filepath: file.path }, target.torrentName)
a71de50b 60 .then(() => res(path))
a1587156 61 .catch(err => logger.error('Cannot destroy webtorrent.', { err }))
69fa54a0 62 })
a71de50b
C
63
64 file.createReadStream().pipe(writeStream)
3e17515e 65 })
ce33919c
C
66
67 torrent.on('error', err => rej(err))
c48e82b5 68
a1587156
C
69 timer = setTimeout(() => {
70 const err = new Error('Webtorrent download timeout.')
71
72 safeWebtorrentDestroy(webtorrent, torrentId, file ? { directoryPath, filepath: file.path } : undefined, target.torrentName)
73 .then(() => rej(err))
74 .catch(destroyErr => {
75 logger.error('Cannot destroy webtorrent.', { err: destroyErr })
76 rej(err)
77 })
78
cf9166cf 79 }, timeout)
ce33919c
C
80 })
81}
82
1f6125be 83function createTorrentAndSetInfoHash (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, videoFile: MVideoFile) {
8efc27bf
C
84 const video = extractVideo(videoOrPlaylist)
85
d7a25329
C
86 const options = {
87 // Keep the extname, it's used by the client to stream the file inside a web browser
88 name: `${video.name} ${videoFile.resolution}p${videoFile.extname}`,
89 createdBy: 'PeerTube',
1f6125be
C
90 announceList: buildAnnounceList(),
91 urlList: buildUrlList(video, videoFile)
d7a25329
C
92 }
93
0305db28 94 return VideoPathManager.Instance.makeAvailableVideoFile(videoOrPlaylist, videoFile, async videoPath => {
1f6125be 95 const torrentContent = await createTorrentPromise(videoPath, options)
d7a25329 96
0305db28
JB
97 const torrentFilename = generateTorrentFileName(videoOrPlaylist, videoFile.resolution)
98 const torrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, torrentFilename)
99 logger.info('Creating torrent %s.', torrentPath)
d7a25329 100
1f6125be 101 await writeFile(torrentPath, torrentContent)
d7a25329 102
0305db28
JB
103 // Remove old torrent file if it existed
104 if (videoFile.hasTorrent()) {
105 await remove(join(CONFIG.STORAGE.TORRENTS_DIR, videoFile.torrentFilename))
106 }
764b1a14 107
1f6125be 108 const parsedTorrent = parseTorrent(torrentContent)
0305db28
JB
109 videoFile.infoHash = parsedTorrent.infoHash
110 videoFile.torrentFilename = torrentFilename
111 })
d7a25329
C
112}
113
1f6125be
C
114async function updateTorrentUrls (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, videoFile: MVideoFile) {
115 const video = extractVideo(videoOrPlaylist)
116
117 const oldTorrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, videoFile.torrentFilename)
118
119 const torrentContent = await readFile(oldTorrentPath)
120 const decoded = bencode.decode(torrentContent)
121
122 decoded['announce-list'] = buildAnnounceList()
123 decoded.announce = decoded['announce-list'][0][0]
124
125 decoded['url-list'] = buildUrlList(video, videoFile)
126
127 const newTorrentFilename = generateTorrentFileName(videoOrPlaylist, videoFile.resolution)
128 const newTorrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, newTorrentFilename)
129
f645af43 130 logger.info('Updating torrent URLs %s -> %s.', oldTorrentPath, newTorrentPath)
1f6125be
C
131
132 await writeFile(newTorrentPath, bencode.encode(decoded))
f645af43 133 await remove(join(CONFIG.STORAGE.TORRENTS_DIR, videoFile.torrentFilename))
1f6125be
C
134
135 videoFile.torrentFilename = newTorrentFilename
136}
137
d7a25329 138function generateMagnetUri (
8efc27bf 139 video: MVideo,
d7a25329 140 videoFile: MVideoFileRedundanciesOpt,
d9a2a031 141 trackerUrls: string[]
d7a25329 142) {
90a8bd30 143 const xs = videoFile.getTorrentUrl()
d9a2a031 144 const announce = trackerUrls
90a8bd30 145 let urlList = [ videoFile.getFileUrl(video) ]
d7a25329
C
146
147 const redundancies = videoFile.RedundancyVideos
148 if (isArray(redundancies)) urlList = urlList.concat(redundancies.map(r => r.fileUrl))
149
150 const magnetHash = {
151 xs,
152 announce,
153 urlList,
154 infoHash: videoFile.infoHash,
155 name: video.name
156 }
157
158 return magnetUtil.encode(magnetHash)
159}
30ff39e7 160
ce33919c
C
161// ---------------------------------------------------------------------------
162
163export {
d441f2ed 164 createTorrentPromise,
1f6125be 165 updateTorrentUrls,
d7a25329
C
166 createTorrentAndSetInfoHash,
167 generateMagnetUri,
ce33919c
C
168 downloadWebTorrentVideo
169}
c48e82b5
C
170
171// ---------------------------------------------------------------------------
172
d0b52b52
C
173function safeWebtorrentDestroy (
174 webtorrent: WebTorrent.Instance,
175 torrentId: string,
176 downloadedFile?: { directoryPath: string, filepath: string },
177 torrentName?: string
178) {
ba5a8d89 179 return new Promise<void>(res => {
c48e82b5
C
180 webtorrent.destroy(err => {
181 // Delete torrent file
182 if (torrentName) {
d0b52b52 183 logger.debug('Removing %s torrent after webtorrent download.', torrentId)
c48e82b5
C
184 remove(torrentId)
185 .catch(err => logger.error('Cannot remove torrent %s in webtorrent download.', torrentId, { err }))
186 }
187
188 // Delete downloaded file
e37c85e9 189 if (downloadedFile) deleteDownloadedFile(downloadedFile)
c48e82b5 190
e37c85e9 191 if (err) logger.warn('Cannot destroy webtorrent in timeout.', { err })
c48e82b5
C
192
193 return res()
194 })
195 })
196}
e37c85e9
C
197
198function deleteDownloadedFile (downloadedFile: { directoryPath: string, filepath: string }) {
199 // We want to delete the base directory
200 let pathToDelete = dirname(downloadedFile.filepath)
201 if (pathToDelete === '.') pathToDelete = downloadedFile.filepath
202
203 const toRemovePath = join(downloadedFile.directoryPath, pathToDelete)
204
205 logger.debug('Removing %s after webtorrent download.', toRemovePath)
206 remove(toRemovePath)
207 .catch(err => logger.error('Cannot remove torrent file %s in webtorrent download.', toRemovePath, { err }))
208}
1f6125be
C
209
210function buildAnnounceList () {
211 return [
212 [ WEBSERVER.WS + '://' + WEBSERVER.HOSTNAME + ':' + WEBSERVER.PORT + '/tracker/socket' ],
213 [ WEBSERVER.URL + '/tracker/announce' ]
214 ]
215}
216
217function buildUrlList (video: MVideo, videoFile: MVideoFile) {
218 return [ videoFile.getFileUrl(video) ]
219}