diff options
author | Chocobozzz <me@florianbigard.com> | 2021-02-16 16:25:53 +0100 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2021-02-18 13:38:09 +0100 |
commit | 90a8bd305de4153ec21137a73ff482dcc2e3e19b (patch) | |
tree | 2e35b5504ec11bc51579c92a70c77ed3d5ace816 /server/helpers/webtorrent.ts | |
parent | 684cdacbbd775b5f404dd7b373e02dd21baf5ff0 (diff) | |
download | PeerTube-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.ts | 57 |
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 @@ | |||
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' | 1 | import * as createTorrent from 'create-torrent' |
8 | import { promisify2 } from './core-utils' | 2 | import { createWriteStream, ensureDir, remove, writeFile } from 'fs-extra' |
9 | import { MVideo } from '@server/types/models/video/video' | ||
10 | import { MVideoFile, MVideoFileRedundanciesOpt } from '@server/types/models/video/video-file' | ||
11 | import { isStreamingPlaylist, MStreamingPlaylistVideo } from '@server/types/models/video/video-streaming-playlist' | ||
12 | import { WEBSERVER } from '@server/initializers/constants' | ||
13 | import * as parseTorrent from 'parse-torrent' | ||
14 | import * as magnetUtil from 'magnet-uri' | 3 | import * as magnetUtil from 'magnet-uri' |
4 | import * as parseTorrent from 'parse-torrent' | ||
5 | import { dirname, join } from 'path' | ||
6 | import * as WebTorrent from 'webtorrent' | ||
15 | import { isArray } from '@server/helpers/custom-validators/misc' | 7 | import { isArray } from '@server/helpers/custom-validators/misc' |
16 | import { getTorrentFileName, getVideoFilePath } from '@server/lib/video-paths' | 8 | import { WEBSERVER } from '@server/initializers/constants' |
17 | import { extractVideo } from '@server/helpers/video' | 9 | import { generateTorrentFileName, getVideoFilePath } from '@server/lib/video-paths' |
10 | import { MVideo, MVideoWithHost } from '@server/types/models/video/video' | ||
11 | import { MVideoFile, MVideoFileRedundanciesOpt } from '@server/types/models/video/video-file' | ||
12 | import { MStreamingPlaylistVideo } from '@server/types/models/video/video-streaming-playlist' | ||
13 | import { CONFIG } from '../initializers/config' | ||
14 | import { promisify2 } from './core-utils' | ||
15 | import { logger } from './logger' | ||
16 | import { generateVideoImportTmpPath } from './utils' | ||
18 | 17 | ||
19 | const createTorrentPromise = promisify2<string, any, any>(createTorrent) | 18 | const 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 | ||
81 | async function createTorrentAndSetInfoHash (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, videoFile: MVideoFile) { | 80 | // FIXME: refactor/merge videoOrPlaylist and video arguments |
82 | const video = extractVideo(videoOrPlaylist) | 81 | async 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 | ||
107 | function generateMagnetUri ( | 111 | function 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)) |