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