aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/webtorrent.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-02-16 16:25:53 +0100
committerChocobozzz <chocobozzz@cpy.re>2021-02-18 13:38:09 +0100
commit90a8bd305de4153ec21137a73ff482dcc2e3e19b (patch)
tree2e35b5504ec11bc51579c92a70c77ed3d5ace816 /server/helpers/webtorrent.ts
parent684cdacbbd775b5f404dd7b373e02dd21baf5ff0 (diff)
downloadPeerTube-90a8bd305de4153ec21137a73ff482dcc2e3e19b.tar.gz
PeerTube-90a8bd305de4153ec21137a73ff482dcc2e3e19b.tar.zst
PeerTube-90a8bd305de4153ec21137a73ff482dcc2e3e19b.zip
Dissociate video file names and video uuid
Diffstat (limited to 'server/helpers/webtorrent.ts')
-rw-r--r--server/helpers/webtorrent.ts57
1 files changed, 29 insertions, 28 deletions
diff --git a/server/helpers/webtorrent.ts b/server/helpers/webtorrent.ts
index 9c5df2083..73418aa0a 100644
--- a/server/helpers/webtorrent.ts
+++ b/server/helpers/webtorrent.ts
@@ -1,20 +1,19 @@
1import { logger } from './logger'
2import { generateVideoImportTmpPath } from './utils'
3import * as WebTorrent from 'webtorrent'
4import { createWriteStream, ensureDir, remove, writeFile } from 'fs-extra'
5import { CONFIG } from '../initializers/config'
6import { dirname, join } from 'path'
7import * as createTorrent from 'create-torrent' 1import * as createTorrent from 'create-torrent'
8import { promisify2 } from './core-utils' 2import { createWriteStream, ensureDir, remove, writeFile } from 'fs-extra'
9import { MVideo } from '@server/types/models/video/video'
10import { MVideoFile, MVideoFileRedundanciesOpt } from '@server/types/models/video/video-file'
11import { isStreamingPlaylist, MStreamingPlaylistVideo } from '@server/types/models/video/video-streaming-playlist'
12import { WEBSERVER } from '@server/initializers/constants'
13import * as parseTorrent from 'parse-torrent'
14import * as magnetUtil from 'magnet-uri' 3import * as magnetUtil from 'magnet-uri'
4import * as parseTorrent from 'parse-torrent'
5import { dirname, join } from 'path'
6import * as WebTorrent from 'webtorrent'
15import { isArray } from '@server/helpers/custom-validators/misc' 7import { isArray } from '@server/helpers/custom-validators/misc'
16import { getTorrentFileName, getVideoFilePath } from '@server/lib/video-paths' 8import { WEBSERVER } from '@server/initializers/constants'
17import { extractVideo } from '@server/helpers/video' 9import { generateTorrentFileName, getVideoFilePath } from '@server/lib/video-paths'
10import { MVideo, MVideoWithHost } from '@server/types/models/video/video'
11import { MVideoFile, MVideoFileRedundanciesOpt } from '@server/types/models/video/video-file'
12import { MStreamingPlaylistVideo } from '@server/types/models/video/video-streaming-playlist'
13import { CONFIG } from '../initializers/config'
14import { promisify2 } from './core-utils'
15import { logger } from './logger'
16import { generateVideoImportTmpPath } from './utils'
18 17
19const createTorrentPromise = promisify2<string, any, any>(createTorrent) 18const createTorrentPromise = promisify2<string, any, any>(createTorrent)
20 19
@@ -78,10 +77,12 @@ async function downloadWebTorrentVideo (target: { magnetUri: string, torrentName
78 }) 77 })
79} 78}
80 79
81async function createTorrentAndSetInfoHash (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, videoFile: MVideoFile) { 80// FIXME: refactor/merge videoOrPlaylist and video arguments
82 const video = extractVideo(videoOrPlaylist) 81async function createTorrentAndSetInfoHash (
83 const { baseUrlHttp } = video.getBaseUrls() 82 videoOrPlaylist: MVideo | MStreamingPlaylistVideo,
84 83 video: MVideoWithHost,
84 videoFile: MVideoFile
85) {
85 const options = { 86 const options = {
86 // Keep the extname, it's used by the client to stream the file inside a web browser 87 // Keep the extname, it's used by the client to stream the file inside a web browser
87 name: `${video.name} ${videoFile.resolution}p${videoFile.extname}`, 88 name: `${video.name} ${videoFile.resolution}p${videoFile.extname}`,
@@ -90,33 +91,33 @@ async function createTorrentAndSetInfoHash (videoOrPlaylist: MVideo | MStreaming
90 [ WEBSERVER.WS + '://' + WEBSERVER.HOSTNAME + ':' + WEBSERVER.PORT + '/tracker/socket' ], 91 [ WEBSERVER.WS + '://' + WEBSERVER.HOSTNAME + ':' + WEBSERVER.PORT + '/tracker/socket' ],
91 [ WEBSERVER.URL + '/tracker/announce' ] 92 [ WEBSERVER.URL + '/tracker/announce' ]
92 ], 93 ],
93 urlList: [ videoOrPlaylist.getVideoFileUrl(videoFile, baseUrlHttp) ] 94 urlList: [ videoFile.getFileUrl(video) ]
94 } 95 }
95 96
96 const torrent = await createTorrentPromise(getVideoFilePath(videoOrPlaylist, videoFile), options) 97 const torrent = await createTorrentPromise(getVideoFilePath(videoOrPlaylist, videoFile), options)
97 98
98 const filePath = join(CONFIG.STORAGE.TORRENTS_DIR, getTorrentFileName(videoOrPlaylist, videoFile)) 99 const torrentFilename = generateTorrentFileName(videoOrPlaylist, videoFile.resolution)
99 logger.info('Creating torrent %s.', filePath) 100 const torrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, torrentFilename)
101 logger.info('Creating torrent %s.', torrentPath)
100 102
101 await writeFile(filePath, torrent) 103 await writeFile(torrentPath, torrent)
102 104
103 const parsedTorrent = parseTorrent(torrent) 105 const parsedTorrent = parseTorrent(torrent)
104 videoFile.infoHash = parsedTorrent.infoHash 106 videoFile.infoHash = parsedTorrent.infoHash
107 videoFile.torrentFilename = torrentFilename
105} 108}
106 109
110// FIXME: merge/refactor videoOrPlaylist and video arguments
107function generateMagnetUri ( 111function generateMagnetUri (
108 videoOrPlaylist: MVideo | MStreamingPlaylistVideo, 112 videoOrPlaylist: MVideo | MStreamingPlaylistVideo,
113 video: MVideoWithHost,
109 videoFile: MVideoFileRedundanciesOpt, 114 videoFile: MVideoFileRedundanciesOpt,
110 baseUrlHttp: string, 115 baseUrlHttp: string,
111 baseUrlWs: string 116 baseUrlWs: string
112) { 117) {
113 const video = isStreamingPlaylist(videoOrPlaylist) 118 const xs = videoFile.getTorrentUrl()
114 ? videoOrPlaylist.Video
115 : videoOrPlaylist
116
117 const xs = videoOrPlaylist.getTorrentUrl(videoFile, baseUrlHttp)
118 const announce = videoOrPlaylist.getTrackerUrls(baseUrlHttp, baseUrlWs) 119 const announce = videoOrPlaylist.getTrackerUrls(baseUrlHttp, baseUrlWs)
119 let urlList = [ videoOrPlaylist.getVideoFileUrl(videoFile, baseUrlHttp) ] 120 let urlList = [ videoFile.getFileUrl(video) ]
120 121
121 const redundancies = videoFile.RedundancyVideos 122 const redundancies = videoFile.RedundancyVideos
122 if (isArray(redundancies)) urlList = urlList.concat(redundancies.map(r => r.fileUrl)) 123 if (isArray(redundancies)) urlList = urlList.concat(redundancies.map(r => r.fileUrl))