diff options
Diffstat (limited to 'server/models/video.ts')
-rw-r--r-- | server/models/video.ts | 79 |
1 files changed, 43 insertions, 36 deletions
diff --git a/server/models/video.ts b/server/models/video.ts index 5558a7c3b..3f808b811 100644 --- a/server/models/video.ts +++ b/server/models/video.ts | |||
@@ -11,6 +11,7 @@ import { join } from 'path' | |||
11 | import * as Sequelize from 'sequelize' | 11 | import * as Sequelize from 'sequelize' |
12 | 12 | ||
13 | import { database as db } from '../initializers/database' | 13 | import { database as db } from '../initializers/database' |
14 | import { VideoTagInstance } from './video-tag-interface' | ||
14 | import { | 15 | import { |
15 | logger, | 16 | logger, |
16 | isVideoNameValid, | 17 | isVideoNameValid, |
@@ -266,7 +267,7 @@ export default function (sequelize, DataTypes) { | |||
266 | return Video | 267 | return Video |
267 | } | 268 | } |
268 | 269 | ||
269 | function beforeValidate (video, options) { | 270 | function beforeValidate (video: VideoInstance) { |
270 | // Put a fake infoHash if it does not exists yet | 271 | // Put a fake infoHash if it does not exists yet |
271 | if (video.isOwned() && !video.infoHash) { | 272 | if (video.isOwned() && !video.infoHash) { |
272 | // 40 hexa length | 273 | // 40 hexa length |
@@ -274,7 +275,7 @@ function beforeValidate (video, options) { | |||
274 | } | 275 | } |
275 | } | 276 | } |
276 | 277 | ||
277 | function beforeCreate (video, options) { | 278 | function beforeCreate (video: VideoInstance, options: { transaction: Sequelize.Transaction }) { |
278 | return new Promise(function (resolve, reject) { | 279 | return new Promise(function (resolve, reject) { |
279 | const tasks = [] | 280 | const tasks = [] |
280 | 281 | ||
@@ -318,7 +319,7 @@ function beforeCreate (video, options) { | |||
318 | }) | 319 | }) |
319 | } | 320 | } |
320 | 321 | ||
321 | function afterDestroy (video, options) { | 322 | function afterDestroy (video: VideoInstance) { |
322 | return new Promise(function (resolve, reject) { | 323 | return new Promise(function (resolve, reject) { |
323 | const tasks = [] | 324 | const tasks = [] |
324 | 325 | ||
@@ -401,7 +402,7 @@ generateMagnetUri = function () { | |||
401 | } | 402 | } |
402 | 403 | ||
403 | const xs = baseUrlHttp + STATIC_PATHS.TORRENTS + this.getTorrentName() | 404 | const xs = baseUrlHttp + STATIC_PATHS.TORRENTS + this.getTorrentName() |
404 | const announce = baseUrlWs + '/tracker/socket' | 405 | const announce = [ baseUrlWs + '/tracker/socket' ] |
405 | const urlList = [ baseUrlHttp + STATIC_PATHS.WEBSEED + this.getVideoFilename() ] | 406 | const urlList = [ baseUrlHttp + STATIC_PATHS.WEBSEED + this.getVideoFilename() ] |
406 | 407 | ||
407 | const magnetHash = { | 408 | const magnetHash = { |
@@ -496,7 +497,7 @@ toFormatedJSON = function () { | |||
496 | return json | 497 | return json |
497 | } | 498 | } |
498 | 499 | ||
499 | toAddRemoteJSON = function (callback) { | 500 | toAddRemoteJSON = function (callback: VideoMethods.ToAddRemoteJSONCallback) { |
500 | // Get thumbnail data to send to the other pod | 501 | // Get thumbnail data to send to the other pod |
501 | const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, this.getThumbnailName()) | 502 | const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, this.getThumbnailName()) |
502 | fs.readFile(thumbnailPath, (err, thumbnailData) => { | 503 | fs.readFile(thumbnailPath, (err, thumbnailData) => { |
@@ -517,7 +518,7 @@ toAddRemoteJSON = function (callback) { | |||
517 | author: this.Author.name, | 518 | author: this.Author.name, |
518 | duration: this.duration, | 519 | duration: this.duration, |
519 | thumbnailData: thumbnailData.toString('binary'), | 520 | thumbnailData: thumbnailData.toString('binary'), |
520 | tags: map(this.Tags, 'name'), | 521 | tags: map<VideoTagInstance, string>(this.Tags, 'name'), |
521 | createdAt: this.createdAt, | 522 | createdAt: this.createdAt, |
522 | updatedAt: this.updatedAt, | 523 | updatedAt: this.updatedAt, |
523 | extname: this.extname, | 524 | extname: this.extname, |
@@ -530,7 +531,7 @@ toAddRemoteJSON = function (callback) { | |||
530 | }) | 531 | }) |
531 | } | 532 | } |
532 | 533 | ||
533 | toUpdateRemoteJSON = function (callback) { | 534 | toUpdateRemoteJSON = function () { |
534 | const json = { | 535 | const json = { |
535 | name: this.name, | 536 | name: this.name, |
536 | category: this.category, | 537 | category: this.category, |
@@ -542,7 +543,7 @@ toUpdateRemoteJSON = function (callback) { | |||
542 | remoteId: this.id, | 543 | remoteId: this.id, |
543 | author: this.Author.name, | 544 | author: this.Author.name, |
544 | duration: this.duration, | 545 | duration: this.duration, |
545 | tags: map(this.Tags, 'name'), | 546 | tags: map<VideoTagInstance, string>(this.Tags, 'name'), |
546 | createdAt: this.createdAt, | 547 | createdAt: this.createdAt, |
547 | updatedAt: this.updatedAt, | 548 | updatedAt: this.updatedAt, |
548 | extname: this.extname, | 549 | extname: this.extname, |
@@ -554,7 +555,7 @@ toUpdateRemoteJSON = function (callback) { | |||
554 | return json | 555 | return json |
555 | } | 556 | } |
556 | 557 | ||
557 | transcodeVideofile = function (finalCallback) { | 558 | transcodeVideofile = function (finalCallback: VideoMethods.TranscodeVideofileCallback) { |
558 | const video = this | 559 | const video = this |
559 | 560 | ||
560 | const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR | 561 | const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR |
@@ -591,9 +592,9 @@ transcodeVideofile = function (finalCallback) { | |||
591 | video.save().asCallback(callback) | 592 | video.save().asCallback(callback) |
592 | } | 593 | } |
593 | 594 | ||
594 | ], function (err) { | 595 | ], function (err: Error) { |
595 | if (err) { | 596 | if (err) { |
596 | // Autodescruction... | 597 | // Autodesctruction... |
597 | video.destroy().asCallback(function (err) { | 598 | video.destroy().asCallback(function (err) { |
598 | if (err) logger.error('Cannot destruct video after transcoding failure.', { error: err }) | 599 | if (err) logger.error('Cannot destruct video after transcoding failure.', { error: err }) |
599 | }) | 600 | }) |
@@ -609,7 +610,7 @@ transcodeVideofile = function (finalCallback) { | |||
609 | 610 | ||
610 | // ------------------------------ STATICS ------------------------------ | 611 | // ------------------------------ STATICS ------------------------------ |
611 | 612 | ||
612 | generateThumbnailFromData = function (video, thumbnailData, callback) { | 613 | generateThumbnailFromData = function (video: VideoInstance, thumbnailData: string, callback: VideoMethods.GenerateThumbnailFromDataCallback) { |
613 | // Creating the thumbnail for a remote video | 614 | // Creating the thumbnail for a remote video |
614 | 615 | ||
615 | const thumbnailName = video.getThumbnailName() | 616 | const thumbnailName = video.getThumbnailName() |
@@ -621,7 +622,7 @@ generateThumbnailFromData = function (video, thumbnailData, callback) { | |||
621 | }) | 622 | }) |
622 | } | 623 | } |
623 | 624 | ||
624 | getDurationFromFile = function (videoPath, callback) { | 625 | getDurationFromFile = function (videoPath: string, callback: VideoMethods.GetDurationFromFileCallback) { |
625 | ffmpeg.ffprobe(videoPath, function (err, metadata) { | 626 | ffmpeg.ffprobe(videoPath, function (err, metadata) { |
626 | if (err) return callback(err) | 627 | if (err) return callback(err) |
627 | 628 | ||
@@ -629,11 +630,11 @@ getDurationFromFile = function (videoPath, callback) { | |||
629 | }) | 630 | }) |
630 | } | 631 | } |
631 | 632 | ||
632 | list = function (callback) { | 633 | list = function (callback: VideoMethods.ListCallback) { |
633 | return Video.findAll().asCallback(callback) | 634 | return Video.findAll().asCallback(callback) |
634 | } | 635 | } |
635 | 636 | ||
636 | listForApi = function (start, count, sort, callback) { | 637 | listForApi = function (start: number, count: number, sort: string, callback: VideoMethods.ListForApiCallback) { |
637 | // Exclude Blakclisted videos from the list | 638 | // Exclude Blakclisted videos from the list |
638 | const query = { | 639 | const query = { |
639 | distinct: true, | 640 | distinct: true, |
@@ -658,7 +659,7 @@ listForApi = function (start, count, sort, callback) { | |||
658 | }) | 659 | }) |
659 | } | 660 | } |
660 | 661 | ||
661 | loadByHostAndRemoteId = function (fromHost, remoteId, callback) { | 662 | loadByHostAndRemoteId = function (fromHost: string, remoteId: string, callback: VideoMethods.LoadByHostAndRemoteIdCallback) { |
662 | const query = { | 663 | const query = { |
663 | where: { | 664 | where: { |
664 | remoteId: remoteId | 665 | remoteId: remoteId |
@@ -682,7 +683,7 @@ loadByHostAndRemoteId = function (fromHost, remoteId, callback) { | |||
682 | return Video.findOne(query).asCallback(callback) | 683 | return Video.findOne(query).asCallback(callback) |
683 | } | 684 | } |
684 | 685 | ||
685 | listOwnedAndPopulateAuthorAndTags = function (callback) { | 686 | listOwnedAndPopulateAuthorAndTags = function (callback: VideoMethods.ListOwnedAndPopulateAuthorAndTagsCallback) { |
686 | // If remoteId is null this is *our* video | 687 | // If remoteId is null this is *our* video |
687 | const query = { | 688 | const query = { |
688 | where: { | 689 | where: { |
@@ -694,7 +695,7 @@ listOwnedAndPopulateAuthorAndTags = function (callback) { | |||
694 | return Video.findAll(query).asCallback(callback) | 695 | return Video.findAll(query).asCallback(callback) |
695 | } | 696 | } |
696 | 697 | ||
697 | listOwnedByAuthor = function (author, callback) { | 698 | listOwnedByAuthor = function (author: string, callback: VideoMethods.ListOwnedByAuthorCallback) { |
698 | const query = { | 699 | const query = { |
699 | where: { | 700 | where: { |
700 | remoteId: null | 701 | remoteId: null |
@@ -712,11 +713,11 @@ listOwnedByAuthor = function (author, callback) { | |||
712 | return Video.findAll(query).asCallback(callback) | 713 | return Video.findAll(query).asCallback(callback) |
713 | } | 714 | } |
714 | 715 | ||
715 | load = function (id, callback) { | 716 | load = function (id: string, callback: VideoMethods.LoadCallback) { |
716 | return Video.findById(id).asCallback(callback) | 717 | return Video.findById(id).asCallback(callback) |
717 | } | 718 | } |
718 | 719 | ||
719 | loadAndPopulateAuthor = function (id, callback) { | 720 | loadAndPopulateAuthor = function (id: string, callback: VideoMethods.LoadAndPopulateAuthorCallback) { |
720 | const options = { | 721 | const options = { |
721 | include: [ Video['sequelize'].models.Author ] | 722 | include: [ Video['sequelize'].models.Author ] |
722 | } | 723 | } |
@@ -724,7 +725,7 @@ loadAndPopulateAuthor = function (id, callback) { | |||
724 | return Video.findById(id, options).asCallback(callback) | 725 | return Video.findById(id, options).asCallback(callback) |
725 | } | 726 | } |
726 | 727 | ||
727 | loadAndPopulateAuthorAndPodAndTags = function (id, callback) { | 728 | loadAndPopulateAuthorAndPodAndTags = function (id: string, callback: VideoMethods.LoadAndPopulateAuthorAndPodAndTagsCallback) { |
728 | const options = { | 729 | const options = { |
729 | include: [ | 730 | include: [ |
730 | { | 731 | { |
@@ -738,7 +739,14 @@ loadAndPopulateAuthorAndPodAndTags = function (id, callback) { | |||
738 | return Video.findById(id, options).asCallback(callback) | 739 | return Video.findById(id, options).asCallback(callback) |
739 | } | 740 | } |
740 | 741 | ||
741 | searchAndPopulateAuthorAndPodAndTags = function (value, field, start, count, sort, callback) { | 742 | searchAndPopulateAuthorAndPodAndTags = function ( |
743 | value: string, | ||
744 | field: string, | ||
745 | start: number, | ||
746 | count: number, | ||
747 | sort: string, | ||
748 | callback: VideoMethods.SearchAndPopulateAuthorAndPodAndTagsCallback | ||
749 | ) { | ||
742 | const podInclude: any = { | 750 | const podInclude: any = { |
743 | model: Video['sequelize'].models.Pod, | 751 | model: Video['sequelize'].models.Pod, |
744 | required: false | 752 | required: false |
@@ -821,27 +829,27 @@ function createBaseVideosWhere () { | |||
821 | } | 829 | } |
822 | } | 830 | } |
823 | 831 | ||
824 | function removeThumbnail (video, callback) { | 832 | function removeThumbnail (video: VideoInstance, callback: (err: Error) => void) { |
825 | const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, video.getThumbnailName()) | 833 | const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, video.getThumbnailName()) |
826 | fs.unlink(thumbnailPath, callback) | 834 | fs.unlink(thumbnailPath, callback) |
827 | } | 835 | } |
828 | 836 | ||
829 | function removeFile (video, callback) { | 837 | function removeFile (video: VideoInstance, callback: (err: Error) => void) { |
830 | const filePath = join(CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename()) | 838 | const filePath = join(CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename()) |
831 | fs.unlink(filePath, callback) | 839 | fs.unlink(filePath, callback) |
832 | } | 840 | } |
833 | 841 | ||
834 | function removeTorrent (video, callback) { | 842 | function removeTorrent (video: VideoInstance, callback: (err: Error) => void) { |
835 | const torrenPath = join(CONFIG.STORAGE.TORRENTS_DIR, video.getTorrentName()) | 843 | const torrenPath = join(CONFIG.STORAGE.TORRENTS_DIR, video.getTorrentName()) |
836 | fs.unlink(torrenPath, callback) | 844 | fs.unlink(torrenPath, callback) |
837 | } | 845 | } |
838 | 846 | ||
839 | function removePreview (video, callback) { | 847 | function removePreview (video: VideoInstance, callback: (err: Error) => void) { |
840 | // Same name than video thumnail | 848 | // Same name than video thumnail |
841 | fs.unlink(CONFIG.STORAGE.PREVIEWS_DIR + video.getPreviewName(), callback) | 849 | fs.unlink(CONFIG.STORAGE.PREVIEWS_DIR + video.getPreviewName(), callback) |
842 | } | 850 | } |
843 | 851 | ||
844 | function createTorrentFromVideo (video, videoPath, callback) { | 852 | function createTorrentFromVideo (video: VideoInstance, videoPath: string, callback: (err: Error) => void) { |
845 | const options = { | 853 | const options = { |
846 | announceList: [ | 854 | announceList: [ |
847 | [ CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT + '/tracker/socket' ] | 855 | [ CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT + '/tracker/socket' ] |
@@ -865,24 +873,23 @@ function createTorrentFromVideo (video, videoPath, callback) { | |||
865 | }) | 873 | }) |
866 | } | 874 | } |
867 | 875 | ||
868 | function createPreview (video, videoPath, callback) { | 876 | function createPreview (video: VideoInstance, videoPath: string, callback: (err: Error) => void) { |
869 | generateImage(video, videoPath, CONFIG.STORAGE.PREVIEWS_DIR, video.getPreviewName(), callback) | 877 | generateImage(video, videoPath, CONFIG.STORAGE.PREVIEWS_DIR, video.getPreviewName(), null, callback) |
870 | } | 878 | } |
871 | 879 | ||
872 | function createThumbnail (video, videoPath, callback) { | 880 | function createThumbnail (video: VideoInstance, videoPath: string, callback: (err: Error) => void) { |
873 | generateImage(video, videoPath, CONFIG.STORAGE.THUMBNAILS_DIR, video.getThumbnailName(), THUMBNAILS_SIZE, callback) | 881 | generateImage(video, videoPath, CONFIG.STORAGE.THUMBNAILS_DIR, video.getThumbnailName(), THUMBNAILS_SIZE, callback) |
874 | } | 882 | } |
875 | 883 | ||
876 | function generateImage (video, videoPath, folder, imageName, size, callback?) { | 884 | type GenerateImageCallback = (err: Error, imageName: string) => void |
885 | function generateImage (video: VideoInstance, videoPath: string, folder: string, imageName: string, size: string, callback?: GenerateImageCallback) { | ||
877 | const options: any = { | 886 | const options: any = { |
878 | filename: imageName, | 887 | filename: imageName, |
879 | count: 1, | 888 | count: 1, |
880 | folder | 889 | folder |
881 | } | 890 | } |
882 | 891 | ||
883 | if (!callback) { | 892 | if (size) { |
884 | callback = size | ||
885 | } else { | ||
886 | options.size = size | 893 | options.size = size |
887 | } | 894 | } |
888 | 895 | ||
@@ -894,7 +901,7 @@ function generateImage (video, videoPath, folder, imageName, size, callback?) { | |||
894 | .thumbnail(options) | 901 | .thumbnail(options) |
895 | } | 902 | } |
896 | 903 | ||
897 | function removeFromBlacklist (video, callback) { | 904 | function removeFromBlacklist (video: VideoInstance, callback: (err: Error) => void) { |
898 | // Find the blacklisted video | 905 | // Find the blacklisted video |
899 | db.BlacklistedVideo.loadByVideoId(video.id, function (err, video) { | 906 | db.BlacklistedVideo.loadByVideoId(video.id, function (err, video) { |
900 | // If an error occured, stop here | 907 | // If an error occured, stop here |
@@ -908,7 +915,7 @@ function removeFromBlacklist (video, callback) { | |||
908 | video.destroy().asCallback(callback) | 915 | video.destroy().asCallback(callback) |
909 | } else { | 916 | } else { |
910 | // If haven't found it, simply ignore it and do nothing | 917 | // If haven't found it, simply ignore it and do nothing |
911 | return callback() | 918 | return callback(null) |
912 | } | 919 | } |
913 | }) | 920 | }) |
914 | } | 921 | } |