+async function createTorrentAndSetInfoHash (
+ videoOrPlaylist: MVideo | MStreamingPlaylistVideo,
+ videoFile: MVideoFile
+) {
+ const video = extractVideo(videoOrPlaylist)
+
+ 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: [ videoFile.getFileUrl(video) ]
+ }
+
+ const torrent = await createTorrentPromise(getVideoFilePath(videoOrPlaylist, videoFile), options)
+
+ const torrentFilename = generateTorrentFileName(videoOrPlaylist, videoFile.resolution)
+ const torrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, torrentFilename)
+ logger.info('Creating torrent %s.', torrentPath)
+
+ await writeFile(torrentPath, torrent)
+
+ const parsedTorrent = parseTorrent(torrent)
+ videoFile.infoHash = parsedTorrent.infoHash
+ videoFile.torrentFilename = torrentFilename
+}
+
+function generateMagnetUri (
+ video: MVideo,
+ videoFile: MVideoFileRedundanciesOpt,
+ trackerUrls: string[]
+) {
+ const xs = videoFile.getTorrentUrl()
+ const announce = trackerUrls
+ let urlList = [ videoFile.getFileUrl(video) ]
+
+ 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)
+}
+