aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/video/video-format-utils.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/models/video/video-format-utils.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/models/video/video-format-utils.ts')
-rw-r--r--server/models/video/video-format-utils.ts64
1 files changed, 38 insertions, 26 deletions
diff --git a/server/models/video/video-format-utils.ts b/server/models/video/video-format-utils.ts
index 77b8bcfe3..adf460734 100644
--- a/server/models/video/video-format-utils.ts
+++ b/server/models/video/video-format-utils.ts
@@ -1,16 +1,17 @@
1import { Video, VideoDetails } from '../../../shared/models/videos' 1import { generateMagnetUri } from '@server/helpers/webtorrent'
2import { VideoModel } from './video' 2import { getLocalVideoFileMetadataUrl } from '@server/lib/video-paths'
3import { VideoFile } from '@shared/models/videos/video-file.model'
3import { ActivityTagObject, ActivityUrlObject, VideoObject } from '../../../shared/models/activitypub/objects' 4import { ActivityTagObject, ActivityUrlObject, VideoObject } from '../../../shared/models/activitypub/objects'
5import { Video, VideoDetails } from '../../../shared/models/videos'
6import { VideoStreamingPlaylist } from '../../../shared/models/videos/video-streaming-playlist.model'
7import { isArray } from '../../helpers/custom-validators/misc'
4import { MIMETYPES, WEBSERVER } from '../../initializers/constants' 8import { MIMETYPES, WEBSERVER } from '../../initializers/constants'
5import { VideoCaptionModel } from './video-caption'
6import { 9import {
7 getLocalVideoCommentsActivityPubUrl, 10 getLocalVideoCommentsActivityPubUrl,
8 getLocalVideoDislikesActivityPubUrl, 11 getLocalVideoDislikesActivityPubUrl,
9 getLocalVideoLikesActivityPubUrl, 12 getLocalVideoLikesActivityPubUrl,
10 getLocalVideoSharesActivityPubUrl 13 getLocalVideoSharesActivityPubUrl
11} from '../../lib/activitypub/url' 14} from '../../lib/activitypub/url'
12import { isArray } from '../../helpers/custom-validators/misc'
13import { VideoStreamingPlaylist } from '../../../shared/models/videos/video-streaming-playlist.model'
14import { 15import {
15 MStreamingPlaylistRedundanciesOpt, 16 MStreamingPlaylistRedundanciesOpt,
16 MStreamingPlaylistVideo, 17 MStreamingPlaylistVideo,
@@ -18,12 +19,12 @@ import {
18 MVideoAP, 19 MVideoAP,
19 MVideoFile, 20 MVideoFile,
20 MVideoFormattable, 21 MVideoFormattable,
21 MVideoFormattableDetails 22 MVideoFormattableDetails,
23 MVideoWithHost
22} from '../../types/models' 24} from '../../types/models'
23import { MVideoFileRedundanciesOpt } from '../../types/models/video/video-file' 25import { MVideoFileRedundanciesOpt } from '../../types/models/video/video-file'
24import { VideoFile } from '@shared/models/videos/video-file.model' 26import { VideoModel } from './video'
25import { generateMagnetUri } from '@server/helpers/webtorrent' 27import { VideoCaptionModel } from './video-caption'
26import { extractVideo } from '@server/helpers/video'
27 28
28export type VideoFormattingJSONOptions = { 29export type VideoFormattingJSONOptions = {
29 completeDescription?: boolean 30 completeDescription?: boolean
@@ -153,12 +154,15 @@ function videoModelToFormattedDetailsJSON (video: MVideoFormattableDetails): Vid
153 } 154 }
154 155
155 // Format and sort video files 156 // Format and sort video files
156 detailsJson.files = videoFilesModelToFormattedJSON(video, baseUrlHttp, baseUrlWs, video.VideoFiles) 157 detailsJson.files = videoFilesModelToFormattedJSON(video, video, baseUrlHttp, baseUrlWs, video.VideoFiles)
157 158
158 return Object.assign(formattedJson, detailsJson) 159 return Object.assign(formattedJson, detailsJson)
159} 160}
160 161
161function streamingPlaylistsModelToFormattedJSON (video: MVideo, playlists: MStreamingPlaylistRedundanciesOpt[]): VideoStreamingPlaylist[] { 162function streamingPlaylistsModelToFormattedJSON (
163 video: MVideoFormattableDetails,
164 playlists: MStreamingPlaylistRedundanciesOpt[]
165): VideoStreamingPlaylist[] {
162 if (isArray(playlists) === false) return [] 166 if (isArray(playlists) === false) return []
163 167
164 const { baseUrlHttp, baseUrlWs } = video.getBaseUrls() 168 const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
@@ -171,7 +175,7 @@ function streamingPlaylistsModelToFormattedJSON (video: MVideo, playlists: MStre
171 ? playlist.RedundancyVideos.map(r => ({ baseUrl: r.fileUrl })) 175 ? playlist.RedundancyVideos.map(r => ({ baseUrl: r.fileUrl }))
172 : [] 176 : []
173 177
174 const files = videoFilesModelToFormattedJSON(playlistWithVideo, baseUrlHttp, baseUrlWs, playlist.VideoFiles) 178 const files = videoFilesModelToFormattedJSON(playlistWithVideo, video, baseUrlHttp, baseUrlWs, playlist.VideoFiles)
175 179
176 return { 180 return {
177 id: playlist.id, 181 id: playlist.id,
@@ -190,14 +194,14 @@ function sortByResolutionDesc (fileA: MVideoFile, fileB: MVideoFile) {
190 return -1 194 return -1
191} 195}
192 196
197// FIXME: refactor/merge model and video arguments
193function videoFilesModelToFormattedJSON ( 198function videoFilesModelToFormattedJSON (
194 model: MVideo | MStreamingPlaylistVideo, 199 model: MVideo | MStreamingPlaylistVideo,
200 video: MVideoFormattableDetails,
195 baseUrlHttp: string, 201 baseUrlHttp: string,
196 baseUrlWs: string, 202 baseUrlWs: string,
197 videoFiles: MVideoFileRedundanciesOpt[] 203 videoFiles: MVideoFileRedundanciesOpt[]
198): VideoFile[] { 204): VideoFile[] {
199 const video = extractVideo(model)
200
201 return [ ...videoFiles ] 205 return [ ...videoFiles ]
202 .filter(f => !f.isLive()) 206 .filter(f => !f.isLive())
203 .sort(sortByResolutionDesc) 207 .sort(sortByResolutionDesc)
@@ -207,21 +211,29 @@ function videoFilesModelToFormattedJSON (
207 id: videoFile.resolution, 211 id: videoFile.resolution,
208 label: videoFile.resolution + 'p' 212 label: videoFile.resolution + 'p'
209 }, 213 },
210 magnetUri: generateMagnetUri(model, videoFile, baseUrlHttp, baseUrlWs), 214
215 // FIXME: deprecated in 3.2
216 magnetUri: generateMagnetUri(model, video, videoFile, baseUrlHttp, baseUrlWs),
217
211 size: videoFile.size, 218 size: videoFile.size,
212 fps: videoFile.fps, 219 fps: videoFile.fps,
213 torrentUrl: model.getTorrentUrl(videoFile, baseUrlHttp), 220
214 torrentDownloadUrl: model.getTorrentDownloadUrl(videoFile, baseUrlHttp), 221 torrentUrl: videoFile.getTorrentUrl(),
215 fileUrl: model.getVideoFileUrl(videoFile, baseUrlHttp), 222 torrentDownloadUrl: videoFile.getTorrentDownloadUrl(),
216 fileDownloadUrl: model.getVideoFileDownloadUrl(videoFile, baseUrlHttp), 223
217 metadataUrl: video.getVideoFileMetadataUrl(videoFile, baseUrlHttp) 224 fileUrl: videoFile.getFileUrl(video),
225 fileDownloadUrl: videoFile.getFileDownloadUrl(video),
226
227 metadataUrl: videoFile.metadataUrl ?? getLocalVideoFileMetadataUrl(video, videoFile)
218 } as VideoFile 228 } as VideoFile
219 }) 229 })
220} 230}
221 231
232// FIXME: refactor/merge model and video arguments
222function addVideoFilesInAPAcc ( 233function addVideoFilesInAPAcc (
223 acc: ActivityUrlObject[] | ActivityTagObject[], 234 acc: ActivityUrlObject[] | ActivityTagObject[],
224 model: MVideoAP | MStreamingPlaylistVideo, 235 model: MVideoAP | MStreamingPlaylistVideo,
236 video: MVideoWithHost,
225 baseUrlHttp: string, 237 baseUrlHttp: string,
226 baseUrlWs: string, 238 baseUrlWs: string,
227 files: MVideoFile[] 239 files: MVideoFile[]
@@ -234,7 +246,7 @@ function addVideoFilesInAPAcc (
234 acc.push({ 246 acc.push({
235 type: 'Link', 247 type: 'Link',
236 mediaType: MIMETYPES.VIDEO.EXT_MIMETYPE[file.extname] as any, 248 mediaType: MIMETYPES.VIDEO.EXT_MIMETYPE[file.extname] as any,
237 href: model.getVideoFileUrl(file, baseUrlHttp), 249 href: file.getFileUrl(video),
238 height: file.resolution, 250 height: file.resolution,
239 size: file.size, 251 size: file.size,
240 fps: file.fps 252 fps: file.fps
@@ -244,7 +256,7 @@ function addVideoFilesInAPAcc (
244 type: 'Link', 256 type: 'Link',
245 rel: [ 'metadata', MIMETYPES.VIDEO.EXT_MIMETYPE[file.extname] ], 257 rel: [ 'metadata', MIMETYPES.VIDEO.EXT_MIMETYPE[file.extname] ],
246 mediaType: 'application/json' as 'application/json', 258 mediaType: 'application/json' as 'application/json',
247 href: extractVideo(model).getVideoFileMetadataUrl(file, baseUrlHttp), 259 href: getLocalVideoFileMetadataUrl(video, file),
248 height: file.resolution, 260 height: file.resolution,
249 fps: file.fps 261 fps: file.fps
250 }) 262 })
@@ -252,14 +264,14 @@ function addVideoFilesInAPAcc (
252 acc.push({ 264 acc.push({
253 type: 'Link', 265 type: 'Link',
254 mediaType: 'application/x-bittorrent' as 'application/x-bittorrent', 266 mediaType: 'application/x-bittorrent' as 'application/x-bittorrent',
255 href: model.getTorrentUrl(file, baseUrlHttp), 267 href: file.getTorrentUrl(),
256 height: file.resolution 268 height: file.resolution
257 }) 269 })
258 270
259 acc.push({ 271 acc.push({
260 type: 'Link', 272 type: 'Link',
261 mediaType: 'application/x-bittorrent;x-scheme-handler/magnet' as 'application/x-bittorrent;x-scheme-handler/magnet', 273 mediaType: 'application/x-bittorrent;x-scheme-handler/magnet' as 'application/x-bittorrent;x-scheme-handler/magnet',
262 href: generateMagnetUri(model, file, baseUrlHttp, baseUrlWs), 274 href: generateMagnetUri(model, video, file, baseUrlHttp, baseUrlWs),
263 height: file.resolution 275 height: file.resolution
264 }) 276 })
265 } 277 }
@@ -307,7 +319,7 @@ function videoModelToActivityPubObject (video: MVideoAP): VideoObject {
307 } 319 }
308 ] 320 ]
309 321
310 addVideoFilesInAPAcc(url, video, baseUrlHttp, baseUrlWs, video.VideoFiles || []) 322 addVideoFilesInAPAcc(url, video, video, baseUrlHttp, baseUrlWs, video.VideoFiles || [])
311 323
312 for (const playlist of (video.VideoStreamingPlaylists || [])) { 324 for (const playlist of (video.VideoStreamingPlaylists || [])) {
313 const tag = playlist.p2pMediaLoaderInfohashes 325 const tag = playlist.p2pMediaLoaderInfohashes
@@ -320,7 +332,7 @@ function videoModelToActivityPubObject (video: MVideoAP): VideoObject {
320 }) 332 })
321 333
322 const playlistWithVideo = Object.assign(playlist, { Video: video }) 334 const playlistWithVideo = Object.assign(playlist, { Video: video })
323 addVideoFilesInAPAcc(tag, playlistWithVideo, baseUrlHttp, baseUrlWs, playlist.VideoFiles || []) 335 addVideoFilesInAPAcc(tag, playlistWithVideo, video, baseUrlHttp, baseUrlWs, playlist.VideoFiles || [])
324 336
325 url.push({ 337 url.push({
326 type: 'Link', 338 type: 'Link',