]>
Commit | Line | Data |
---|---|---|
69290ab3 C |
1 | import { maxBy, minBy } from 'lodash' |
2 | import * as 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/video-paths' | |
764b1a14 | 10 | import { VideoCaptionModel } from '@server/models/video/video-caption' |
69290ab3 C |
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' | |
764b1a14 | 14 | import { isStreamingPlaylist, MChannelId, MStreamingPlaylistVideo, MVideo, MVideoFile, MVideoId } from '@server/types/models' |
69290ab3 C |
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 | ||
08a47c75 | 48 | function getFileAttributesFromUrl ( |
69290ab3 C |
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 | |
764b1a14 C |
83 | const videoId = isStreamingPlaylist(videoOrPlaylist) ? null : videoOrPlaylist.id |
84 | const videoStreamingPlaylistId = isStreamingPlaylist(videoOrPlaylist) ? videoOrPlaylist.id : null | |
69290ab3 C |
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 | ||
08a47c75 | 113 | function getStreamingPlaylistAttributesFromObject (video: MVideoId, videoObject: VideoObject, videoFiles: MVideoFile[]) { |
69290ab3 C |
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 | let files: unknown[] = playlistUrlObject.tag.filter(u => isAPVideoUrlObject(u)) as ActivityVideoUrlObject[] | |
122 | ||
123 | // FIXME: backward compatibility introduced in v2.1.0 | |
124 | if (files.length === 0) files = videoFiles | |
125 | ||
126 | if (!segmentsSha256UrlObject) { | |
127 | logger.warn('No segment sha256 URL found in AP playlist object.', { playlistUrl: playlistUrlObject }) | |
128 | continue | |
129 | } | |
130 | ||
131 | const attribute = { | |
132 | type: VideoStreamingPlaylistType.HLS, | |
764b1a14 C |
133 | |
134 | playlistFilename: basename(playlistUrlObject.href), | |
69290ab3 | 135 | playlistUrl: playlistUrlObject.href, |
764b1a14 C |
136 | |
137 | segmentsSha256Filename: basename(segmentsSha256UrlObject.href), | |
69290ab3 | 138 | segmentsSha256Url: segmentsSha256UrlObject.href, |
764b1a14 | 139 | |
69290ab3 C |
140 | p2pMediaLoaderInfohashes: VideoStreamingPlaylistModel.buildP2PMediaLoaderInfoHashes(playlistUrlObject.href, files), |
141 | p2pMediaLoaderPeerVersion: P2P_MEDIA_LOADER_PEER_VERSION, | |
142 | videoId: video.id, | |
08a47c75 | 143 | |
69290ab3 C |
144 | tagAPObject: playlistUrlObject.tag |
145 | } | |
146 | ||
147 | attributes.push(attribute) | |
148 | } | |
149 | ||
150 | return attributes | |
151 | } | |
152 | ||
08a47c75 C |
153 | function getLiveAttributesFromObject (video: MVideoId, videoObject: VideoObject) { |
154 | return { | |
155 | saveReplay: videoObject.liveSaveReplay, | |
156 | permanentLive: videoObject.permanentLive, | |
157 | videoId: video.id | |
158 | } | |
159 | } | |
160 | ||
161 | function getCaptionAttributesFromObject (video: MVideoId, videoObject: VideoObject) { | |
162 | return videoObject.subtitleLanguage.map(c => ({ | |
163 | videoId: video.id, | |
164 | filename: VideoCaptionModel.generateCaptionName(c.identifier), | |
165 | language: c.identifier, | |
166 | fileUrl: c.url | |
167 | })) | |
168 | } | |
169 | ||
170 | function getVideoAttributesFromObject (videoChannel: MChannelId, videoObject: VideoObject, to: string[] = []) { | |
69290ab3 C |
171 | const privacy = to.includes(ACTIVITY_PUB.PUBLIC) |
172 | ? VideoPrivacy.PUBLIC | |
173 | : VideoPrivacy.UNLISTED | |
174 | ||
175 | const duration = videoObject.duration.replace(/[^\d]+/, '') | |
176 | const language = videoObject.language?.identifier | |
177 | ||
178 | const category = videoObject.category | |
179 | ? parseInt(videoObject.category.identifier, 10) | |
180 | : undefined | |
181 | ||
182 | const licence = videoObject.licence | |
183 | ? parseInt(videoObject.licence.identifier, 10) | |
184 | : undefined | |
185 | ||
186 | const description = videoObject.content || null | |
187 | const support = videoObject.support || null | |
188 | ||
189 | return { | |
190 | name: videoObject.name, | |
191 | uuid: videoObject.uuid, | |
192 | url: videoObject.id, | |
193 | category, | |
194 | licence, | |
195 | language, | |
196 | description, | |
197 | support, | |
198 | nsfw: videoObject.sensitive, | |
199 | commentsEnabled: videoObject.commentsEnabled, | |
200 | downloadEnabled: videoObject.downloadEnabled, | |
201 | waitTranscoding: videoObject.waitTranscoding, | |
202 | isLive: videoObject.isLiveBroadcast, | |
203 | state: videoObject.state, | |
204 | channelId: videoChannel.id, | |
205 | duration: parseInt(duration, 10), | |
206 | createdAt: new Date(videoObject.published), | |
207 | publishedAt: new Date(videoObject.published), | |
208 | ||
209 | originallyPublishedAt: videoObject.originallyPublishedAt | |
210 | ? new Date(videoObject.originallyPublishedAt) | |
211 | : null, | |
212 | ||
213 | updatedAt: new Date(videoObject.updated), | |
214 | views: videoObject.views, | |
215 | likes: 0, | |
216 | dislikes: 0, | |
217 | remote: true, | |
218 | privacy | |
219 | } | |
220 | } | |
221 | ||
222 | // --------------------------------------------------------------------------- | |
223 | ||
224 | export { | |
225 | getThumbnailFromIcons, | |
226 | getPreviewFromIcons, | |
227 | ||
228 | getTagsFromObject, | |
229 | ||
08a47c75 C |
230 | getFileAttributesFromUrl, |
231 | getStreamingPlaylistAttributesFromObject, | |
232 | ||
233 | getLiveAttributesFromObject, | |
234 | getCaptionAttributesFromObject, | |
69290ab3 | 235 | |
08a47c75 | 236 | getVideoAttributesFromObject |
69290ab3 C |
237 | } |
238 | ||
239 | // --------------------------------------------------------------------------- | |
240 | ||
241 | function isAPVideoUrlObject (url: any): url is ActivityVideoUrlObject { | |
242 | const urlMediaType = url.mediaType | |
243 | ||
244 | return MIMETYPES.VIDEO.MIMETYPE_EXT[urlMediaType] && urlMediaType.startsWith('video/') | |
245 | } | |
246 | ||
247 | function isAPStreamingPlaylistUrlObject (url: any): url is ActivityPlaylistUrlObject { | |
248 | return url && url.mediaType === 'application/x-mpegURL' | |
249 | } | |
250 | ||
251 | function isAPPlaylistSegmentHashesUrlObject (tag: any): tag is ActivityPlaylistSegmentHashesObject { | |
252 | return tag && tag.name === 'sha256' && tag.type === 'Link' && tag.mediaType === 'application/json' | |
253 | } | |
254 | ||
255 | function isAPMagnetUrlObject (url: any): url is ActivityMagnetUrlObject { | |
256 | return url && url.mediaType === 'application/x-bittorrent;x-scheme-handler/magnet' | |
257 | } | |
258 | ||
259 | function isAPHashTagObject (url: any): url is ActivityHashTagObject { | |
260 | return url && url.type === 'Hashtag' | |
261 | } |