]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/custom-validators/activitypub/videos.ts
replace fs by fs-extra to prevent EMFILE error
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / activitypub / videos.ts
CommitLineData
0d0e8dd0 1import * as validator from 'validator'
c73e83da
C
2import { ACTIVITY_PUB, CONSTRAINTS_FIELDS } from '../../../initializers'
3import { peertubeTruncate } from '../../core-utils'
47564bbe 4import { exists, isBooleanValid, isDateValid, isUUIDValid } from '../misc'
65fcc311 5import {
65fcc311 6 isVideoDurationValid,
65fcc311 7 isVideoNameValid,
2186386c 8 isVideoStateValid,
e34c85e5 9 isVideoTagValid,
8e10cf1a 10 isVideoTruncatedDescriptionValid,
8e10cf1a 11 isVideoViewsValid
65fcc311 12} from '../videos'
50d6de9c 13import { isActivityPubUrlValid, isBaseActivityValid, setValidAttributedTo } from './misc'
2186386c 14import { VideoState } from '../../../../shared/models/videos'
268eebed 15import { isVideoAbuseReasonValid } from '../video-abuses'
0d0e8dd0 16
1d6e5dfc 17function sanitizeAndCheckVideoTorrentCreateActivity (activity: any) {
50d6de9c 18 return isBaseActivityValid(activity, 'Create') &&
1d6e5dfc 19 sanitizeAndCheckVideoTorrentObject(activity.object)
0d0e8dd0
C
20}
21
1d6e5dfc 22function sanitizeAndCheckVideoTorrentUpdateActivity (activity: any) {
0d0e8dd0 23 return isBaseActivityValid(activity, 'Update') &&
1d6e5dfc 24 sanitizeAndCheckVideoTorrentObject(activity.object)
65fcc311
C
25}
26
350e31d6
C
27function isVideoTorrentDeleteActivityValid (activity: any) {
28 return isBaseActivityValid(activity, 'Delete')
29}
30
54141398
C
31function isVideoFlagValid (activity: any) {
32 return isBaseActivityValid(activity, 'Create') &&
33 activity.object.type === 'Flag' &&
34 isVideoAbuseReasonValid(activity.object.content) &&
35 isActivityPubUrlValid(activity.object.object)
36}
37
8e10cf1a
C
38function isActivityPubVideoDurationValid (value: string) {
39 // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration
40 return exists(value) &&
41 typeof value === 'string' &&
42 value.startsWith('PT') &&
43 value.endsWith('S') &&
efc32059 44 isVideoDurationValid(value.replace(/[^0-9]+/g, ''))
8e10cf1a
C
45}
46
1d6e5dfc 47function sanitizeAndCheckVideoTorrentObject (video: any) {
fbad87b0 48 if (!video || video.type !== 'Video') return false
5cf13500 49
1d6e5dfc
C
50 if (!setValidRemoteTags(video)) return false
51 if (!setValidRemoteVideoUrls(video)) return false
52 if (!setRemoteVideoTruncatedContent(video)) return false
53 if (!setValidAttributedTo(video)) return false
40e87e9e 54 if (!setValidRemoteCaptions(video)) return false
1d6e5dfc 55
2186386c
C
56 // Default attributes
57 if (!isVideoStateValid(video.state)) video.state = VideoState.PUBLISHED
58 if (!isBooleanValid(video.waitTranscoding)) video.waitTranscoding = false
59
5cf13500 60 return isActivityPubUrlValid(video.id) &&
0d0e8dd0 61 isVideoNameValid(video.name) &&
8e10cf1a 62 isActivityPubVideoDurationValid(video.duration) &&
0d0e8dd0 63 isUUIDValid(video.uuid) &&
9d3ef9fe
C
64 (!video.category || isRemoteNumberIdentifierValid(video.category)) &&
65 (!video.licence || isRemoteNumberIdentifierValid(video.licence)) &&
66 (!video.language || isRemoteStringIdentifierValid(video.language)) &&
efc32059 67 isVideoViewsValid(video.views) &&
0a67e28b 68 isBooleanValid(video.sensitive) &&
47564bbe 69 isBooleanValid(video.commentsEnabled) &&
0d0e8dd0
C
70 isDateValid(video.published) &&
71 isDateValid(video.updated) &&
f595d394 72 (!video.content || isRemoteVideoContentValid(video.mediaType, video.content)) &&
0d0e8dd0 73 isRemoteVideoIconValid(video.icon) &&
50d6de9c 74 video.url.length !== 0 &&
50d6de9c 75 video.attributedTo.length !== 0
65fcc311
C
76}
77
65fcc311
C
78// ---------------------------------------------------------------------------
79
80export {
1d6e5dfc
C
81 sanitizeAndCheckVideoTorrentCreateActivity,
82 sanitizeAndCheckVideoTorrentUpdateActivity,
d8465018 83 isVideoTorrentDeleteActivityValid,
9d3ef9fe 84 isRemoteStringIdentifierValid,
2ccaeeb3 85 isVideoFlagValid,
1d6e5dfc 86 sanitizeAndCheckVideoTorrentObject
65fcc311
C
87}
88
89// ---------------------------------------------------------------------------
90
0d0e8dd0
C
91function setValidRemoteTags (video: any) {
92 if (Array.isArray(video.tag) === false) return false
65fcc311 93
a2431b7d 94 video.tag = video.tag.filter(t => {
0d0e8dd0
C
95 return t.type === 'Hashtag' &&
96 isVideoTagValid(t.name)
97 })
72c7248b 98
0d0e8dd0 99 return true
72c7248b
C
100}
101
40e87e9e
C
102function setValidRemoteCaptions (video: any) {
103 if (!video.subtitleLanguage) video.subtitleLanguage = []
104
105 if (Array.isArray(video.subtitleLanguage) === false) return false
106
107 video.subtitleLanguage = video.subtitleLanguage.filter(caption => {
108 return isRemoteStringIdentifierValid(caption)
109 })
110
111 return true
112}
113
9d3ef9fe 114function isRemoteNumberIdentifierValid (data: any) {
0d0e8dd0 115 return validator.isInt(data.identifier, { min: 0 })
72c7248b
C
116}
117
9d3ef9fe
C
118function isRemoteStringIdentifierValid (data: any) {
119 return typeof data.identifier === 'string'
120}
121
0d0e8dd0
C
122function isRemoteVideoContentValid (mediaType: string, content: string) {
123 return mediaType === 'text/markdown' && isVideoTruncatedDescriptionValid(content)
72c7248b
C
124}
125
0d0e8dd0
C
126function isRemoteVideoIconValid (icon: any) {
127 return icon.type === 'Image' &&
a2431b7d 128 isActivityPubUrlValid(icon.url) &&
0d0e8dd0 129 icon.mediaType === 'image/jpeg' &&
efc32059
C
130 validator.isInt(icon.width + '', { min: 0 }) &&
131 validator.isInt(icon.height + '', { min: 0 })
72c7248b
C
132}
133
0d0e8dd0
C
134function setValidRemoteVideoUrls (video: any) {
135 if (Array.isArray(video.url) === false) return false
65fcc311 136
a2431b7d 137 video.url = video.url.filter(u => isRemoteVideoUrlValid(u))
65fcc311 138
0d0e8dd0 139 return true
65fcc311
C
140}
141
45cd28b6 142function setRemoteVideoTruncatedContent (video: any) {
c73e83da
C
143 if (video.content) {
144 video.content = peertubeTruncate(video.content, CONSTRAINTS_FIELDS.VIDEOS.TRUNCATED_DESCRIPTION.max)
145 }
146
147 return true
148}
149
0d0e8dd0 150function isRemoteVideoUrlValid (url: any) {
965c4b22
C
151 // FIXME: Old bug, we used the width to represent the resolution. Remove it in a few realease (currently beta.11)
152 if (url.width && !url.height) url.height = url.width
153
0d0e8dd0 154 return url.type === 'Link' &&
20494f12
C
155 (
156 ACTIVITY_PUB.URL_MIME_TYPES.VIDEO.indexOf(url.mimeType) !== -1 &&
9fb3abfd 157 isActivityPubUrlValid(url.href) &&
965c4b22 158 validator.isInt(url.height + '', { min: 0 }) &&
b2977eec
C
159 validator.isInt(url.size + '', { min: 0 }) &&
160 (!url.fps || validator.isInt(url.fps + '', { min: 0 }))
20494f12
C
161 ) ||
162 (
163 ACTIVITY_PUB.URL_MIME_TYPES.TORRENT.indexOf(url.mimeType) !== -1 &&
9fb3abfd 164 isActivityPubUrlValid(url.href) &&
965c4b22 165 validator.isInt(url.height + '', { min: 0 })
20494f12
C
166 ) ||
167 (
168 ACTIVITY_PUB.URL_MIME_TYPES.MAGNET.indexOf(url.mimeType) !== -1 &&
9fb3abfd 169 validator.isLength(url.href, { min: 5 }) &&
965c4b22 170 validator.isInt(url.height + '', { min: 0 })
20494f12 171 )
65fcc311 172}