aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/video/video-format-utils.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-09-18 11:02:51 +0200
committerChocobozzz <me@florianbigard.com>2018-09-19 09:54:35 +0200
commit098eb37797fdadd4adf660b76867da68061fd588 (patch)
tree8d7b78b31d5186eb54bfc868e55fbb6540317e4a /server/models/video/video-format-utils.ts
parentdf182b373fc49f20188d531494e1bff1a9ad247e (diff)
downloadPeerTube-098eb37797fdadd4adf660b76867da68061fd588.tar.gz
PeerTube-098eb37797fdadd4adf660b76867da68061fd588.tar.zst
PeerTube-098eb37797fdadd4adf660b76867da68061fd588.zip
Reduce video.ts file size by moving some methods in other files
Diffstat (limited to 'server/models/video/video-format-utils.ts')
-rw-r--r--server/models/video/video-format-utils.ts295
1 files changed, 295 insertions, 0 deletions
diff --git a/server/models/video/video-format-utils.ts b/server/models/video/video-format-utils.ts
new file mode 100644
index 000000000..fae38507b
--- /dev/null
+++ b/server/models/video/video-format-utils.ts
@@ -0,0 +1,295 @@
1import { Video, VideoDetails, VideoFile } from '../../../shared/models/videos'
2import { VideoModel } from './video'
3import { VideoFileModel } from './video-file'
4import { ActivityUrlObject, VideoTorrentObject } from '../../../shared/models/activitypub/objects'
5import { CONFIG, THUMBNAILS_SIZE, VIDEO_EXT_MIMETYPE } from '../../initializers'
6import { VideoCaptionModel } from './video-caption'
7import {
8 getVideoCommentsActivityPubUrl,
9 getVideoDislikesActivityPubUrl,
10 getVideoLikesActivityPubUrl,
11 getVideoSharesActivityPubUrl
12} from '../../lib/activitypub'
13
14export type VideoFormattingJSONOptions = {
15 additionalAttributes: {
16 state?: boolean,
17 waitTranscoding?: boolean,
18 scheduledUpdate?: boolean,
19 blacklistInfo?: boolean
20 }
21}
22function videoModelToFormattedJSON (video: VideoModel, options?: VideoFormattingJSONOptions): Video {
23 const formattedAccount = video.VideoChannel.Account.toFormattedJSON()
24 const formattedVideoChannel = video.VideoChannel.toFormattedJSON()
25
26 const videoObject: Video = {
27 id: video.id,
28 uuid: video.uuid,
29 name: video.name,
30 category: {
31 id: video.category,
32 label: VideoModel.getCategoryLabel(video.category)
33 },
34 licence: {
35 id: video.licence,
36 label: VideoModel.getLicenceLabel(video.licence)
37 },
38 language: {
39 id: video.language,
40 label: VideoModel.getLanguageLabel(video.language)
41 },
42 privacy: {
43 id: video.privacy,
44 label: VideoModel.getPrivacyLabel(video.privacy)
45 },
46 nsfw: video.nsfw,
47 description: video.getTruncatedDescription(),
48 isLocal: video.isOwned(),
49 duration: video.duration,
50 views: video.views,
51 likes: video.likes,
52 dislikes: video.dislikes,
53 thumbnailPath: video.getThumbnailStaticPath(),
54 previewPath: video.getPreviewStaticPath(),
55 embedPath: video.getEmbedStaticPath(),
56 createdAt: video.createdAt,
57 updatedAt: video.updatedAt,
58 publishedAt: video.publishedAt,
59 account: {
60 id: formattedAccount.id,
61 uuid: formattedAccount.uuid,
62 name: formattedAccount.name,
63 displayName: formattedAccount.displayName,
64 url: formattedAccount.url,
65 host: formattedAccount.host,
66 avatar: formattedAccount.avatar
67 },
68 channel: {
69 id: formattedVideoChannel.id,
70 uuid: formattedVideoChannel.uuid,
71 name: formattedVideoChannel.name,
72 displayName: formattedVideoChannel.displayName,
73 url: formattedVideoChannel.url,
74 host: formattedVideoChannel.host,
75 avatar: formattedVideoChannel.avatar
76 }
77 }
78
79 if (options) {
80 if (options.additionalAttributes.state === true) {
81 videoObject.state = {
82 id: video.state,
83 label: VideoModel.getStateLabel(video.state)
84 }
85 }
86
87 if (options.additionalAttributes.waitTranscoding === true) {
88 videoObject.waitTranscoding = video.waitTranscoding
89 }
90
91 if (options.additionalAttributes.scheduledUpdate === true && video.ScheduleVideoUpdate) {
92 videoObject.scheduledUpdate = {
93 updateAt: video.ScheduleVideoUpdate.updateAt,
94 privacy: video.ScheduleVideoUpdate.privacy || undefined
95 }
96 }
97
98 if (options.additionalAttributes.blacklistInfo === true) {
99 videoObject.blacklisted = !!video.VideoBlacklist
100 videoObject.blacklistedReason = video.VideoBlacklist ? video.VideoBlacklist.reason : null
101 }
102 }
103
104 return videoObject
105}
106
107function videoModelToFormattedDetailsJSON (video: VideoModel): VideoDetails {
108 const formattedJson = video.toFormattedJSON({
109 additionalAttributes: {
110 scheduledUpdate: true,
111 blacklistInfo: true
112 }
113 })
114
115 const detailsJson = {
116 support: video.support,
117 descriptionPath: video.getDescriptionPath(),
118 channel: video.VideoChannel.toFormattedJSON(),
119 account: video.VideoChannel.Account.toFormattedJSON(),
120 tags: video.Tags.map(t => t.name),
121 commentsEnabled: video.commentsEnabled,
122 waitTranscoding: video.waitTranscoding,
123 state: {
124 id: video.state,
125 label: VideoModel.getStateLabel(video.state)
126 },
127 files: []
128 }
129
130 // Format and sort video files
131 detailsJson.files = videoFilesModelToFormattedJSON(video, video.VideoFiles)
132
133 return Object.assign(formattedJson, detailsJson)
134}
135
136function videoFilesModelToFormattedJSON (video: VideoModel, videoFiles: VideoFileModel[]): VideoFile[] {
137 const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
138
139 return videoFiles
140 .map(videoFile => {
141 let resolutionLabel = videoFile.resolution + 'p'
142
143 return {
144 resolution: {
145 id: videoFile.resolution,
146 label: resolutionLabel
147 },
148 magnetUri: video.generateMagnetUri(videoFile, baseUrlHttp, baseUrlWs),
149 size: videoFile.size,
150 fps: videoFile.fps,
151 torrentUrl: video.getTorrentUrl(videoFile, baseUrlHttp),
152 torrentDownloadUrl: video.getTorrentDownloadUrl(videoFile, baseUrlHttp),
153 fileUrl: video.getVideoFileUrl(videoFile, baseUrlHttp),
154 fileDownloadUrl: video.getVideoFileDownloadUrl(videoFile, baseUrlHttp)
155 } as VideoFile
156 })
157 .sort((a, b) => {
158 if (a.resolution.id < b.resolution.id) return 1
159 if (a.resolution.id === b.resolution.id) return 0
160 return -1
161 })
162}
163
164function videoModelToActivityPubObject (video: VideoModel): VideoTorrentObject {
165 const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
166 if (!video.Tags) video.Tags = []
167
168 const tag = video.Tags.map(t => ({
169 type: 'Hashtag' as 'Hashtag',
170 name: t.name
171 }))
172
173 let language
174 if (video.language) {
175 language = {
176 identifier: video.language,
177 name: VideoModel.getLanguageLabel(video.language)
178 }
179 }
180
181 let category
182 if (video.category) {
183 category = {
184 identifier: video.category + '',
185 name: VideoModel.getCategoryLabel(video.category)
186 }
187 }
188
189 let licence
190 if (video.licence) {
191 licence = {
192 identifier: video.licence + '',
193 name: VideoModel.getLicenceLabel(video.licence)
194 }
195 }
196
197 const url: ActivityUrlObject[] = []
198 for (const file of video.VideoFiles) {
199 url.push({
200 type: 'Link',
201 mimeType: VIDEO_EXT_MIMETYPE[ file.extname ] as any,
202 href: video.getVideoFileUrl(file, baseUrlHttp),
203 height: file.resolution,
204 size: file.size,
205 fps: file.fps
206 })
207
208 url.push({
209 type: 'Link',
210 mimeType: 'application/x-bittorrent' as 'application/x-bittorrent',
211 href: video.getTorrentUrl(file, baseUrlHttp),
212 height: file.resolution
213 })
214
215 url.push({
216 type: 'Link',
217 mimeType: 'application/x-bittorrent;x-scheme-handler/magnet' as 'application/x-bittorrent;x-scheme-handler/magnet',
218 href: video.generateMagnetUri(file, baseUrlHttp, baseUrlWs),
219 height: file.resolution
220 })
221 }
222
223 // Add video url too
224 url.push({
225 type: 'Link',
226 mimeType: 'text/html',
227 href: CONFIG.WEBSERVER.URL + '/videos/watch/' + video.uuid
228 })
229
230 const subtitleLanguage = []
231 for (const caption of video.VideoCaptions) {
232 subtitleLanguage.push({
233 identifier: caption.language,
234 name: VideoCaptionModel.getLanguageLabel(caption.language)
235 })
236 }
237
238 return {
239 type: 'Video' as 'Video',
240 id: video.url,
241 name: video.name,
242 duration: getActivityStreamDuration(video.duration),
243 uuid: video.uuid,
244 tag,
245 category,
246 licence,
247 language,
248 views: video.views,
249 sensitive: video.nsfw,
250 waitTranscoding: video.waitTranscoding,
251 state: video.state,
252 commentsEnabled: video.commentsEnabled,
253 published: video.publishedAt.toISOString(),
254 updated: video.updatedAt.toISOString(),
255 mediaType: 'text/markdown',
256 content: video.getTruncatedDescription(),
257 support: video.support,
258 subtitleLanguage,
259 icon: {
260 type: 'Image',
261 url: video.getThumbnailUrl(baseUrlHttp),
262 mediaType: 'image/jpeg',
263 width: THUMBNAILS_SIZE.width,
264 height: THUMBNAILS_SIZE.height
265 },
266 url,
267 likes: getVideoLikesActivityPubUrl(video),
268 dislikes: getVideoDislikesActivityPubUrl(video),
269 shares: getVideoSharesActivityPubUrl(video),
270 comments: getVideoCommentsActivityPubUrl(video),
271 attributedTo: [
272 {
273 type: 'Person',
274 id: video.VideoChannel.Account.Actor.url
275 },
276 {
277 type: 'Group',
278 id: video.VideoChannel.Actor.url
279 }
280 ]
281 }
282}
283
284function getActivityStreamDuration (duration: number) {
285 // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration
286 return 'PT' + duration + 'S'
287}
288
289export {
290 videoModelToFormattedJSON,
291 videoModelToFormattedDetailsJSON,
292 videoFilesModelToFormattedJSON,
293 videoModelToActivityPubObject,
294 getActivityStreamDuration
295}