diff options
Diffstat (limited to 'server/models/video/video.ts')
-rw-r--r-- | server/models/video/video.ts | 100 |
1 files changed, 61 insertions, 39 deletions
diff --git a/server/models/video/video.ts b/server/models/video/video.ts index 496385b35..3bb74bf6d 100644 --- a/server/models/video/video.ts +++ b/server/models/video/video.ts | |||
@@ -62,21 +62,23 @@ let generateThumbnailFromData: VideoMethods.GenerateThumbnailFromData | |||
62 | let getDurationFromFile: VideoMethods.GetDurationFromFile | 62 | let getDurationFromFile: VideoMethods.GetDurationFromFile |
63 | let list: VideoMethods.List | 63 | let list: VideoMethods.List |
64 | let listForApi: VideoMethods.ListForApi | 64 | let listForApi: VideoMethods.ListForApi |
65 | let loadByHostAndRemoteId: VideoMethods.LoadByHostAndRemoteId | 65 | let loadByHostAndUUID: VideoMethods.LoadByHostAndUUID |
66 | let listOwnedAndPopulateAuthorAndTags: VideoMethods.ListOwnedAndPopulateAuthorAndTags | 66 | let listOwnedAndPopulateAuthorAndTags: VideoMethods.ListOwnedAndPopulateAuthorAndTags |
67 | let listOwnedByAuthor: VideoMethods.ListOwnedByAuthor | 67 | let listOwnedByAuthor: VideoMethods.ListOwnedByAuthor |
68 | let load: VideoMethods.Load | 68 | let load: VideoMethods.Load |
69 | let loadByUUID: VideoMethods.LoadByUUID | ||
69 | let loadAndPopulateAuthor: VideoMethods.LoadAndPopulateAuthor | 70 | let loadAndPopulateAuthor: VideoMethods.LoadAndPopulateAuthor |
70 | let loadAndPopulateAuthorAndPodAndTags: VideoMethods.LoadAndPopulateAuthorAndPodAndTags | 71 | let loadAndPopulateAuthorAndPodAndTags: VideoMethods.LoadAndPopulateAuthorAndPodAndTags |
72 | let loadByUUIDAndPopulateAuthorAndPodAndTags: VideoMethods.LoadByUUIDAndPopulateAuthorAndPodAndTags | ||
71 | let searchAndPopulateAuthorAndPodAndTags: VideoMethods.SearchAndPopulateAuthorAndPodAndTags | 73 | let searchAndPopulateAuthorAndPodAndTags: VideoMethods.SearchAndPopulateAuthorAndPodAndTags |
72 | 74 | ||
73 | export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { | 75 | export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { |
74 | Video = sequelize.define<VideoInstance, VideoAttributes>('Video', | 76 | Video = sequelize.define<VideoInstance, VideoAttributes>('Video', |
75 | { | 77 | { |
76 | id: { | 78 | uuid: { |
77 | type: DataTypes.UUID, | 79 | type: DataTypes.UUID, |
78 | defaultValue: DataTypes.UUIDV4, | 80 | defaultValue: DataTypes.UUIDV4, |
79 | primaryKey: true, | 81 | allowNull: false, |
80 | validate: { | 82 | validate: { |
81 | isUUID: 4 | 83 | isUUID: 4 |
82 | } | 84 | } |
@@ -95,13 +97,6 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da | |||
95 | type: DataTypes.ENUM(values(CONSTRAINTS_FIELDS.VIDEOS.EXTNAME)), | 97 | type: DataTypes.ENUM(values(CONSTRAINTS_FIELDS.VIDEOS.EXTNAME)), |
96 | allowNull: false | 98 | allowNull: false |
97 | }, | 99 | }, |
98 | remoteId: { | ||
99 | type: DataTypes.UUID, | ||
100 | allowNull: true, | ||
101 | validate: { | ||
102 | isUUID: 4 | ||
103 | } | ||
104 | }, | ||
105 | category: { | 100 | category: { |
106 | type: DataTypes.INTEGER, | 101 | type: DataTypes.INTEGER, |
107 | allowNull: false, | 102 | allowNull: false, |
@@ -199,6 +194,11 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da | |||
199 | min: 0, | 194 | min: 0, |
200 | isInt: true | 195 | isInt: true |
201 | } | 196 | } |
197 | }, | ||
198 | remote: { | ||
199 | type: DataTypes.BOOLEAN, | ||
200 | allowNull: false, | ||
201 | defaultValue: false | ||
202 | } | 202 | } |
203 | }, | 203 | }, |
204 | { | 204 | { |
@@ -207,9 +207,6 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da | |||
207 | fields: [ 'authorId' ] | 207 | fields: [ 'authorId' ] |
208 | }, | 208 | }, |
209 | { | 209 | { |
210 | fields: [ 'remoteId' ] | ||
211 | }, | ||
212 | { | ||
213 | fields: [ 'name' ] | 210 | fields: [ 'name' ] |
214 | }, | 211 | }, |
215 | { | 212 | { |
@@ -226,6 +223,9 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da | |||
226 | }, | 223 | }, |
227 | { | 224 | { |
228 | fields: [ 'likes' ] | 225 | fields: [ 'likes' ] |
226 | }, | ||
227 | { | ||
228 | fields: [ 'uuid' ] | ||
229 | } | 229 | } |
230 | ], | 230 | ], |
231 | hooks: { | 231 | hooks: { |
@@ -246,9 +246,11 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da | |||
246 | listOwnedAndPopulateAuthorAndTags, | 246 | listOwnedAndPopulateAuthorAndTags, |
247 | listOwnedByAuthor, | 247 | listOwnedByAuthor, |
248 | load, | 248 | load, |
249 | loadByHostAndRemoteId, | 249 | loadByUUID, |
250 | loadByHostAndUUID, | ||
250 | loadAndPopulateAuthor, | 251 | loadAndPopulateAuthor, |
251 | loadAndPopulateAuthorAndPodAndTags, | 252 | loadAndPopulateAuthorAndPodAndTags, |
253 | loadByUUIDAndPopulateAuthorAndPodAndTags, | ||
252 | searchAndPopulateAuthorAndPodAndTags, | 254 | searchAndPopulateAuthorAndPodAndTags, |
253 | removeFromBlacklist | 255 | removeFromBlacklist |
254 | ] | 256 | ] |
@@ -289,8 +291,9 @@ function beforeCreate (video: VideoInstance, options: { transaction: Sequelize.T | |||
289 | ) | 291 | ) |
290 | 292 | ||
291 | if (CONFIG.TRANSCODING.ENABLED === true) { | 293 | if (CONFIG.TRANSCODING.ENABLED === true) { |
294 | // Put uuid because we don't have id auto incremented for now | ||
292 | const dataInput = { | 295 | const dataInput = { |
293 | id: video.id | 296 | videoUUID: video.uuid |
294 | } | 297 | } |
295 | 298 | ||
296 | tasks.push( | 299 | tasks.push( |
@@ -313,7 +316,7 @@ function afterDestroy (video: VideoInstance) { | |||
313 | 316 | ||
314 | if (video.isOwned()) { | 317 | if (video.isOwned()) { |
315 | const removeVideoToFriendsParams = { | 318 | const removeVideoToFriendsParams = { |
316 | remoteId: video.id | 319 | uuid: video.uuid |
317 | } | 320 | } |
318 | 321 | ||
319 | tasks.push( | 322 | tasks.push( |
@@ -381,34 +384,27 @@ generateMagnetUri = function (this: VideoInstance) { | |||
381 | } | 384 | } |
382 | 385 | ||
383 | getVideoFilename = function (this: VideoInstance) { | 386 | getVideoFilename = function (this: VideoInstance) { |
384 | if (this.isOwned()) return this.id + this.extname | 387 | return this.uuid + this.extname |
385 | |||
386 | return this.remoteId + this.extname | ||
387 | } | 388 | } |
388 | 389 | ||
389 | getThumbnailName = function (this: VideoInstance) { | 390 | getThumbnailName = function (this: VideoInstance) { |
390 | // We always have a copy of the thumbnail | 391 | // We always have a copy of the thumbnail |
391 | return this.id + '.jpg' | 392 | const extension = '.jpg' |
393 | return this.uuid + extension | ||
392 | } | 394 | } |
393 | 395 | ||
394 | getPreviewName = function (this: VideoInstance) { | 396 | getPreviewName = function (this: VideoInstance) { |
395 | const extension = '.jpg' | 397 | const extension = '.jpg' |
396 | 398 | return this.uuid + extension | |
397 | if (this.isOwned()) return this.id + extension | ||
398 | |||
399 | return this.remoteId + extension | ||
400 | } | 399 | } |
401 | 400 | ||
402 | getTorrentName = function (this: VideoInstance) { | 401 | getTorrentName = function (this: VideoInstance) { |
403 | const extension = '.torrent' | 402 | const extension = '.torrent' |
404 | 403 | return this.uuid + extension | |
405 | if (this.isOwned()) return this.id + extension | ||
406 | |||
407 | return this.remoteId + extension | ||
408 | } | 404 | } |
409 | 405 | ||
410 | isOwned = function (this: VideoInstance) { | 406 | isOwned = function (this: VideoInstance) { |
411 | return this.remoteId === null | 407 | return this.remote === false |
412 | } | 408 | } |
413 | 409 | ||
414 | toFormatedJSON = function (this: VideoInstance) { | 410 | toFormatedJSON = function (this: VideoInstance) { |
@@ -435,6 +431,7 @@ toFormatedJSON = function (this: VideoInstance) { | |||
435 | 431 | ||
436 | const json = { | 432 | const json = { |
437 | id: this.id, | 433 | id: this.id, |
434 | uuid: this.uuid, | ||
438 | name: this.name, | 435 | name: this.name, |
439 | category: this.category, | 436 | category: this.category, |
440 | categoryLabel, | 437 | categoryLabel, |
@@ -467,6 +464,7 @@ toAddRemoteJSON = function (this: VideoInstance) { | |||
467 | 464 | ||
468 | return readFileBufferPromise(thumbnailPath).then(thumbnailData => { | 465 | return readFileBufferPromise(thumbnailPath).then(thumbnailData => { |
469 | const remoteVideo = { | 466 | const remoteVideo = { |
467 | uuid: this.uuid, | ||
470 | name: this.name, | 468 | name: this.name, |
471 | category: this.category, | 469 | category: this.category, |
472 | licence: this.licence, | 470 | licence: this.licence, |
@@ -474,7 +472,6 @@ toAddRemoteJSON = function (this: VideoInstance) { | |||
474 | nsfw: this.nsfw, | 472 | nsfw: this.nsfw, |
475 | description: this.description, | 473 | description: this.description, |
476 | infoHash: this.infoHash, | 474 | infoHash: this.infoHash, |
477 | remoteId: this.id, | ||
478 | author: this.Author.name, | 475 | author: this.Author.name, |
479 | duration: this.duration, | 476 | duration: this.duration, |
480 | thumbnailData: thumbnailData.toString('binary'), | 477 | thumbnailData: thumbnailData.toString('binary'), |
@@ -493,6 +490,7 @@ toAddRemoteJSON = function (this: VideoInstance) { | |||
493 | 490 | ||
494 | toUpdateRemoteJSON = function (this: VideoInstance) { | 491 | toUpdateRemoteJSON = function (this: VideoInstance) { |
495 | const json = { | 492 | const json = { |
493 | uuid: this.uuid, | ||
496 | name: this.name, | 494 | name: this.name, |
497 | category: this.category, | 495 | category: this.category, |
498 | licence: this.licence, | 496 | licence: this.licence, |
@@ -500,7 +498,6 @@ toUpdateRemoteJSON = function (this: VideoInstance) { | |||
500 | nsfw: this.nsfw, | 498 | nsfw: this.nsfw, |
501 | description: this.description, | 499 | description: this.description, |
502 | infoHash: this.infoHash, | 500 | infoHash: this.infoHash, |
503 | remoteId: this.id, | ||
504 | author: this.Author.name, | 501 | author: this.Author.name, |
505 | duration: this.duration, | 502 | duration: this.duration, |
506 | tags: map<TagInstance, string>(this.Tags, 'name'), | 503 | tags: map<TagInstance, string>(this.Tags, 'name'), |
@@ -615,10 +612,10 @@ listForApi = function (start: number, count: number, sort: string) { | |||
615 | }) | 612 | }) |
616 | } | 613 | } |
617 | 614 | ||
618 | loadByHostAndRemoteId = function (fromHost: string, remoteId: string) { | 615 | loadByHostAndUUID = function (fromHost: string, uuid: string) { |
619 | const query = { | 616 | const query = { |
620 | where: { | 617 | where: { |
621 | remoteId: remoteId | 618 | uuid |
622 | }, | 619 | }, |
623 | include: [ | 620 | include: [ |
624 | { | 621 | { |
@@ -640,10 +637,9 @@ loadByHostAndRemoteId = function (fromHost: string, remoteId: string) { | |||
640 | } | 637 | } |
641 | 638 | ||
642 | listOwnedAndPopulateAuthorAndTags = function () { | 639 | listOwnedAndPopulateAuthorAndTags = function () { |
643 | // If remoteId is null this is *our* video | ||
644 | const query = { | 640 | const query = { |
645 | where: { | 641 | where: { |
646 | remoteId: null | 642 | remote: false |
647 | }, | 643 | }, |
648 | include: [ Video['sequelize'].models.Author, Video['sequelize'].models.Tag ] | 644 | include: [ Video['sequelize'].models.Author, Video['sequelize'].models.Tag ] |
649 | } | 645 | } |
@@ -654,7 +650,7 @@ listOwnedAndPopulateAuthorAndTags = function () { | |||
654 | listOwnedByAuthor = function (author: string) { | 650 | listOwnedByAuthor = function (author: string) { |
655 | const query = { | 651 | const query = { |
656 | where: { | 652 | where: { |
657 | remoteId: null | 653 | remote: false |
658 | }, | 654 | }, |
659 | include: [ | 655 | include: [ |
660 | { | 656 | { |
@@ -669,11 +665,20 @@ listOwnedByAuthor = function (author: string) { | |||
669 | return Video.findAll(query) | 665 | return Video.findAll(query) |
670 | } | 666 | } |
671 | 667 | ||
672 | load = function (id: string) { | 668 | load = function (id: number) { |
673 | return Video.findById(id) | 669 | return Video.findById(id) |
674 | } | 670 | } |
675 | 671 | ||
676 | loadAndPopulateAuthor = function (id: string) { | 672 | loadByUUID = function (uuid: string) { |
673 | const query = { | ||
674 | where: { | ||
675 | uuid | ||
676 | } | ||
677 | } | ||
678 | return Video.findOne(query) | ||
679 | } | ||
680 | |||
681 | loadAndPopulateAuthor = function (id: number) { | ||
677 | const options = { | 682 | const options = { |
678 | include: [ Video['sequelize'].models.Author ] | 683 | include: [ Video['sequelize'].models.Author ] |
679 | } | 684 | } |
@@ -681,7 +686,7 @@ loadAndPopulateAuthor = function (id: string) { | |||
681 | return Video.findById(id, options) | 686 | return Video.findById(id, options) |
682 | } | 687 | } |
683 | 688 | ||
684 | loadAndPopulateAuthorAndPodAndTags = function (id: string) { | 689 | loadAndPopulateAuthorAndPodAndTags = function (id: number) { |
685 | const options = { | 690 | const options = { |
686 | include: [ | 691 | include: [ |
687 | { | 692 | { |
@@ -695,6 +700,23 @@ loadAndPopulateAuthorAndPodAndTags = function (id: string) { | |||
695 | return Video.findById(id, options) | 700 | return Video.findById(id, options) |
696 | } | 701 | } |
697 | 702 | ||
703 | loadByUUIDAndPopulateAuthorAndPodAndTags = function (uuid: string) { | ||
704 | const options = { | ||
705 | where: { | ||
706 | uuid | ||
707 | }, | ||
708 | include: [ | ||
709 | { | ||
710 | model: Video['sequelize'].models.Author, | ||
711 | include: [ { model: Video['sequelize'].models.Pod, required: false } ] | ||
712 | }, | ||
713 | Video['sequelize'].models.Tag | ||
714 | ] | ||
715 | } | ||
716 | |||
717 | return Video.findOne(options) | ||
718 | } | ||
719 | |||
698 | searchAndPopulateAuthorAndPodAndTags = function (value: string, field: string, start: number, count: number, sort: string) { | 720 | searchAndPopulateAuthorAndPodAndTags = function (value: string, field: string, start: number, count: number, sort: string) { |
699 | const podInclude: Sequelize.IncludeOptions = { | 721 | const podInclude: Sequelize.IncludeOptions = { |
700 | model: Video['sequelize'].models.Pod, | 722 | model: Video['sequelize'].models.Pod, |