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