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