diff options
Diffstat (limited to 'server/models/video/video-format-utils.ts')
-rw-r--r-- | server/models/video/video-format-utils.ts | 64 |
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 @@ | |||
1 | import { Video, VideoDetails } from '../../../shared/models/videos' | 1 | import { generateMagnetUri } from '@server/helpers/webtorrent' |
2 | import { VideoModel } from './video' | 2 | import { getLocalVideoFileMetadataUrl } from '@server/lib/video-paths' |
3 | import { VideoFile } from '@shared/models/videos/video-file.model' | ||
3 | import { ActivityTagObject, ActivityUrlObject, VideoObject } from '../../../shared/models/activitypub/objects' | 4 | import { ActivityTagObject, ActivityUrlObject, VideoObject } from '../../../shared/models/activitypub/objects' |
5 | import { Video, VideoDetails } from '../../../shared/models/videos' | ||
6 | import { VideoStreamingPlaylist } from '../../../shared/models/videos/video-streaming-playlist.model' | ||
7 | import { isArray } from '../../helpers/custom-validators/misc' | ||
4 | import { MIMETYPES, WEBSERVER } from '../../initializers/constants' | 8 | import { MIMETYPES, WEBSERVER } from '../../initializers/constants' |
5 | import { VideoCaptionModel } from './video-caption' | ||
6 | import { | 9 | import { |
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' |
12 | import { isArray } from '../../helpers/custom-validators/misc' | ||
13 | import { VideoStreamingPlaylist } from '../../../shared/models/videos/video-streaming-playlist.model' | ||
14 | import { | 15 | import { |
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' |
23 | import { MVideoFileRedundanciesOpt } from '../../types/models/video/video-file' | 25 | import { MVideoFileRedundanciesOpt } from '../../types/models/video/video-file' |
24 | import { VideoFile } from '@shared/models/videos/video-file.model' | 26 | import { VideoModel } from './video' |
25 | import { generateMagnetUri } from '@server/helpers/webtorrent' | 27 | import { VideoCaptionModel } from './video-caption' |
26 | import { extractVideo } from '@server/helpers/video' | ||
27 | 28 | ||
28 | export type VideoFormattingJSONOptions = { | 29 | export 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 | ||
161 | function streamingPlaylistsModelToFormattedJSON (video: MVideo, playlists: MStreamingPlaylistRedundanciesOpt[]): VideoStreamingPlaylist[] { | 162 | function 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 | ||
193 | function videoFilesModelToFormattedJSON ( | 198 | function 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 | ||
222 | function addVideoFilesInAPAcc ( | 233 | function 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', |