]>
Commit | Line | Data |
---|---|---|
53abc4c2 | 1 | import { map, maxBy, truncate } from 'lodash' |
571389d4 | 2 | import * as magnetUtil from 'magnet-uri' |
4d4e5cd4 | 3 | import * as parseTorrent from 'parse-torrent' |
65fcc311 | 4 | import { join } from 'path' |
571389d4 | 5 | import * as safeBuffer from 'safe-buffer' |
e02643f3 | 6 | import * as Sequelize from 'sequelize' |
571389d4 C |
7 | import { VideoPrivacy, VideoResolution } from '../../../shared' |
8 | import { VideoTorrentObject } from '../../../shared/models/activitypub/objects/video-torrent-object' | |
65fcc311 | 9 | import { |
571389d4 C |
10 | createTorrentPromise, |
11 | generateImageFromVideoFile, | |
12 | getActivityPubUrl, | |
13 | getVideoFileHeight, | |
65fcc311 | 14 | isVideoCategoryValid, |
65fcc311 | 15 | isVideoDescriptionValid, |
6fcd19ba | 16 | isVideoDurationValid, |
571389d4 C |
17 | isVideoLanguageValid, |
18 | isVideoLicenceValid, | |
19 | isVideoNameValid, | |
20 | isVideoNSFWValid, | |
fd45e8f4 | 21 | isVideoPrivacyValid, |
571389d4 | 22 | logger, |
6fcd19ba | 23 | renamePromise, |
14d3270f | 24 | statPromise, |
14d3270f | 25 | transcode, |
571389d4 C |
26 | unlinkPromise, |
27 | writeFilePromise | |
74889a71 | 28 | } from '../../helpers' |
65fcc311 | 29 | import { |
571389d4 | 30 | API_VERSION, |
65fcc311 | 31 | CONFIG, |
571389d4 C |
32 | CONSTRAINTS_FIELDS, |
33 | PREVIEWS_SIZE, | |
65fcc311 C |
34 | REMOTE_SCHEME, |
35 | STATIC_PATHS, | |
571389d4 | 36 | THUMBNAILS_SIZE, |
65fcc311 | 37 | VIDEO_CATEGORIES, |
65fcc311 | 38 | VIDEO_LANGUAGES, |
571389d4 | 39 | VIDEO_LICENCES, |
fd45e8f4 | 40 | VIDEO_PRIVACIES |
74889a71 | 41 | } from '../../initializers' |
aaf61f38 | 42 | |
74889a71 | 43 | import { addMethodsToModel, getSort } from '../utils' |
e02643f3 | 44 | |
571389d4 C |
45 | import { TagInstance } from './tag-interface' |
46 | import { VideoFileInstance, VideoFileModel } from './video-file-interface' | |
47 | import { VideoAttributes, VideoInstance, VideoMethods } from './video-interface' | |
7a7724e6 | 48 | import { sendDeleteVideo } from '../../lib/activitypub/send-request' |
e34c85e5 | 49 | import { isVideoUrlValid } from '../../helpers/custom-validators/videos' |
571389d4 C |
50 | |
51 | const Buffer = safeBuffer.Buffer | |
e02643f3 C |
52 | |
53 | let Video: Sequelize.Model<VideoInstance, VideoAttributes> | |
40298b02 | 54 | let getOriginalFile: VideoMethods.GetOriginalFile |
e02643f3 C |
55 | let getVideoFilename: VideoMethods.GetVideoFilename |
56 | let getThumbnailName: VideoMethods.GetThumbnailName | |
d8755eed | 57 | let getThumbnailPath: VideoMethods.GetThumbnailPath |
e02643f3 | 58 | let getPreviewName: VideoMethods.GetPreviewName |
d8755eed | 59 | let getPreviewPath: VideoMethods.GetPreviewPath |
93e1258c | 60 | let getTorrentFileName: VideoMethods.GetTorrentFileName |
e02643f3 | 61 | let isOwned: VideoMethods.IsOwned |
0aef76c4 | 62 | let toFormattedJSON: VideoMethods.ToFormattedJSON |
72c7248b | 63 | let toFormattedDetailsJSON: VideoMethods.ToFormattedDetailsJSON |
e4f97bab | 64 | let toActivityPubObject: VideoMethods.ToActivityPubObject |
40298b02 C |
65 | let optimizeOriginalVideofile: VideoMethods.OptimizeOriginalVideofile |
66 | let transcodeOriginalVideofile: VideoMethods.TranscodeOriginalVideofile | |
93e1258c C |
67 | let createPreview: VideoMethods.CreatePreview |
68 | let createThumbnail: VideoMethods.CreateThumbnail | |
69 | let getVideoFilePath: VideoMethods.GetVideoFilePath | |
70 | let createTorrentAndSetInfoHash: VideoMethods.CreateTorrentAndSetInfoHash | |
40298b02 | 71 | let getOriginalFileHeight: VideoMethods.GetOriginalFileHeight |
d8755eed | 72 | let getEmbedPath: VideoMethods.GetEmbedPath |
9567011b C |
73 | let getDescriptionPath: VideoMethods.GetDescriptionPath |
74 | let getTruncatedDescription: VideoMethods.GetTruncatedDescription | |
e4f97bab C |
75 | let getCategoryLabel: VideoMethods.GetCategoryLabel |
76 | let getLicenceLabel: VideoMethods.GetLicenceLabel | |
77 | let getLanguageLabel: VideoMethods.GetLanguageLabel | |
e02643f3 C |
78 | |
79 | let generateThumbnailFromData: VideoMethods.GenerateThumbnailFromData | |
e02643f3 C |
80 | let list: VideoMethods.List |
81 | let listForApi: VideoMethods.ListForApi | |
fd45e8f4 | 82 | let listUserVideosForApi: VideoMethods.ListUserVideosForApi |
0a6658fd | 83 | let loadByHostAndUUID: VideoMethods.LoadByHostAndUUID |
e4f97bab C |
84 | let listOwnedAndPopulateAccountAndTags: VideoMethods.ListOwnedAndPopulateAccountAndTags |
85 | let listOwnedByAccount: VideoMethods.ListOwnedByAccount | |
e02643f3 | 86 | let load: VideoMethods.Load |
0a6658fd | 87 | let loadByUUID: VideoMethods.LoadByUUID |
0d0e8dd0 | 88 | let loadByUUIDOrURL: VideoMethods.LoadByUUIDOrURL |
a041b171 | 89 | let loadLocalVideoByUUID: VideoMethods.LoadLocalVideoByUUID |
e4f97bab | 90 | let loadAndPopulateAccount: VideoMethods.LoadAndPopulateAccount |
60862425 C |
91 | let loadAndPopulateAccountAndServerAndTags: VideoMethods.LoadAndPopulateAccountAndServerAndTags |
92 | let loadByUUIDAndPopulateAccountAndServerAndTags: VideoMethods.LoadByUUIDAndPopulateAccountAndServerAndTags | |
93 | let searchAndPopulateAccountAndServerAndTags: VideoMethods.SearchAndPopulateAccountAndServerAndTags | |
93e1258c C |
94 | let removeThumbnail: VideoMethods.RemoveThumbnail |
95 | let removePreview: VideoMethods.RemovePreview | |
96 | let removeFile: VideoMethods.RemoveFile | |
97 | let removeTorrent: VideoMethods.RemoveTorrent | |
e02643f3 | 98 | |
127944aa C |
99 | export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { |
100 | Video = sequelize.define<VideoInstance, VideoAttributes>('Video', | |
feb4bdfd | 101 | { |
0a6658fd | 102 | uuid: { |
feb4bdfd C |
103 | type: DataTypes.UUID, |
104 | defaultValue: DataTypes.UUIDV4, | |
0a6658fd | 105 | allowNull: false, |
67bf9b96 C |
106 | validate: { |
107 | isUUID: 4 | |
108 | } | |
aaf61f38 | 109 | }, |
feb4bdfd | 110 | name: { |
67bf9b96 C |
111 | type: DataTypes.STRING, |
112 | allowNull: false, | |
113 | validate: { | |
075f16ca | 114 | nameValid: value => { |
65fcc311 | 115 | const res = isVideoNameValid(value) |
67bf9b96 C |
116 | if (res === false) throw new Error('Video name is not valid.') |
117 | } | |
118 | } | |
6a94a109 | 119 | }, |
6e07c3de C |
120 | category: { |
121 | type: DataTypes.INTEGER, | |
122 | allowNull: false, | |
123 | validate: { | |
075f16ca | 124 | categoryValid: value => { |
65fcc311 | 125 | const res = isVideoCategoryValid(value) |
6e07c3de C |
126 | if (res === false) throw new Error('Video category is not valid.') |
127 | } | |
128 | } | |
129 | }, | |
6f0c39e2 C |
130 | licence: { |
131 | type: DataTypes.INTEGER, | |
132 | allowNull: false, | |
3092476e | 133 | defaultValue: null, |
6f0c39e2 | 134 | validate: { |
075f16ca | 135 | licenceValid: value => { |
65fcc311 | 136 | const res = isVideoLicenceValid(value) |
6f0c39e2 C |
137 | if (res === false) throw new Error('Video licence is not valid.') |
138 | } | |
139 | } | |
140 | }, | |
3092476e C |
141 | language: { |
142 | type: DataTypes.INTEGER, | |
143 | allowNull: true, | |
144 | validate: { | |
075f16ca | 145 | languageValid: value => { |
65fcc311 | 146 | const res = isVideoLanguageValid(value) |
3092476e C |
147 | if (res === false) throw new Error('Video language is not valid.') |
148 | } | |
149 | } | |
150 | }, | |
fd45e8f4 C |
151 | privacy: { |
152 | type: DataTypes.INTEGER, | |
153 | allowNull: false, | |
154 | validate: { | |
155 | privacyValid: value => { | |
156 | const res = isVideoPrivacyValid(value) | |
157 | if (res === false) throw new Error('Video privacy is not valid.') | |
158 | } | |
159 | } | |
160 | }, | |
31b59b47 C |
161 | nsfw: { |
162 | type: DataTypes.BOOLEAN, | |
163 | allowNull: false, | |
164 | validate: { | |
075f16ca | 165 | nsfwValid: value => { |
65fcc311 | 166 | const res = isVideoNSFWValid(value) |
31b59b47 C |
167 | if (res === false) throw new Error('Video nsfw attribute is not valid.') |
168 | } | |
169 | } | |
170 | }, | |
feb4bdfd | 171 | description: { |
9567011b | 172 | type: DataTypes.STRING(CONSTRAINTS_FIELDS.VIDEOS.DESCRIPTION.max), |
67bf9b96 C |
173 | allowNull: false, |
174 | validate: { | |
075f16ca | 175 | descriptionValid: value => { |
65fcc311 | 176 | const res = isVideoDescriptionValid(value) |
67bf9b96 C |
177 | if (res === false) throw new Error('Video description is not valid.') |
178 | } | |
179 | } | |
feb4bdfd | 180 | }, |
feb4bdfd | 181 | duration: { |
67bf9b96 C |
182 | type: DataTypes.INTEGER, |
183 | allowNull: false, | |
184 | validate: { | |
075f16ca | 185 | durationValid: value => { |
65fcc311 | 186 | const res = isVideoDurationValid(value) |
67bf9b96 C |
187 | if (res === false) throw new Error('Video duration is not valid.') |
188 | } | |
189 | } | |
9e167724 C |
190 | }, |
191 | views: { | |
192 | type: DataTypes.INTEGER, | |
193 | allowNull: false, | |
194 | defaultValue: 0, | |
195 | validate: { | |
196 | min: 0, | |
197 | isInt: true | |
198 | } | |
d38b8281 C |
199 | }, |
200 | likes: { | |
201 | type: DataTypes.INTEGER, | |
202 | allowNull: false, | |
203 | defaultValue: 0, | |
204 | validate: { | |
205 | min: 0, | |
206 | isInt: true | |
207 | } | |
208 | }, | |
209 | dislikes: { | |
210 | type: DataTypes.INTEGER, | |
211 | allowNull: false, | |
212 | defaultValue: 0, | |
213 | validate: { | |
214 | min: 0, | |
215 | isInt: true | |
216 | } | |
0a6658fd C |
217 | }, |
218 | remote: { | |
219 | type: DataTypes.BOOLEAN, | |
220 | allowNull: false, | |
221 | defaultValue: false | |
e4f97bab C |
222 | }, |
223 | url: { | |
e34c85e5 | 224 | type: DataTypes.STRING(CONSTRAINTS_FIELDS.VIDEOS.URL.max), |
e4f97bab C |
225 | allowNull: false, |
226 | validate: { | |
e34c85e5 C |
227 | urlValid: value => { |
228 | const res = isVideoUrlValid(value) | |
229 | if (res === false) throw new Error('Video URL is not valid.') | |
230 | } | |
e4f97bab | 231 | } |
aaf61f38 | 232 | } |
feb4bdfd C |
233 | }, |
234 | { | |
319d072e | 235 | indexes: [ |
319d072e C |
236 | { |
237 | fields: [ 'name' ] | |
238 | }, | |
239 | { | |
240 | fields: [ 'createdAt' ] | |
241 | }, | |
242 | { | |
243 | fields: [ 'duration' ] | |
244 | }, | |
9e167724 C |
245 | { |
246 | fields: [ 'views' ] | |
d38b8281 C |
247 | }, |
248 | { | |
249 | fields: [ 'likes' ] | |
0a6658fd C |
250 | }, |
251 | { | |
252 | fields: [ 'uuid' ] | |
72c7248b C |
253 | }, |
254 | { | |
255 | fields: [ 'channelId' ] | |
319d072e C |
256 | } |
257 | ], | |
feb4bdfd | 258 | hooks: { |
feb4bdfd C |
259 | afterDestroy |
260 | } | |
261 | } | |
262 | ) | |
aaf61f38 | 263 | |
e02643f3 C |
264 | const classMethods = [ |
265 | associate, | |
266 | ||
267 | generateThumbnailFromData, | |
e02643f3 C |
268 | list, |
269 | listForApi, | |
fd45e8f4 | 270 | listUserVideosForApi, |
e4f97bab C |
271 | listOwnedAndPopulateAccountAndTags, |
272 | listOwnedByAccount, | |
e02643f3 | 273 | load, |
e4f97bab | 274 | loadAndPopulateAccount, |
60862425 | 275 | loadAndPopulateAccountAndServerAndTags, |
93e1258c | 276 | loadByHostAndUUID, |
0d0e8dd0 | 277 | loadByUUIDOrURL, |
93e1258c | 278 | loadByUUID, |
a041b171 | 279 | loadLocalVideoByUUID, |
60862425 C |
280 | loadByUUIDAndPopulateAccountAndServerAndTags, |
281 | searchAndPopulateAccountAndServerAndTags | |
e02643f3 C |
282 | ] |
283 | const instanceMethods = [ | |
93e1258c C |
284 | createPreview, |
285 | createThumbnail, | |
286 | createTorrentAndSetInfoHash, | |
e02643f3 | 287 | getPreviewName, |
d8755eed | 288 | getPreviewPath, |
93e1258c | 289 | getThumbnailName, |
d8755eed | 290 | getThumbnailPath, |
93e1258c C |
291 | getTorrentFileName, |
292 | getVideoFilename, | |
293 | getVideoFilePath, | |
40298b02 | 294 | getOriginalFile, |
e02643f3 | 295 | isOwned, |
93e1258c C |
296 | removeFile, |
297 | removePreview, | |
298 | removeThumbnail, | |
299 | removeTorrent, | |
e4f97bab | 300 | toActivityPubObject, |
0aef76c4 | 301 | toFormattedJSON, |
72c7248b | 302 | toFormattedDetailsJSON, |
40298b02 C |
303 | optimizeOriginalVideofile, |
304 | transcodeOriginalVideofile, | |
d8755eed | 305 | getOriginalFileHeight, |
9567011b C |
306 | getEmbedPath, |
307 | getTruncatedDescription, | |
e4f97bab C |
308 | getDescriptionPath, |
309 | getCategoryLabel, | |
310 | getLicenceLabel, | |
311 | getLanguageLabel | |
e02643f3 C |
312 | ] |
313 | addMethodsToModel(Video, classMethods, instanceMethods) | |
314 | ||
feb4bdfd C |
315 | return Video |
316 | } | |
aaf61f38 | 317 | |
aaf61f38 C |
318 | // ------------------------------ METHODS ------------------------------ |
319 | ||
feb4bdfd | 320 | function associate (models) { |
72c7248b | 321 | Video.belongsTo(models.VideoChannel, { |
feb4bdfd | 322 | foreignKey: { |
72c7248b | 323 | name: 'channelId', |
feb4bdfd C |
324 | allowNull: false |
325 | }, | |
326 | onDelete: 'cascade' | |
327 | }) | |
7920c273 | 328 | |
e02643f3 | 329 | Video.belongsToMany(models.Tag, { |
7920c273 C |
330 | foreignKey: 'videoId', |
331 | through: models.VideoTag, | |
332 | onDelete: 'cascade' | |
333 | }) | |
55fa55a9 | 334 | |
e02643f3 | 335 | Video.hasMany(models.VideoAbuse, { |
55fa55a9 C |
336 | foreignKey: { |
337 | name: 'videoId', | |
338 | allowNull: false | |
339 | }, | |
340 | onDelete: 'cascade' | |
341 | }) | |
93e1258c C |
342 | |
343 | Video.hasMany(models.VideoFile, { | |
344 | foreignKey: { | |
345 | name: 'videoId', | |
346 | allowNull: false | |
347 | }, | |
348 | onDelete: 'cascade' | |
349 | }) | |
feb4bdfd C |
350 | } |
351 | ||
911238e3 | 352 | function afterDestroy (video: VideoInstance) { |
93e1258c | 353 | const tasks = [] |
f285faa0 | 354 | |
93e1258c C |
355 | tasks.push( |
356 | video.removeThumbnail() | |
357 | ) | |
f285faa0 | 358 | |
93e1258c | 359 | if (video.isOwned()) { |
93e1258c | 360 | tasks.push( |
7a7724e6 C |
361 | video.removePreview(), |
362 | sendDeleteVideo(video, undefined) | |
93e1258c C |
363 | ) |
364 | ||
91f6f169 | 365 | // Remove physical files and torrents |
93e1258c | 366 | video.VideoFiles.forEach(file => { |
9fd54056 C |
367 | tasks.push(video.removeFile(file)) |
368 | tasks.push(video.removeTorrent(file)) | |
93e1258c | 369 | }) |
f285faa0 C |
370 | } |
371 | ||
93e1258c | 372 | return Promise.all(tasks) |
9fd54056 | 373 | .catch(err => { |
6cd44728 | 374 | logger.error('Some errors when removing files of video %s in after destroy hook.', video.uuid, err) |
9fd54056 | 375 | }) |
558d7c23 C |
376 | } |
377 | ||
40298b02 C |
378 | getOriginalFile = function (this: VideoInstance) { |
379 | if (Array.isArray(this.VideoFiles) === false) return undefined | |
380 | ||
14d3270f C |
381 | // The original file is the file that have the higher resolution |
382 | return maxBy(this.VideoFiles, file => file.resolution) | |
40298b02 C |
383 | } |
384 | ||
93e1258c | 385 | getVideoFilename = function (this: VideoInstance, videoFile: VideoFileInstance) { |
14d3270f | 386 | return this.uuid + '-' + videoFile.resolution + videoFile.extname |
f285faa0 C |
387 | } |
388 | ||
70c065d6 | 389 | getThumbnailName = function (this: VideoInstance) { |
f285faa0 | 390 | // We always have a copy of the thumbnail |
0a6658fd C |
391 | const extension = '.jpg' |
392 | return this.uuid + extension | |
558d7c23 C |
393 | } |
394 | ||
70c065d6 | 395 | getPreviewName = function (this: VideoInstance) { |
f285faa0 | 396 | const extension = '.jpg' |
0a6658fd | 397 | return this.uuid + extension |
f285faa0 C |
398 | } |
399 | ||
93e1258c | 400 | getTorrentFileName = function (this: VideoInstance, videoFile: VideoFileInstance) { |
f285faa0 | 401 | const extension = '.torrent' |
14d3270f | 402 | return this.uuid + '-' + videoFile.resolution + extension |
558d7c23 C |
403 | } |
404 | ||
70c065d6 | 405 | isOwned = function (this: VideoInstance) { |
0a6658fd | 406 | return this.remote === false |
aaf61f38 C |
407 | } |
408 | ||
93e1258c | 409 | createPreview = function (this: VideoInstance, videoFile: VideoFileInstance) { |
164174a6 C |
410 | const imageSize = PREVIEWS_SIZE.width + 'x' + PREVIEWS_SIZE.height |
411 | ||
14d3270f C |
412 | return generateImageFromVideoFile( |
413 | this.getVideoFilePath(videoFile), | |
414 | CONFIG.STORAGE.PREVIEWS_DIR, | |
164174a6 C |
415 | this.getPreviewName(), |
416 | imageSize | |
14d3270f | 417 | ) |
93e1258c C |
418 | } |
419 | ||
420 | createThumbnail = function (this: VideoInstance, videoFile: VideoFileInstance) { | |
d8755eed C |
421 | const imageSize = THUMBNAILS_SIZE.width + 'x' + THUMBNAILS_SIZE.height |
422 | ||
14d3270f C |
423 | return generateImageFromVideoFile( |
424 | this.getVideoFilePath(videoFile), | |
425 | CONFIG.STORAGE.THUMBNAILS_DIR, | |
426 | this.getThumbnailName(), | |
d8755eed | 427 | imageSize |
14d3270f | 428 | ) |
93e1258c C |
429 | } |
430 | ||
431 | getVideoFilePath = function (this: VideoInstance, videoFile: VideoFileInstance) { | |
432 | return join(CONFIG.STORAGE.VIDEOS_DIR, this.getVideoFilename(videoFile)) | |
433 | } | |
434 | ||
e4f97bab | 435 | createTorrentAndSetInfoHash = async function (this: VideoInstance, videoFile: VideoFileInstance) { |
93e1258c C |
436 | const options = { |
437 | announceList: [ | |
438 | [ CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT + '/tracker/socket' ] | |
439 | ], | |
440 | urlList: [ | |
441 | CONFIG.WEBSERVER.URL + STATIC_PATHS.WEBSEED + this.getVideoFilename(videoFile) | |
442 | ] | |
443 | } | |
444 | ||
e4f97bab | 445 | const torrent = await createTorrentPromise(this.getVideoFilePath(videoFile), options) |
fdbda9e3 | 446 | |
e4f97bab C |
447 | const filePath = join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile)) |
448 | logger.info('Creating torrent %s.', filePath) | |
93e1258c | 449 | |
e4f97bab C |
450 | await writeFilePromise(filePath, torrent) |
451 | ||
452 | const parsedTorrent = parseTorrent(torrent) | |
453 | videoFile.infoHash = parsedTorrent.infoHash | |
93e1258c C |
454 | } |
455 | ||
d8755eed C |
456 | getEmbedPath = function (this: VideoInstance) { |
457 | return '/videos/embed/' + this.uuid | |
458 | } | |
459 | ||
460 | getThumbnailPath = function (this: VideoInstance) { | |
461 | return join(STATIC_PATHS.THUMBNAILS, this.getThumbnailName()) | |
462 | } | |
463 | ||
464 | getPreviewPath = function (this: VideoInstance) { | |
465 | return join(STATIC_PATHS.PREVIEWS, this.getPreviewName()) | |
466 | } | |
467 | ||
0aef76c4 | 468 | toFormattedJSON = function (this: VideoInstance) { |
60862425 | 469 | let serverHost |
feb4bdfd | 470 | |
60862425 C |
471 | if (this.VideoChannel.Account.Server) { |
472 | serverHost = this.VideoChannel.Account.Server.host | |
feb4bdfd C |
473 | } else { |
474 | // It means it's our video | |
60862425 | 475 | serverHost = CONFIG.WEBSERVER.HOST |
feb4bdfd C |
476 | } |
477 | ||
aaf61f38 | 478 | const json = { |
feb4bdfd | 479 | id: this.id, |
0a6658fd | 480 | uuid: this.uuid, |
aaf61f38 | 481 | name: this.name, |
6e07c3de | 482 | category: this.category, |
e4f97bab | 483 | categoryLabel: this.getCategoryLabel(), |
6f0c39e2 | 484 | licence: this.licence, |
e4f97bab | 485 | licenceLabel: this.getLicenceLabel(), |
3092476e | 486 | language: this.language, |
e4f97bab | 487 | languageLabel: this.getLanguageLabel(), |
31b59b47 | 488 | nsfw: this.nsfw, |
9567011b | 489 | description: this.getTruncatedDescription(), |
60862425 | 490 | serverHost, |
aaf61f38 | 491 | isLocal: this.isOwned(), |
e4f97bab | 492 | account: this.VideoChannel.Account.name, |
72c7248b C |
493 | duration: this.duration, |
494 | views: this.views, | |
495 | likes: this.likes, | |
496 | dislikes: this.dislikes, | |
497 | tags: map<TagInstance, string>(this.Tags, 'name'), | |
498 | thumbnailPath: this.getThumbnailPath(), | |
499 | previewPath: this.getPreviewPath(), | |
500 | embedPath: this.getEmbedPath(), | |
501 | createdAt: this.createdAt, | |
502 | updatedAt: this.updatedAt | |
503 | } | |
504 | ||
505 | return json | |
506 | } | |
507 | ||
508 | toFormattedDetailsJSON = function (this: VideoInstance) { | |
9567011b | 509 | const formattedJson = this.toFormattedJSON() |
72c7248b | 510 | |
60862425 | 511 | // Maybe our server is not up to date and there are new privacy settings since our version |
fd45e8f4 C |
512 | let privacyLabel = VIDEO_PRIVACIES[this.privacy] |
513 | if (!privacyLabel) privacyLabel = 'Unknown' | |
514 | ||
9567011b | 515 | const detailsJson = { |
fd45e8f4 C |
516 | privacyLabel, |
517 | privacy: this.privacy, | |
9567011b | 518 | descriptionPath: this.getDescriptionPath(), |
72c7248b | 519 | channel: this.VideoChannel.toFormattedJSON(), |
93e1258c | 520 | files: [] |
aaf61f38 C |
521 | } |
522 | ||
aa8b6df4 | 523 | // Format and sort video files |
a96aed15 | 524 | const { baseUrlHttp, baseUrlWs } = getBaseUrls(this) |
9567011b | 525 | detailsJson.files = this.VideoFiles |
aa8b6df4 | 526 | .map(videoFile => { |
14d3270f | 527 | let resolutionLabel = videoFile.resolution + 'p' |
aa8b6df4 C |
528 | |
529 | const videoFileJson = { | |
530 | resolution: videoFile.resolution, | |
531 | resolutionLabel, | |
a96aed15 C |
532 | magnetUri: generateMagnetUri(this, videoFile, baseUrlHttp, baseUrlWs), |
533 | size: videoFile.size, | |
534 | torrentUrl: getTorrentUrl(this, videoFile, baseUrlHttp), | |
535 | fileUrl: getVideoFileUrl(this, videoFile, baseUrlHttp) | |
aa8b6df4 C |
536 | } |
537 | ||
538 | return videoFileJson | |
539 | }) | |
540 | .sort((a, b) => { | |
541 | if (a.resolution < b.resolution) return 1 | |
542 | if (a.resolution === b.resolution) return 0 | |
543 | return -1 | |
544 | }) | |
93e1258c | 545 | |
9567011b | 546 | return Object.assign(formattedJson, detailsJson) |
aaf61f38 C |
547 | } |
548 | ||
e4f97bab C |
549 | toActivityPubObject = function (this: VideoInstance) { |
550 | const { baseUrlHttp, baseUrlWs } = getBaseUrls(this) | |
aaf61f38 | 551 | |
e4f97bab | 552 | const tag = this.Tags.map(t => ({ |
571389d4 | 553 | type: 'Hashtag' as 'Hashtag', |
e4f97bab C |
554 | name: t.name |
555 | })) | |
556 | ||
557 | const url = [] | |
558 | for (const file of this.VideoFiles) { | |
559 | url.push({ | |
560 | type: 'Link', | |
efc32059 | 561 | mimeType: 'video/' + file.extname.replace('.', ''), |
e4f97bab C |
562 | url: getVideoFileUrl(this, file, baseUrlHttp), |
563 | width: file.resolution, | |
564 | size: file.size | |
565 | }) | |
aaf61f38 | 566 | |
e4f97bab C |
567 | url.push({ |
568 | type: 'Link', | |
569 | mimeType: 'application/x-bittorrent', | |
570 | url: getTorrentUrl(this, file, baseUrlHttp), | |
571 | width: file.resolution | |
93e1258c C |
572 | }) |
573 | ||
e4f97bab C |
574 | url.push({ |
575 | type: 'Link', | |
576 | mimeType: 'application/x-bittorrent;x-scheme-handler/magnet', | |
577 | url: generateMagnetUri(this, file, baseUrlHttp, baseUrlWs), | |
578 | width: file.resolution | |
579 | }) | |
580 | } | |
aaf61f38 | 581 | |
e4f97bab | 582 | const videoObject: VideoTorrentObject = { |
571389d4 | 583 | type: 'Video' as 'Video', |
0d0e8dd0 | 584 | id: getActivityPubUrl('video', this.uuid), |
7b1f49de | 585 | name: this.name, |
e4f97bab C |
586 | // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration |
587 | duration: 'PT' + this.duration + 'S', | |
588 | uuid: this.uuid, | |
589 | tag, | |
590 | category: { | |
571389d4 C |
591 | identifier: this.category + '', |
592 | name: this.getCategoryLabel() | |
e4f97bab C |
593 | }, |
594 | licence: { | |
571389d4 | 595 | identifier: this.licence + '', |
e4f97bab C |
596 | name: this.getLicenceLabel() |
597 | }, | |
598 | language: { | |
571389d4 | 599 | identifier: this.language + '', |
e4f97bab C |
600 | name: this.getLanguageLabel() |
601 | }, | |
d38b8281 | 602 | views: this.views, |
e4f97bab | 603 | nsfw: this.nsfw, |
efc32059 C |
604 | published: this.createdAt.toISOString(), |
605 | updated: this.updatedAt.toISOString(), | |
e4f97bab C |
606 | mediaType: 'text/markdown', |
607 | content: this.getTruncatedDescription(), | |
608 | icon: { | |
609 | type: 'Image', | |
610 | url: getThumbnailUrl(this, baseUrlHttp), | |
611 | mediaType: 'image/jpeg', | |
612 | width: THUMBNAILS_SIZE.width, | |
613 | height: THUMBNAILS_SIZE.height | |
614 | }, | |
615 | url | |
7b1f49de C |
616 | } |
617 | ||
e4f97bab | 618 | return videoObject |
7b1f49de C |
619 | } |
620 | ||
9567011b C |
621 | getTruncatedDescription = function (this: VideoInstance) { |
622 | const options = { | |
623 | length: CONSTRAINTS_FIELDS.VIDEOS.TRUNCATED_DESCRIPTION.max | |
624 | } | |
625 | ||
626 | return truncate(this.description, options) | |
627 | } | |
628 | ||
e4f97bab | 629 | optimizeOriginalVideofile = async function (this: VideoInstance) { |
65fcc311 | 630 | const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR |
227d02fe | 631 | const newExtname = '.mp4' |
40298b02 | 632 | const inputVideoFile = this.getOriginalFile() |
93e1258c C |
633 | const videoInputPath = join(videosDirectory, this.getVideoFilename(inputVideoFile)) |
634 | const videoOutputPath = join(videosDirectory, this.id + '-transcoded' + newExtname) | |
227d02fe | 635 | |
14d3270f C |
636 | const transcodeOptions = { |
637 | inputPath: videoInputPath, | |
638 | outputPath: videoOutputPath | |
639 | } | |
640 | ||
e4f97bab C |
641 | try { |
642 | // Could be very long! | |
643 | await transcode(transcodeOptions) | |
14d3270f | 644 | |
e4f97bab | 645 | await unlinkPromise(videoInputPath) |
14d3270f | 646 | |
e4f97bab C |
647 | // Important to do this before getVideoFilename() to take in account the new file extension |
648 | inputVideoFile.set('extname', newExtname) | |
649 | ||
650 | await renamePromise(videoOutputPath, this.getVideoFilePath(inputVideoFile)) | |
651 | const stats = await statPromise(this.getVideoFilePath(inputVideoFile)) | |
652 | ||
653 | inputVideoFile.set('size', stats.size) | |
654 | ||
655 | await this.createTorrentAndSetInfoHash(inputVideoFile) | |
656 | await inputVideoFile.save() | |
657 | ||
658 | } catch (err) { | |
659 | // Auto destruction... | |
660 | this.destroy().catch(err => logger.error('Cannot destruct video after transcoding failure.', err)) | |
661 | ||
662 | throw err | |
663 | } | |
227d02fe C |
664 | } |
665 | ||
e4f97bab | 666 | transcodeOriginalVideofile = async function (this: VideoInstance, resolution: VideoResolution) { |
40298b02 C |
667 | const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR |
668 | const extname = '.mp4' | |
669 | ||
670 | // We are sure it's x264 in mp4 because optimizeOriginalVideofile was already executed | |
671 | const videoInputPath = join(videosDirectory, this.getVideoFilename(this.getOriginalFile())) | |
672 | ||
673 | const newVideoFile = (Video['sequelize'].models.VideoFile as VideoFileModel).build({ | |
674 | resolution, | |
675 | extname, | |
676 | size: 0, | |
677 | videoId: this.id | |
678 | }) | |
679 | const videoOutputPath = join(videosDirectory, this.getVideoFilename(newVideoFile)) | |
14d3270f C |
680 | |
681 | const transcodeOptions = { | |
682 | inputPath: videoInputPath, | |
683 | outputPath: videoOutputPath, | |
684 | resolution | |
685 | } | |
14d3270f | 686 | |
e4f97bab C |
687 | await transcode(transcodeOptions) |
688 | ||
689 | const stats = await statPromise(videoOutputPath) | |
690 | ||
691 | newVideoFile.set('size', stats.size) | |
692 | ||
693 | await this.createTorrentAndSetInfoHash(newVideoFile) | |
694 | ||
695 | await newVideoFile.save() | |
696 | ||
697 | this.VideoFiles.push(newVideoFile) | |
40298b02 C |
698 | } |
699 | ||
700 | getOriginalFileHeight = function (this: VideoInstance) { | |
701 | const originalFilePath = this.getVideoFilePath(this.getOriginalFile()) | |
702 | ||
14d3270f | 703 | return getVideoFileHeight(originalFilePath) |
40298b02 C |
704 | } |
705 | ||
9567011b C |
706 | getDescriptionPath = function (this: VideoInstance) { |
707 | return `/api/${API_VERSION}/videos/${this.uuid}/description` | |
708 | } | |
709 | ||
e4f97bab C |
710 | getCategoryLabel = function (this: VideoInstance) { |
711 | let categoryLabel = VIDEO_CATEGORIES[this.category] | |
712 | ||
60862425 | 713 | // Maybe our server is not up to date and there are new categories since our version |
e4f97bab C |
714 | if (!categoryLabel) categoryLabel = 'Misc' |
715 | ||
716 | return categoryLabel | |
717 | } | |
718 | ||
719 | getLicenceLabel = function (this: VideoInstance) { | |
720 | let licenceLabel = VIDEO_LICENCES[this.licence] | |
0d0e8dd0 | 721 | |
60862425 | 722 | // Maybe our server is not up to date and there are new licences since our version |
e4f97bab C |
723 | if (!licenceLabel) licenceLabel = 'Unknown' |
724 | ||
725 | return licenceLabel | |
726 | } | |
727 | ||
728 | getLanguageLabel = function (this: VideoInstance) { | |
729 | // Language is an optional attribute | |
730 | let languageLabel = VIDEO_LANGUAGES[this.language] | |
731 | if (!languageLabel) languageLabel = 'Unknown' | |
732 | ||
733 | return languageLabel | |
734 | } | |
735 | ||
93e1258c C |
736 | removeThumbnail = function (this: VideoInstance) { |
737 | const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, this.getThumbnailName()) | |
738 | return unlinkPromise(thumbnailPath) | |
739 | } | |
740 | ||
741 | removePreview = function (this: VideoInstance) { | |
742 | // Same name than video thumbnail | |
743 | return unlinkPromise(CONFIG.STORAGE.PREVIEWS_DIR + this.getPreviewName()) | |
744 | } | |
745 | ||
746 | removeFile = function (this: VideoInstance, videoFile: VideoFileInstance) { | |
747 | const filePath = join(CONFIG.STORAGE.VIDEOS_DIR, this.getVideoFilename(videoFile)) | |
748 | return unlinkPromise(filePath) | |
749 | } | |
750 | ||
751 | removeTorrent = function (this: VideoInstance, videoFile: VideoFileInstance) { | |
b0f9f39e C |
752 | const torrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile)) |
753 | return unlinkPromise(torrentPath) | |
93e1258c C |
754 | } |
755 | ||
aaf61f38 C |
756 | // ------------------------------ STATICS ------------------------------ |
757 | ||
6fcd19ba | 758 | generateThumbnailFromData = function (video: VideoInstance, thumbnailData: string) { |
c77fa067 C |
759 | // Creating the thumbnail for a remote video |
760 | ||
761 | const thumbnailName = video.getThumbnailName() | |
65fcc311 | 762 | const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, thumbnailName) |
6fcd19ba C |
763 | return writeFilePromise(thumbnailPath, Buffer.from(thumbnailData, 'binary')).then(() => { |
764 | return thumbnailName | |
c77fa067 C |
765 | }) |
766 | } | |
767 | ||
6fcd19ba | 768 | list = function () { |
93e1258c C |
769 | const query = { |
770 | include: [ Video['sequelize'].models.VideoFile ] | |
771 | } | |
772 | ||
773 | return Video.findAll(query) | |
b769007f C |
774 | } |
775 | ||
fd45e8f4 C |
776 | listUserVideosForApi = function (userId: number, start: number, count: number, sort: string) { |
777 | const query = { | |
778 | distinct: true, | |
779 | offset: start, | |
780 | limit: count, | |
781 | order: [ getSort(sort), [ Video['sequelize'].models.Tag, 'name', 'ASC' ] ], | |
782 | include: [ | |
783 | { | |
784 | model: Video['sequelize'].models.VideoChannel, | |
785 | required: true, | |
786 | include: [ | |
787 | { | |
e4f97bab | 788 | model: Video['sequelize'].models.Account, |
fd45e8f4 C |
789 | where: { |
790 | userId | |
791 | }, | |
792 | required: true | |
793 | } | |
794 | ] | |
795 | }, | |
796 | Video['sequelize'].models.Tag | |
797 | ] | |
798 | } | |
799 | ||
800 | return Video.findAndCountAll(query).then(({ rows, count }) => { | |
801 | return { | |
802 | data: rows, | |
803 | total: count | |
804 | } | |
805 | }) | |
806 | } | |
807 | ||
6fcd19ba | 808 | listForApi = function (start: number, count: number, sort: string) { |
feb4bdfd | 809 | const query = { |
e02643f3 | 810 | distinct: true, |
feb4bdfd C |
811 | offset: start, |
812 | limit: count, | |
e02643f3 | 813 | order: [ getSort(sort), [ Video['sequelize'].models.Tag, 'name', 'ASC' ] ], |
feb4bdfd C |
814 | include: [ |
815 | { | |
72c7248b | 816 | model: Video['sequelize'].models.VideoChannel, |
8e10cf1a | 817 | required: true, |
72c7248b C |
818 | include: [ |
819 | { | |
e4f97bab | 820 | model: Video['sequelize'].models.Account, |
8e10cf1a | 821 | required: true, |
72c7248b C |
822 | include: [ |
823 | { | |
60862425 | 824 | model: Video['sequelize'].models.Server, |
72c7248b C |
825 | required: false |
826 | } | |
827 | ] | |
828 | } | |
829 | ] | |
7920c273 | 830 | }, |
fd45e8f4 | 831 | Video['sequelize'].models.Tag |
198b205c | 832 | ], |
e02643f3 | 833 | where: createBaseVideosWhere() |
feb4bdfd C |
834 | } |
835 | ||
6fcd19ba C |
836 | return Video.findAndCountAll(query).then(({ rows, count }) => { |
837 | return { | |
838 | data: rows, | |
839 | total: count | |
840 | } | |
feb4bdfd | 841 | }) |
aaf61f38 C |
842 | } |
843 | ||
72c7248b C |
844 | loadByHostAndUUID = function (fromHost: string, uuid: string, t?: Sequelize.Transaction) { |
845 | const query: Sequelize.FindOptions<VideoAttributes> = { | |
feb4bdfd | 846 | where: { |
0a6658fd | 847 | uuid |
feb4bdfd C |
848 | }, |
849 | include: [ | |
93e1258c C |
850 | { |
851 | model: Video['sequelize'].models.VideoFile | |
852 | }, | |
feb4bdfd | 853 | { |
72c7248b | 854 | model: Video['sequelize'].models.VideoChannel, |
feb4bdfd C |
855 | include: [ |
856 | { | |
e4f97bab | 857 | model: Video['sequelize'].models.Account, |
72c7248b C |
858 | include: [ |
859 | { | |
60862425 | 860 | model: Video['sequelize'].models.Server, |
72c7248b C |
861 | required: true, |
862 | where: { | |
863 | host: fromHost | |
864 | } | |
865 | } | |
866 | ] | |
feb4bdfd C |
867 | } |
868 | ] | |
869 | } | |
870 | ] | |
871 | } | |
aaf61f38 | 872 | |
72c7248b C |
873 | if (t !== undefined) query.transaction = t |
874 | ||
6fcd19ba | 875 | return Video.findOne(query) |
aaf61f38 C |
876 | } |
877 | ||
e4f97bab | 878 | listOwnedAndPopulateAccountAndTags = function () { |
feb4bdfd C |
879 | const query = { |
880 | where: { | |
0a6658fd | 881 | remote: false |
feb4bdfd | 882 | }, |
93e1258c C |
883 | include: [ |
884 | Video['sequelize'].models.VideoFile, | |
72c7248b C |
885 | { |
886 | model: Video['sequelize'].models.VideoChannel, | |
e4f97bab | 887 | include: [ Video['sequelize'].models.Account ] |
72c7248b | 888 | }, |
93e1258c C |
889 | Video['sequelize'].models.Tag |
890 | ] | |
feb4bdfd C |
891 | } |
892 | ||
6fcd19ba | 893 | return Video.findAll(query) |
aaf61f38 C |
894 | } |
895 | ||
e4f97bab | 896 | listOwnedByAccount = function (account: string) { |
feb4bdfd C |
897 | const query = { |
898 | where: { | |
0a6658fd | 899 | remote: false |
feb4bdfd C |
900 | }, |
901 | include: [ | |
93e1258c C |
902 | { |
903 | model: Video['sequelize'].models.VideoFile | |
904 | }, | |
feb4bdfd | 905 | { |
72c7248b C |
906 | model: Video['sequelize'].models.VideoChannel, |
907 | include: [ | |
908 | { | |
e4f97bab | 909 | model: Video['sequelize'].models.Account, |
72c7248b | 910 | where: { |
e4f97bab | 911 | name: account |
72c7248b C |
912 | } |
913 | } | |
914 | ] | |
feb4bdfd C |
915 | } |
916 | ] | |
917 | } | |
9bd26629 | 918 | |
6fcd19ba | 919 | return Video.findAll(query) |
aaf61f38 C |
920 | } |
921 | ||
0a6658fd | 922 | load = function (id: number) { |
6fcd19ba | 923 | return Video.findById(id) |
feb4bdfd C |
924 | } |
925 | ||
72c7248b C |
926 | loadByUUID = function (uuid: string, t?: Sequelize.Transaction) { |
927 | const query: Sequelize.FindOptions<VideoAttributes> = { | |
0a6658fd C |
928 | where: { |
929 | uuid | |
93e1258c C |
930 | }, |
931 | include: [ Video['sequelize'].models.VideoFile ] | |
a041b171 C |
932 | } |
933 | ||
934 | if (t !== undefined) query.transaction = t | |
935 | ||
936 | return Video.findOne(query) | |
937 | } | |
938 | ||
0d0e8dd0 C |
939 | loadByUUIDOrURL = function (uuid: string, url: string, t?: Sequelize.Transaction) { |
940 | const query: Sequelize.FindOptions<VideoAttributes> = { | |
941 | where: { | |
942 | [Sequelize.Op.or]: [ | |
943 | { uuid }, | |
944 | { url } | |
945 | ] | |
946 | }, | |
947 | include: [ Video['sequelize'].models.VideoFile ] | |
948 | } | |
949 | ||
950 | if (t !== undefined) query.transaction = t | |
951 | ||
952 | return Video.findOne(query) | |
953 | } | |
954 | ||
a041b171 C |
955 | loadLocalVideoByUUID = function (uuid: string, t?: Sequelize.Transaction) { |
956 | const query: Sequelize.FindOptions<VideoAttributes> = { | |
957 | where: { | |
958 | uuid, | |
959 | remote: false | |
960 | }, | |
961 | include: [ Video['sequelize'].models.VideoFile ] | |
0a6658fd | 962 | } |
72c7248b C |
963 | |
964 | if (t !== undefined) query.transaction = t | |
965 | ||
0a6658fd C |
966 | return Video.findOne(query) |
967 | } | |
968 | ||
e4f97bab | 969 | loadAndPopulateAccount = function (id: number) { |
feb4bdfd | 970 | const options = { |
72c7248b C |
971 | include: [ |
972 | Video['sequelize'].models.VideoFile, | |
973 | { | |
974 | model: Video['sequelize'].models.VideoChannel, | |
e4f97bab | 975 | include: [ Video['sequelize'].models.Account ] |
72c7248b C |
976 | } |
977 | ] | |
feb4bdfd C |
978 | } |
979 | ||
6fcd19ba | 980 | return Video.findById(id, options) |
feb4bdfd C |
981 | } |
982 | ||
60862425 | 983 | loadAndPopulateAccountAndServerAndTags = function (id: number) { |
feb4bdfd C |
984 | const options = { |
985 | include: [ | |
986 | { | |
72c7248b C |
987 | model: Video['sequelize'].models.VideoChannel, |
988 | include: [ | |
989 | { | |
e4f97bab | 990 | model: Video['sequelize'].models.Account, |
60862425 | 991 | include: [ { model: Video['sequelize'].models.Server, required: false } ] |
72c7248b C |
992 | } |
993 | ] | |
7920c273 | 994 | }, |
93e1258c C |
995 | Video['sequelize'].models.Tag, |
996 | Video['sequelize'].models.VideoFile | |
feb4bdfd C |
997 | ] |
998 | } | |
999 | ||
6fcd19ba | 1000 | return Video.findById(id, options) |
aaf61f38 C |
1001 | } |
1002 | ||
60862425 | 1003 | loadByUUIDAndPopulateAccountAndServerAndTags = function (uuid: string) { |
0a6658fd C |
1004 | const options = { |
1005 | where: { | |
1006 | uuid | |
1007 | }, | |
1008 | include: [ | |
1009 | { | |
72c7248b C |
1010 | model: Video['sequelize'].models.VideoChannel, |
1011 | include: [ | |
1012 | { | |
e4f97bab | 1013 | model: Video['sequelize'].models.Account, |
60862425 | 1014 | include: [ { model: Video['sequelize'].models.Server, required: false } ] |
72c7248b C |
1015 | } |
1016 | ] | |
0a6658fd | 1017 | }, |
93e1258c C |
1018 | Video['sequelize'].models.Tag, |
1019 | Video['sequelize'].models.VideoFile | |
0a6658fd C |
1020 | ] |
1021 | } | |
1022 | ||
1023 | return Video.findOne(options) | |
1024 | } | |
1025 | ||
60862425 C |
1026 | searchAndPopulateAccountAndServerAndTags = function (value: string, field: string, start: number, count: number, sort: string) { |
1027 | const serverInclude: Sequelize.IncludeOptions = { | |
1028 | model: Video['sequelize'].models.Server, | |
7920c273 | 1029 | required: false |
feb4bdfd | 1030 | } |
7920c273 | 1031 | |
e4f97bab C |
1032 | const accountInclude: Sequelize.IncludeOptions = { |
1033 | model: Video['sequelize'].models.Account, | |
60862425 | 1034 | include: [ serverInclude ] |
72c7248b C |
1035 | } |
1036 | ||
1037 | const videoChannelInclude: Sequelize.IncludeOptions = { | |
1038 | model: Video['sequelize'].models.VideoChannel, | |
e4f97bab | 1039 | include: [ accountInclude ], |
72c7248b | 1040 | required: true |
feb4bdfd C |
1041 | } |
1042 | ||
e6d4b0ff | 1043 | const tagInclude: Sequelize.IncludeOptions = { |
e02643f3 | 1044 | model: Video['sequelize'].models.Tag |
7920c273 C |
1045 | } |
1046 | ||
556ddc31 | 1047 | const query: Sequelize.FindOptions<VideoAttributes> = { |
e02643f3 C |
1048 | distinct: true, |
1049 | where: createBaseVideosWhere(), | |
feb4bdfd C |
1050 | offset: start, |
1051 | limit: count, | |
e02643f3 | 1052 | order: [ getSort(sort), [ Video['sequelize'].models.Tag, 'name', 'ASC' ] ] |
feb4bdfd C |
1053 | } |
1054 | ||
fd45e8f4 | 1055 | if (field === 'tags') { |
e02643f3 | 1056 | const escapedValue = Video['sequelize'].escape('%' + value + '%') |
c2962505 | 1057 | query.where['id'][Sequelize.Op.in] = Video['sequelize'].literal( |
6fcd19ba C |
1058 | `(SELECT "VideoTags"."videoId" |
1059 | FROM "Tags" | |
1060 | INNER JOIN "VideoTags" ON "Tags"."id" = "VideoTags"."tagId" | |
18c8e945 | 1061 | WHERE name ILIKE ${escapedValue} |
6fcd19ba | 1062 | )` |
198b205c | 1063 | ) |
7920c273 | 1064 | } else if (field === 'host') { |
60862425 C |
1065 | // FIXME: Include our server? (not stored in the database) |
1066 | serverInclude.where = { | |
7920c273 | 1067 | host: { |
c2962505 | 1068 | [Sequelize.Op.iLike]: '%' + value + '%' |
feb4bdfd | 1069 | } |
feb4bdfd | 1070 | } |
60862425 | 1071 | serverInclude.required = true |
e4f97bab C |
1072 | } else if (field === 'account') { |
1073 | accountInclude.where = { | |
7920c273 | 1074 | name: { |
c2962505 | 1075 | [Sequelize.Op.iLike]: '%' + value + '%' |
feb4bdfd C |
1076 | } |
1077 | } | |
aaf61f38 | 1078 | } else { |
feb4bdfd | 1079 | query.where[field] = { |
c2962505 | 1080 | [Sequelize.Op.iLike]: '%' + value + '%' |
feb4bdfd | 1081 | } |
aaf61f38 C |
1082 | } |
1083 | ||
7920c273 | 1084 | query.include = [ |
fd45e8f4 | 1085 | videoChannelInclude, tagInclude |
7920c273 C |
1086 | ] |
1087 | ||
6fcd19ba C |
1088 | return Video.findAndCountAll(query).then(({ rows, count }) => { |
1089 | return { | |
1090 | data: rows, | |
1091 | total: count | |
1092 | } | |
feb4bdfd | 1093 | }) |
aaf61f38 C |
1094 | } |
1095 | ||
aaf61f38 C |
1096 | // --------------------------------------------------------------------------- |
1097 | ||
15d4ee04 C |
1098 | function createBaseVideosWhere () { |
1099 | return { | |
1100 | id: { | |
c2962505 | 1101 | [Sequelize.Op.notIn]: Video['sequelize'].literal( |
15d4ee04 C |
1102 | '(SELECT "BlacklistedVideos"."videoId" FROM "BlacklistedVideos")' |
1103 | ) | |
fd45e8f4 C |
1104 | }, |
1105 | privacy: VideoPrivacy.PUBLIC | |
15d4ee04 C |
1106 | } |
1107 | } | |
a96aed15 C |
1108 | |
1109 | function getBaseUrls (video: VideoInstance) { | |
1110 | let baseUrlHttp | |
1111 | let baseUrlWs | |
1112 | ||
1113 | if (video.isOwned()) { | |
1114 | baseUrlHttp = CONFIG.WEBSERVER.URL | |
1115 | baseUrlWs = CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT | |
1116 | } else { | |
60862425 C |
1117 | baseUrlHttp = REMOTE_SCHEME.HTTP + '://' + video.VideoChannel.Account.Server.host |
1118 | baseUrlWs = REMOTE_SCHEME.WS + '://' + video.VideoChannel.Account.Server.host | |
a96aed15 C |
1119 | } |
1120 | ||
1121 | return { baseUrlHttp, baseUrlWs } | |
1122 | } | |
1123 | ||
e4f97bab C |
1124 | function getThumbnailUrl (video: VideoInstance, baseUrlHttp: string) { |
1125 | return baseUrlHttp + STATIC_PATHS.THUMBNAILS + video.getThumbnailName() | |
1126 | } | |
1127 | ||
a96aed15 C |
1128 | function getTorrentUrl (video: VideoInstance, videoFile: VideoFileInstance, baseUrlHttp: string) { |
1129 | return baseUrlHttp + STATIC_PATHS.TORRENTS + video.getTorrentFileName(videoFile) | |
1130 | } | |
1131 | ||
1132 | function getVideoFileUrl (video: VideoInstance, videoFile: VideoFileInstance, baseUrlHttp: string) { | |
1133 | return baseUrlHttp + STATIC_PATHS.WEBSEED + video.getVideoFilename(videoFile) | |
1134 | } | |
1135 | ||
1136 | function generateMagnetUri (video: VideoInstance, videoFile: VideoFileInstance, baseUrlHttp: string, baseUrlWs: string) { | |
1137 | const xs = getTorrentUrl(video, videoFile, baseUrlHttp) | |
1138 | const announce = [ baseUrlWs + '/tracker/socket', baseUrlHttp + '/tracker/announce' ] | |
1139 | const urlList = [ getVideoFileUrl(video, videoFile, baseUrlHttp) ] | |
1140 | ||
1141 | const magnetHash = { | |
1142 | xs, | |
1143 | announce, | |
1144 | urlList, | |
1145 | infoHash: videoFile.infoHash, | |
1146 | name: video.name | |
1147 | } | |
1148 | ||
1149 | return magnetUtil.encode(magnetHash) | |
1150 | } |