+async function createTorrentAndSetInfoHash (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, videoFile: MVideoFile) {
+ const video = extractVideo(videoOrPlaylist)
+ const { baseUrlHttp } = video.getBaseUrls()
+
+ const options = {
+ // Keep the extname, it's used by the client to stream the file inside a web browser
+ name: `${video.name} ${videoFile.resolution}p${videoFile.extname}`,
+ createdBy: 'PeerTube',
+ announceList: [
+ [ WEBSERVER.WS + '://' + WEBSERVER.HOSTNAME + ':' + WEBSERVER.PORT + '/tracker/socket' ],
+ [ WEBSERVER.URL + '/tracker/announce' ]
+ ],
+ urlList: [ videoOrPlaylist.getVideoFileUrl(videoFile, baseUrlHttp) ]
+ }
+
+ const torrent = await createTorrentPromise(getVideoFilePath(videoOrPlaylist, videoFile), options)
+
+ const filePath = join(CONFIG.STORAGE.TORRENTS_DIR, getTorrentFileName(videoOrPlaylist, videoFile))
+ logger.info('Creating torrent %s.', filePath)
+
+ await writeFile(filePath, torrent)
+
+ const parsedTorrent = parseTorrent(torrent)
+ videoFile.infoHash = parsedTorrent.infoHash
+}
+
+function generateMagnetUri (
+ videoOrPlaylist: MVideo | MStreamingPlaylistVideo,
+ videoFile: MVideoFileRedundanciesOpt,
+ baseUrlHttp: string,
+ baseUrlWs: string
+) {
+ const video = isStreamingPlaylist(videoOrPlaylist)
+ ? videoOrPlaylist.Video
+ : videoOrPlaylist
+
+ const xs = videoOrPlaylist.getTorrentUrl(videoFile, baseUrlHttp)
+ const announce = videoOrPlaylist.getTrackerUrls(baseUrlHttp, baseUrlWs)
+ let urlList = [ videoOrPlaylist.getVideoFileUrl(videoFile, baseUrlHttp) ]
+
+ const redundancies = videoFile.RedundancyVideos
+ if (isArray(redundancies)) urlList = urlList.concat(redundancies.map(r => r.fileUrl))
+
+ const magnetHash = {
+ xs,
+ announce,
+ urlList,
+ infoHash: videoFile.infoHash,
+ name: video.name
+ }
+
+ return magnetUtil.encode(magnetHash)
+}
+