]>
Commit | Line | Data |
---|---|---|
1 | import validator from 'validator' | |
2 | import { logger } from '@server/helpers/logger' | |
3 | import { ActivityTrackerUrlObject, ActivityVideoFileMetadataUrlObject } from '@shared/models' | |
4 | import { LiveVideoLatencyMode, VideoState } from '../../../../shared/models/videos' | |
5 | import { ACTIVITY_PUB, CONSTRAINTS_FIELDS } from '../../../initializers/constants' | |
6 | import { peertubeTruncate } from '../../core-utils' | |
7 | import { isArray, isBooleanValid, isDateValid, isUUIDValid } from '../misc' | |
8 | import { isLiveLatencyModeValid } from '../video-lives' | |
9 | import { | |
10 | isVideoDescriptionValid, | |
11 | isVideoDurationValid, | |
12 | isVideoNameValid, | |
13 | isVideoStateValid, | |
14 | isVideoTagValid, | |
15 | isVideoViewsValid | |
16 | } from '../videos' | |
17 | import { isActivityPubUrlValid, isActivityPubVideoDurationValid, isBaseActivityValid, setValidAttributedTo } from './misc' | |
18 | ||
19 | function sanitizeAndCheckVideoTorrentUpdateActivity (activity: any) { | |
20 | return isBaseActivityValid(activity, 'Update') && | |
21 | sanitizeAndCheckVideoTorrentObject(activity.object) | |
22 | } | |
23 | ||
24 | function sanitizeAndCheckVideoTorrentObject (video: any) { | |
25 | if (!video || video.type !== 'Video') return false | |
26 | ||
27 | if (!setValidRemoteTags(video)) { | |
28 | logger.debug('Video has invalid tags', { video }) | |
29 | return false | |
30 | } | |
31 | if (!setValidRemoteVideoUrls(video)) { | |
32 | logger.debug('Video has invalid urls', { video }) | |
33 | return false | |
34 | } | |
35 | if (!setRemoteVideoContent(video)) { | |
36 | logger.debug('Video has invalid content', { video }) | |
37 | return false | |
38 | } | |
39 | if (!setValidAttributedTo(video)) { | |
40 | logger.debug('Video has invalid attributedTo', { video }) | |
41 | return false | |
42 | } | |
43 | if (!setValidRemoteCaptions(video)) { | |
44 | logger.debug('Video has invalid captions', { video }) | |
45 | return false | |
46 | } | |
47 | if (!setValidRemoteIcon(video)) { | |
48 | logger.debug('Video has invalid icons', { video }) | |
49 | return false | |
50 | } | |
51 | ||
52 | // Default attributes | |
53 | if (!isVideoStateValid(video.state)) video.state = VideoState.PUBLISHED | |
54 | if (!isBooleanValid(video.waitTranscoding)) video.waitTranscoding = false | |
55 | if (!isBooleanValid(video.downloadEnabled)) video.downloadEnabled = true | |
56 | if (!isBooleanValid(video.commentsEnabled)) video.commentsEnabled = false | |
57 | if (!isBooleanValid(video.isLiveBroadcast)) video.isLiveBroadcast = false | |
58 | if (!isBooleanValid(video.liveSaveReplay)) video.liveSaveReplay = false | |
59 | if (!isBooleanValid(video.permanentLive)) video.permanentLive = false | |
60 | if (!isLiveLatencyModeValid(video.latencyMode)) video.latencyMode = LiveVideoLatencyMode.DEFAULT | |
61 | ||
62 | return isActivityPubUrlValid(video.id) && | |
63 | isVideoNameValid(video.name) && | |
64 | isActivityPubVideoDurationValid(video.duration) && | |
65 | isVideoDurationValid(video.duration.replace(/[^0-9]+/g, '')) && | |
66 | isUUIDValid(video.uuid) && | |
67 | (!video.category || isRemoteNumberIdentifierValid(video.category)) && | |
68 | (!video.licence || isRemoteNumberIdentifierValid(video.licence)) && | |
69 | (!video.language || isRemoteStringIdentifierValid(video.language)) && | |
70 | isVideoViewsValid(video.views) && | |
71 | isBooleanValid(video.sensitive) && | |
72 | isDateValid(video.published) && | |
73 | isDateValid(video.updated) && | |
74 | (!video.originallyPublishedAt || isDateValid(video.originallyPublishedAt)) && | |
75 | (!video.content || isRemoteVideoContentValid(video.mediaType, video.content)) && | |
76 | video.attributedTo.length !== 0 | |
77 | } | |
78 | ||
79 | function isRemoteVideoUrlValid (url: any) { | |
80 | return url.type === 'Link' && | |
81 | // Video file link | |
82 | ( | |
83 | ACTIVITY_PUB.URL_MIME_TYPES.VIDEO.includes(url.mediaType) && | |
84 | isActivityPubUrlValid(url.href) && | |
85 | validator.isInt(url.height + '', { min: 0 }) && | |
86 | validator.isInt(url.size + '', { min: 0 }) && | |
87 | (!url.fps || validator.isInt(url.fps + '', { min: -1 })) | |
88 | ) || | |
89 | // Torrent link | |
90 | ( | |
91 | ACTIVITY_PUB.URL_MIME_TYPES.TORRENT.includes(url.mediaType) && | |
92 | isActivityPubUrlValid(url.href) && | |
93 | validator.isInt(url.height + '', { min: 0 }) | |
94 | ) || | |
95 | // Magnet link | |
96 | ( | |
97 | ACTIVITY_PUB.URL_MIME_TYPES.MAGNET.includes(url.mediaType) && | |
98 | validator.isLength(url.href, { min: 5 }) && | |
99 | validator.isInt(url.height + '', { min: 0 }) | |
100 | ) || | |
101 | // HLS playlist link | |
102 | ( | |
103 | (url.mediaType || url.mimeType) === 'application/x-mpegURL' && | |
104 | isActivityPubUrlValid(url.href) && | |
105 | isArray(url.tag) | |
106 | ) || | |
107 | isAPVideoTrackerUrlObject(url) || | |
108 | isAPVideoFileUrlMetadataObject(url) | |
109 | } | |
110 | ||
111 | function isAPVideoFileUrlMetadataObject (url: any): url is ActivityVideoFileMetadataUrlObject { | |
112 | return url && | |
113 | url.type === 'Link' && | |
114 | url.mediaType === 'application/json' && | |
115 | isArray(url.rel) && url.rel.includes('metadata') | |
116 | } | |
117 | ||
118 | function isAPVideoTrackerUrlObject (url: any): url is ActivityTrackerUrlObject { | |
119 | return isArray(url.rel) && | |
120 | url.rel.includes('tracker') && | |
121 | isActivityPubUrlValid(url.href) | |
122 | } | |
123 | ||
124 | // --------------------------------------------------------------------------- | |
125 | ||
126 | export { | |
127 | sanitizeAndCheckVideoTorrentUpdateActivity, | |
128 | isRemoteStringIdentifierValid, | |
129 | sanitizeAndCheckVideoTorrentObject, | |
130 | isRemoteVideoUrlValid, | |
131 | isAPVideoFileUrlMetadataObject, | |
132 | isAPVideoTrackerUrlObject | |
133 | } | |
134 | ||
135 | // --------------------------------------------------------------------------- | |
136 | ||
137 | function setValidRemoteTags (video: any) { | |
138 | if (Array.isArray(video.tag) === false) return false | |
139 | ||
140 | video.tag = video.tag.filter(t => { | |
141 | return t.type === 'Hashtag' && | |
142 | isVideoTagValid(t.name) | |
143 | }) | |
144 | ||
145 | return true | |
146 | } | |
147 | ||
148 | function setValidRemoteCaptions (video: any) { | |
149 | if (!video.subtitleLanguage) video.subtitleLanguage = [] | |
150 | ||
151 | if (Array.isArray(video.subtitleLanguage) === false) return false | |
152 | ||
153 | video.subtitleLanguage = video.subtitleLanguage.filter(caption => { | |
154 | if (!isActivityPubUrlValid(caption.url)) caption.url = null | |
155 | ||
156 | return isRemoteStringIdentifierValid(caption) | |
157 | }) | |
158 | ||
159 | return true | |
160 | } | |
161 | ||
162 | function isRemoteNumberIdentifierValid (data: any) { | |
163 | return validator.isInt(data.identifier, { min: 0 }) | |
164 | } | |
165 | ||
166 | function isRemoteStringIdentifierValid (data: any) { | |
167 | return typeof data.identifier === 'string' | |
168 | } | |
169 | ||
170 | function isRemoteVideoContentValid (mediaType: string, content: string) { | |
171 | return mediaType === 'text/markdown' && isVideoDescriptionValid(content) | |
172 | } | |
173 | ||
174 | function setValidRemoteIcon (video: any) { | |
175 | if (video.icon && !isArray(video.icon)) video.icon = [ video.icon ] | |
176 | if (!video.icon) video.icon = [] | |
177 | ||
178 | video.icon = video.icon.filter(icon => { | |
179 | return icon.type === 'Image' && | |
180 | isActivityPubUrlValid(icon.url) && | |
181 | icon.mediaType === 'image/jpeg' && | |
182 | validator.isInt(icon.width + '', { min: 0 }) && | |
183 | validator.isInt(icon.height + '', { min: 0 }) | |
184 | }) | |
185 | ||
186 | return video.icon.length !== 0 | |
187 | } | |
188 | ||
189 | function setValidRemoteVideoUrls (video: any) { | |
190 | if (Array.isArray(video.url) === false) return false | |
191 | ||
192 | video.url = video.url.filter(u => isRemoteVideoUrlValid(u)) | |
193 | ||
194 | return true | |
195 | } | |
196 | ||
197 | function setRemoteVideoContent (video: any) { | |
198 | if (video.content) { | |
199 | video.content = peertubeTruncate(video.content, { length: CONSTRAINTS_FIELDS.VIDEOS.DESCRIPTION.max }) | |
200 | } | |
201 | ||
202 | return true | |
203 | } |