]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/videos/shared/object-to-model-attributes.ts
Force live type specification in first step
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / videos / shared / object-to-model-attributes.ts
1 import { maxBy, minBy } from 'lodash'
2 import magnetUtil from 'magnet-uri'
3 import { basename } from 'path'
4 import { isAPVideoFileUrlMetadataObject } from '@server/helpers/custom-validators/activitypub/videos'
5 import { isVideoFileInfoHashValid } from '@server/helpers/custom-validators/videos'
6 import { logger } from '@server/helpers/logger'
7 import { getExtFromMimetype } from '@server/helpers/video'
8 import { ACTIVITY_PUB, MIMETYPES, P2P_MEDIA_LOADER_PEER_VERSION, PREVIEWS_SIZE, THUMBNAILS_SIZE } from '@server/initializers/constants'
9 import { generateTorrentFileName } from '@server/lib/paths'
10 import { VideoCaptionModel } from '@server/models/video/video-caption'
11 import { VideoFileModel } from '@server/models/video/video-file'
12 import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist'
13 import { FilteredModelAttributes } from '@server/types'
14 import { isStreamingPlaylist, MChannelId, MStreamingPlaylistVideo, MVideo, MVideoId } from '@server/types/models'
15 import {
16 ActivityHashTagObject,
17 ActivityMagnetUrlObject,
18 ActivityPlaylistSegmentHashesObject,
19 ActivityPlaylistUrlObject,
20 ActivityTagObject,
21 ActivityUrlObject,
22 ActivityVideoUrlObject,
23 VideoObject,
24 VideoPrivacy,
25 VideoStreamingPlaylistType
26 } from '@shared/models'
27
28 function getThumbnailFromIcons (videoObject: VideoObject) {
29 let validIcons = videoObject.icon.filter(i => i.width > THUMBNAILS_SIZE.minWidth)
30 // Fallback if there are not valid icons
31 if (validIcons.length === 0) validIcons = videoObject.icon
32
33 return minBy(validIcons, 'width')
34 }
35
36 function getPreviewFromIcons (videoObject: VideoObject) {
37 const validIcons = videoObject.icon.filter(i => i.width > PREVIEWS_SIZE.minWidth)
38
39 return maxBy(validIcons, 'width')
40 }
41
42 function getTagsFromObject (videoObject: VideoObject) {
43 return videoObject.tag
44 .filter(isAPHashTagObject)
45 .map(t => t.name)
46 }
47
48 function getFileAttributesFromUrl (
49 videoOrPlaylist: MVideo | MStreamingPlaylistVideo,
50 urls: (ActivityTagObject | ActivityUrlObject)[]
51 ) {
52 const fileUrls = urls.filter(u => isAPVideoUrlObject(u)) as ActivityVideoUrlObject[]
53
54 if (fileUrls.length === 0) return []
55
56 const attributes: FilteredModelAttributes<VideoFileModel>[] = []
57 for (const fileUrl of fileUrls) {
58 // Fetch associated magnet uri
59 const magnet = urls.filter(isAPMagnetUrlObject)
60 .find(u => u.height === fileUrl.height)
61
62 if (!magnet) throw new Error('Cannot find associated magnet uri for file ' + fileUrl.href)
63
64 const parsed = magnetUtil.decode(magnet.href)
65 if (!parsed || isVideoFileInfoHashValid(parsed.infoHash) === false) {
66 throw new Error('Cannot parse magnet URI ' + magnet.href)
67 }
68
69 const torrentUrl = Array.isArray(parsed.xs)
70 ? parsed.xs[0]
71 : parsed.xs
72
73 // Fetch associated metadata url, if any
74 const metadata = urls.filter(isAPVideoFileUrlMetadataObject)
75 .find(u => {
76 return u.height === fileUrl.height &&
77 u.fps === fileUrl.fps &&
78 u.rel.includes(fileUrl.mediaType)
79 })
80
81 const extname = getExtFromMimetype(MIMETYPES.VIDEO.MIMETYPE_EXT, fileUrl.mediaType)
82 const resolution = fileUrl.height
83 const videoId = isStreamingPlaylist(videoOrPlaylist) ? null : videoOrPlaylist.id
84 const videoStreamingPlaylistId = isStreamingPlaylist(videoOrPlaylist) ? videoOrPlaylist.id : null
85
86 const attribute = {
87 extname,
88 infoHash: parsed.infoHash,
89 resolution,
90 size: fileUrl.size,
91 fps: fileUrl.fps || -1,
92 metadataUrl: metadata?.href,
93
94 // Use the name of the remote file because we don't proxify video file requests
95 filename: basename(fileUrl.href),
96 fileUrl: fileUrl.href,
97
98 torrentUrl,
99 // Use our own torrent name since we proxify torrent requests
100 torrentFilename: generateTorrentFileName(videoOrPlaylist, resolution),
101
102 // This is a video file owned by a video or by a streaming playlist
103 videoId,
104 videoStreamingPlaylistId
105 }
106
107 attributes.push(attribute)
108 }
109
110 return attributes
111 }
112
113 function getStreamingPlaylistAttributesFromObject (video: MVideoId, videoObject: VideoObject) {
114 const playlistUrls = videoObject.url.filter(u => isAPStreamingPlaylistUrlObject(u)) as ActivityPlaylistUrlObject[]
115 if (playlistUrls.length === 0) return []
116
117 const attributes: (FilteredModelAttributes<VideoStreamingPlaylistModel> & { tagAPObject?: ActivityTagObject[] })[] = []
118 for (const playlistUrlObject of playlistUrls) {
119 const segmentsSha256UrlObject = playlistUrlObject.tag.find(isAPPlaylistSegmentHashesUrlObject)
120
121 const files: unknown[] = playlistUrlObject.tag.filter(u => isAPVideoUrlObject(u)) as ActivityVideoUrlObject[]
122
123 if (!segmentsSha256UrlObject) {
124 logger.warn('No segment sha256 URL found in AP playlist object.', { playlistUrl: playlistUrlObject })
125 continue
126 }
127
128 const attribute = {
129 type: VideoStreamingPlaylistType.HLS,
130
131 playlistFilename: basename(playlistUrlObject.href),
132 playlistUrl: playlistUrlObject.href,
133
134 segmentsSha256Filename: basename(segmentsSha256UrlObject.href),
135 segmentsSha256Url: segmentsSha256UrlObject.href,
136
137 p2pMediaLoaderInfohashes: VideoStreamingPlaylistModel.buildP2PMediaLoaderInfoHashes(playlistUrlObject.href, files),
138 p2pMediaLoaderPeerVersion: P2P_MEDIA_LOADER_PEER_VERSION,
139 videoId: video.id,
140
141 tagAPObject: playlistUrlObject.tag
142 }
143
144 attributes.push(attribute)
145 }
146
147 return attributes
148 }
149
150 function getLiveAttributesFromObject (video: MVideoId, videoObject: VideoObject) {
151 return {
152 saveReplay: videoObject.liveSaveReplay,
153 permanentLive: videoObject.permanentLive,
154 videoId: video.id
155 }
156 }
157
158 function getCaptionAttributesFromObject (video: MVideoId, videoObject: VideoObject) {
159 return videoObject.subtitleLanguage.map(c => ({
160 videoId: video.id,
161 filename: VideoCaptionModel.generateCaptionName(c.identifier),
162 language: c.identifier,
163 fileUrl: c.url
164 }))
165 }
166
167 function getVideoAttributesFromObject (videoChannel: MChannelId, videoObject: VideoObject, to: string[] = []) {
168 const privacy = to.includes(ACTIVITY_PUB.PUBLIC)
169 ? VideoPrivacy.PUBLIC
170 : VideoPrivacy.UNLISTED
171
172 const duration = videoObject.duration.replace(/[^\d]+/, '')
173 const language = videoObject.language?.identifier
174
175 const category = videoObject.category
176 ? parseInt(videoObject.category.identifier, 10)
177 : undefined
178
179 const licence = videoObject.licence
180 ? parseInt(videoObject.licence.identifier, 10)
181 : undefined
182
183 const description = videoObject.content || null
184 const support = videoObject.support || null
185
186 return {
187 name: videoObject.name,
188 uuid: videoObject.uuid,
189 url: videoObject.id,
190 category,
191 licence,
192 language,
193 description,
194 support,
195 nsfw: videoObject.sensitive,
196 commentsEnabled: videoObject.commentsEnabled,
197 downloadEnabled: videoObject.downloadEnabled,
198 waitTranscoding: videoObject.waitTranscoding,
199 isLive: videoObject.isLiveBroadcast,
200 state: videoObject.state,
201 channelId: videoChannel.id,
202 duration: parseInt(duration, 10),
203 createdAt: new Date(videoObject.published),
204 publishedAt: new Date(videoObject.published),
205
206 originallyPublishedAt: videoObject.originallyPublishedAt
207 ? new Date(videoObject.originallyPublishedAt)
208 : null,
209
210 updatedAt: new Date(videoObject.updated),
211 views: videoObject.views,
212 likes: 0,
213 dislikes: 0,
214 remote: true,
215 privacy
216 }
217 }
218
219 // ---------------------------------------------------------------------------
220
221 export {
222 getThumbnailFromIcons,
223 getPreviewFromIcons,
224
225 getTagsFromObject,
226
227 getFileAttributesFromUrl,
228 getStreamingPlaylistAttributesFromObject,
229
230 getLiveAttributesFromObject,
231 getCaptionAttributesFromObject,
232
233 getVideoAttributesFromObject
234 }
235
236 // ---------------------------------------------------------------------------
237
238 function isAPVideoUrlObject (url: any): url is ActivityVideoUrlObject {
239 const urlMediaType = url.mediaType
240
241 return MIMETYPES.VIDEO.MIMETYPE_EXT[urlMediaType] && urlMediaType.startsWith('video/')
242 }
243
244 function isAPStreamingPlaylistUrlObject (url: any): url is ActivityPlaylistUrlObject {
245 return url && url.mediaType === 'application/x-mpegURL'
246 }
247
248 function isAPPlaylistSegmentHashesUrlObject (tag: any): tag is ActivityPlaylistSegmentHashesObject {
249 return tag && tag.name === 'sha256' && tag.type === 'Link' && tag.mediaType === 'application/json'
250 }
251
252 function isAPMagnetUrlObject (url: any): url is ActivityMagnetUrlObject {
253 return url && url.mediaType === 'application/x-bittorrent;x-scheme-handler/magnet'
254 }
255
256 function isAPHashTagObject (url: any): url is ActivityHashTagObject {
257 return url && url.type === 'Hashtag'
258 }