1 import * as safeBuffer from 'safe-buffer'
2 const Buffer = safeBuffer.Buffer
3 import * as magnetUtil from 'magnet-uri'
4 import { map, maxBy, truncate } from 'lodash'
5 import * as parseTorrent from 'parse-torrent'
6 import { join } from 'path'
7 import * as Sequelize from 'sequelize'
9 import { TagInstance } from './tag-interface'
17 isVideoDescriptionValid,
20 readFileBufferPromise,
26 generateImageFromVideoFile,
29 } from '../../helpers'
42 } from '../../initializers'
43 import { removeVideoToFriends } from '../../lib'
44 import { VideoResolution, VideoPrivacy } from '../../../shared'
45 import { VideoFileInstance, VideoFileModel } from './video-file-interface'
47 import { addMethodsToModel, getSort } from '../utils'
53 } from './video-interface'
54 import { VideoTorrentObject } from '../../../shared/models/activitypub/objects/video-torrent-object'
56 let Video: Sequelize.Model<VideoInstance, VideoAttributes>
57 let getOriginalFile: VideoMethods.GetOriginalFile
58 let getVideoFilename: VideoMethods.GetVideoFilename
59 let getThumbnailName: VideoMethods.GetThumbnailName
60 let getThumbnailPath: VideoMethods.GetThumbnailPath
61 let getPreviewName: VideoMethods.GetPreviewName
62 let getPreviewPath: VideoMethods.GetPreviewPath
63 let getTorrentFileName: VideoMethods.GetTorrentFileName
64 let isOwned: VideoMethods.IsOwned
65 let toFormattedJSON: VideoMethods.ToFormattedJSON
66 let toFormattedDetailsJSON: VideoMethods.ToFormattedDetailsJSON
67 let toActivityPubObject: VideoMethods.ToActivityPubObject
68 let optimizeOriginalVideofile: VideoMethods.OptimizeOriginalVideofile
69 let transcodeOriginalVideofile: VideoMethods.TranscodeOriginalVideofile
70 let createPreview: VideoMethods.CreatePreview
71 let createThumbnail: VideoMethods.CreateThumbnail
72 let getVideoFilePath: VideoMethods.GetVideoFilePath
73 let createTorrentAndSetInfoHash: VideoMethods.CreateTorrentAndSetInfoHash
74 let getOriginalFileHeight: VideoMethods.GetOriginalFileHeight
75 let getEmbedPath: VideoMethods.GetEmbedPath
76 let getDescriptionPath: VideoMethods.GetDescriptionPath
77 let getTruncatedDescription: VideoMethods.GetTruncatedDescription
78 let getCategoryLabel: VideoMethods.GetCategoryLabel
79 let getLicenceLabel: VideoMethods.GetLicenceLabel
80 let getLanguageLabel: VideoMethods.GetLanguageLabel
82 let generateThumbnailFromData: VideoMethods.GenerateThumbnailFromData
83 let list: VideoMethods.List
84 let listForApi: VideoMethods.ListForApi
85 let listUserVideosForApi: VideoMethods.ListUserVideosForApi
86 let loadByHostAndUUID: VideoMethods.LoadByHostAndUUID
87 let listOwnedAndPopulateAccountAndTags: VideoMethods.ListOwnedAndPopulateAccountAndTags
88 let listOwnedByAccount: VideoMethods.ListOwnedByAccount
89 let load: VideoMethods.Load
90 let loadByUUID: VideoMethods.LoadByUUID
91 let loadByUrl: VideoMethods.LoadByUrl
92 let loadLocalVideoByUUID: VideoMethods.LoadLocalVideoByUUID
93 let loadAndPopulateAccount: VideoMethods.LoadAndPopulateAccount
94 let loadAndPopulateAccountAndPodAndTags: VideoMethods.LoadAndPopulateAccountAndPodAndTags
95 let loadByUUIDAndPopulateAccountAndPodAndTags: VideoMethods.LoadByUUIDAndPopulateAccountAndPodAndTags
96 let searchAndPopulateAccountAndPodAndTags: VideoMethods.SearchAndPopulateAccountAndPodAndTags
97 let removeThumbnail: VideoMethods.RemoveThumbnail
98 let removePreview: VideoMethods.RemovePreview
99 let removeFile: VideoMethods.RemoveFile
100 let removeTorrent: VideoMethods.RemoveTorrent
102 export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
103 Video = sequelize.define<VideoInstance, VideoAttributes>('Video',
106 type: DataTypes.UUID,
107 defaultValue: DataTypes.UUIDV4,
114 type: DataTypes.STRING,
117 nameValid: value => {
118 const res = isVideoNameValid(value)
119 if (res === false) throw new Error('Video name is not valid.')
124 type: DataTypes.INTEGER,
127 categoryValid: value => {
128 const res = isVideoCategoryValid(value)
129 if (res === false) throw new Error('Video category is not valid.')
134 type: DataTypes.INTEGER,
138 licenceValid: value => {
139 const res = isVideoLicenceValid(value)
140 if (res === false) throw new Error('Video licence is not valid.')
145 type: DataTypes.INTEGER,
148 languageValid: value => {
149 const res = isVideoLanguageValid(value)
150 if (res === false) throw new Error('Video language is not valid.')
155 type: DataTypes.INTEGER,
158 privacyValid: value => {
159 const res = isVideoPrivacyValid(value)
160 if (res === false) throw new Error('Video privacy is not valid.')
165 type: DataTypes.BOOLEAN,
168 nsfwValid: value => {
169 const res = isVideoNSFWValid(value)
170 if (res === false) throw new Error('Video nsfw attribute is not valid.')
175 type: DataTypes.STRING(CONSTRAINTS_FIELDS.VIDEOS.DESCRIPTION.max),
178 descriptionValid: value => {
179 const res = isVideoDescriptionValid(value)
180 if (res === false) throw new Error('Video description is not valid.')
185 type: DataTypes.INTEGER,
188 durationValid: value => {
189 const res = isVideoDurationValid(value)
190 if (res === false) throw new Error('Video duration is not valid.')
195 type: DataTypes.INTEGER,
204 type: DataTypes.INTEGER,
213 type: DataTypes.INTEGER,
222 type: DataTypes.BOOLEAN,
227 type: DataTypes.STRING,
240 fields: [ 'createdAt' ]
243 fields: [ 'duration' ]
255 fields: [ 'channelId' ]
258 fields: [ 'parentId' ]
267 const classMethods = [
270 generateThumbnailFromData,
273 listUserVideosForApi,
274 listOwnedAndPopulateAccountAndTags,
277 loadAndPopulateAccount,
278 loadAndPopulateAccountAndPodAndTags,
281 loadLocalVideoByUUID,
282 loadByUUIDAndPopulateAccountAndPodAndTags,
283 searchAndPopulateAccountAndPodAndTags
285 const instanceMethods = [
288 createTorrentAndSetInfoHash,
304 toFormattedDetailsJSON,
305 optimizeOriginalVideofile,
306 transcodeOriginalVideofile,
307 getOriginalFileHeight,
309 getTruncatedDescription,
315 addMethodsToModel(Video, classMethods, instanceMethods)
320 // ------------------------------ METHODS ------------------------------
322 function associate (models) {
323 Video.belongsTo(models.VideoChannel, {
331 Video.belongsTo(models.VideoChannel, {
339 Video.belongsToMany(models.Tag, {
340 foreignKey: 'videoId',
341 through: models.VideoTag,
345 Video.hasMany(models.VideoAbuse, {
353 Video.hasMany(models.VideoFile, {
362 function afterDestroy (video: VideoInstance) {
366 video.removeThumbnail()
369 if (video.isOwned()) {
370 const removeVideoToFriendsParams = {
375 video.removePreview(),
376 removeVideoToFriends(removeVideoToFriendsParams)
379 // Remove physical files and torrents
380 video.VideoFiles.forEach(file => {
381 tasks.push(video.removeFile(file))
382 tasks.push(video.removeTorrent(file))
386 return Promise.all(tasks)
388 logger.error('Some errors when removing files of video %s in after destroy hook.', video.uuid, err)
392 getOriginalFile = function (this: VideoInstance) {
393 if (Array.isArray(this.VideoFiles) === false) return undefined
395 // The original file is the file that have the higher resolution
396 return maxBy(this.VideoFiles, file => file.resolution)
399 getVideoFilename = function (this: VideoInstance, videoFile: VideoFileInstance) {
400 return this.uuid + '-' + videoFile.resolution + videoFile.extname
403 getThumbnailName = function (this: VideoInstance) {
404 // We always have a copy of the thumbnail
405 const extension = '.jpg'
406 return this.uuid + extension
409 getPreviewName = function (this: VideoInstance) {
410 const extension = '.jpg'
411 return this.uuid + extension
414 getTorrentFileName = function (this: VideoInstance, videoFile: VideoFileInstance) {
415 const extension = '.torrent'
416 return this.uuid + '-' + videoFile.resolution + extension
419 isOwned = function (this: VideoInstance) {
420 return this.remote === false
423 createPreview = function (this: VideoInstance, videoFile: VideoFileInstance) {
424 const imageSize = PREVIEWS_SIZE.width + 'x' + PREVIEWS_SIZE.height
426 return generateImageFromVideoFile(
427 this.getVideoFilePath(videoFile),
428 CONFIG.STORAGE.PREVIEWS_DIR,
429 this.getPreviewName(),
434 createThumbnail = function (this: VideoInstance, videoFile: VideoFileInstance) {
435 const imageSize = THUMBNAILS_SIZE.width + 'x' + THUMBNAILS_SIZE.height
437 return generateImageFromVideoFile(
438 this.getVideoFilePath(videoFile),
439 CONFIG.STORAGE.THUMBNAILS_DIR,
440 this.getThumbnailName(),
445 getVideoFilePath = function (this: VideoInstance, videoFile: VideoFileInstance) {
446 return join(CONFIG.STORAGE.VIDEOS_DIR, this.getVideoFilename(videoFile))
449 createTorrentAndSetInfoHash = async function (this: VideoInstance, videoFile: VideoFileInstance) {
452 [ CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT + '/tracker/socket' ]
455 CONFIG.WEBSERVER.URL + STATIC_PATHS.WEBSEED + this.getVideoFilename(videoFile)
459 const torrent = await createTorrentPromise(this.getVideoFilePath(videoFile), options)
461 const filePath = join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile))
462 logger.info('Creating torrent %s.', filePath)
464 await writeFilePromise(filePath, torrent)
466 const parsedTorrent = parseTorrent(torrent)
467 videoFile.infoHash = parsedTorrent.infoHash
470 getEmbedPath = function (this: VideoInstance) {
471 return '/videos/embed/' + this.uuid
474 getThumbnailPath = function (this: VideoInstance) {
475 return join(STATIC_PATHS.THUMBNAILS, this.getThumbnailName())
478 getPreviewPath = function (this: VideoInstance) {
479 return join(STATIC_PATHS.PREVIEWS, this.getPreviewName())
482 toFormattedJSON = function (this: VideoInstance) {
485 if (this.VideoChannel.Account.Pod) {
486 podHost = this.VideoChannel.Account.Pod.host
488 // It means it's our video
489 podHost = CONFIG.WEBSERVER.HOST
496 category: this.category,
497 categoryLabel: this.getCategoryLabel(),
498 licence: this.licence,
499 licenceLabel: this.getLicenceLabel(),
500 language: this.language,
501 languageLabel: this.getLanguageLabel(),
503 description: this.getTruncatedDescription(),
505 isLocal: this.isOwned(),
506 account: this.VideoChannel.Account.name,
507 duration: this.duration,
510 dislikes: this.dislikes,
511 tags: map<TagInstance, string>(this.Tags, 'name'),
512 thumbnailPath: this.getThumbnailPath(),
513 previewPath: this.getPreviewPath(),
514 embedPath: this.getEmbedPath(),
515 createdAt: this.createdAt,
516 updatedAt: this.updatedAt
522 toFormattedDetailsJSON = function (this: VideoInstance) {
523 const formattedJson = this.toFormattedJSON()
525 // Maybe our pod is not up to date and there are new privacy settings since our version
526 let privacyLabel = VIDEO_PRIVACIES[this.privacy]
527 if (!privacyLabel) privacyLabel = 'Unknown'
529 const detailsJson = {
531 privacy: this.privacy,
532 descriptionPath: this.getDescriptionPath(),
533 channel: this.VideoChannel.toFormattedJSON(),
537 // Format and sort video files
538 const { baseUrlHttp, baseUrlWs } = getBaseUrls(this)
539 detailsJson.files = this.VideoFiles
541 let resolutionLabel = videoFile.resolution + 'p'
543 const videoFileJson = {
544 resolution: videoFile.resolution,
546 magnetUri: generateMagnetUri(this, videoFile, baseUrlHttp, baseUrlWs),
547 size: videoFile.size,
548 torrentUrl: getTorrentUrl(this, videoFile, baseUrlHttp),
549 fileUrl: getVideoFileUrl(this, videoFile, baseUrlHttp)
555 if (a.resolution < b.resolution) return 1
556 if (a.resolution === b.resolution) return 0
560 return Object.assign(formattedJson, detailsJson)
563 toActivityPubObject = function (this: VideoInstance) {
564 const { baseUrlHttp, baseUrlWs } = getBaseUrls(this)
566 const tag = this.Tags.map(t => ({
572 for (const file of this.VideoFiles) {
575 mimeType: 'video/' + file.extname,
576 url: getVideoFileUrl(this, file, baseUrlHttp),
577 width: file.resolution,
583 mimeType: 'application/x-bittorrent',
584 url: getTorrentUrl(this, file, baseUrlHttp),
585 width: file.resolution
590 mimeType: 'application/x-bittorrent;x-scheme-handler/magnet',
591 url: generateMagnetUri(this, file, baseUrlHttp, baseUrlWs),
592 width: file.resolution
596 const videoObject: VideoTorrentObject = {
599 // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration
600 duration: 'PT' + this.duration + 'S',
605 label: this.getCategoryLabel()
609 name: this.getLicenceLabel()
613 name: this.getLanguageLabel()
617 published: this.createdAt,
618 updated: this.updatedAt,
619 mediaType: 'text/markdown',
620 content: this.getTruncatedDescription(),
623 url: getThumbnailUrl(this, baseUrlHttp),
624 mediaType: 'image/jpeg',
625 width: THUMBNAILS_SIZE.width,
626 height: THUMBNAILS_SIZE.height
634 getTruncatedDescription = function (this: VideoInstance) {
636 length: CONSTRAINTS_FIELDS.VIDEOS.TRUNCATED_DESCRIPTION.max
639 return truncate(this.description, options)
642 optimizeOriginalVideofile = async function (this: VideoInstance) {
643 const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
644 const newExtname = '.mp4'
645 const inputVideoFile = this.getOriginalFile()
646 const videoInputPath = join(videosDirectory, this.getVideoFilename(inputVideoFile))
647 const videoOutputPath = join(videosDirectory, this.id + '-transcoded' + newExtname)
649 const transcodeOptions = {
650 inputPath: videoInputPath,
651 outputPath: videoOutputPath
655 // Could be very long!
656 await transcode(transcodeOptions)
658 await unlinkPromise(videoInputPath)
660 // Important to do this before getVideoFilename() to take in account the new file extension
661 inputVideoFile.set('extname', newExtname)
663 await renamePromise(videoOutputPath, this.getVideoFilePath(inputVideoFile))
664 const stats = await statPromise(this.getVideoFilePath(inputVideoFile))
666 inputVideoFile.set('size', stats.size)
668 await this.createTorrentAndSetInfoHash(inputVideoFile)
669 await inputVideoFile.save()
672 // Auto destruction...
673 this.destroy().catch(err => logger.error('Cannot destruct video after transcoding failure.', err))
679 transcodeOriginalVideofile = async function (this: VideoInstance, resolution: VideoResolution) {
680 const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
681 const extname = '.mp4'
683 // We are sure it's x264 in mp4 because optimizeOriginalVideofile was already executed
684 const videoInputPath = join(videosDirectory, this.getVideoFilename(this.getOriginalFile()))
686 const newVideoFile = (Video['sequelize'].models.VideoFile as VideoFileModel).build({
692 const videoOutputPath = join(videosDirectory, this.getVideoFilename(newVideoFile))
694 const transcodeOptions = {
695 inputPath: videoInputPath,
696 outputPath: videoOutputPath,
700 await transcode(transcodeOptions)
702 const stats = await statPromise(videoOutputPath)
704 newVideoFile.set('size', stats.size)
706 await this.createTorrentAndSetInfoHash(newVideoFile)
708 await newVideoFile.save()
710 this.VideoFiles.push(newVideoFile)
713 getOriginalFileHeight = function (this: VideoInstance) {
714 const originalFilePath = this.getVideoFilePath(this.getOriginalFile())
716 return getVideoFileHeight(originalFilePath)
719 getDescriptionPath = function (this: VideoInstance) {
720 return `/api/${API_VERSION}/videos/${this.uuid}/description`
723 getCategoryLabel = function (this: VideoInstance) {
724 let categoryLabel = VIDEO_CATEGORIES[this.category]
726 // Maybe our pod is not up to date and there are new categories since our version
727 if (!categoryLabel) categoryLabel = 'Misc'
732 getLicenceLabel = function (this: VideoInstance) {
733 let licenceLabel = VIDEO_LICENCES[this.licence]
734 // Maybe our pod is not up to date and there are new licences since our version
735 if (!licenceLabel) licenceLabel = 'Unknown'
740 getLanguageLabel = function (this: VideoInstance) {
741 // Language is an optional attribute
742 let languageLabel = VIDEO_LANGUAGES[this.language]
743 if (!languageLabel) languageLabel = 'Unknown'
748 removeThumbnail = function (this: VideoInstance) {
749 const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, this.getThumbnailName())
750 return unlinkPromise(thumbnailPath)
753 removePreview = function (this: VideoInstance) {
754 // Same name than video thumbnail
755 return unlinkPromise(CONFIG.STORAGE.PREVIEWS_DIR + this.getPreviewName())
758 removeFile = function (this: VideoInstance, videoFile: VideoFileInstance) {
759 const filePath = join(CONFIG.STORAGE.VIDEOS_DIR, this.getVideoFilename(videoFile))
760 return unlinkPromise(filePath)
763 removeTorrent = function (this: VideoInstance, videoFile: VideoFileInstance) {
764 const torrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile))
765 return unlinkPromise(torrentPath)
768 // ------------------------------ STATICS ------------------------------
770 generateThumbnailFromData = function (video: VideoInstance, thumbnailData: string) {
771 // Creating the thumbnail for a remote video
773 const thumbnailName = video.getThumbnailName()
774 const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, thumbnailName)
775 return writeFilePromise(thumbnailPath, Buffer.from(thumbnailData, 'binary')).then(() => {
782 include: [ Video['sequelize'].models.VideoFile ]
785 return Video.findAll(query)
788 listUserVideosForApi = function (userId: number, start: number, count: number, sort: string) {
793 order: [ getSort(sort), [ Video['sequelize'].models.Tag, 'name', 'ASC' ] ],
796 model: Video['sequelize'].models.VideoChannel,
800 model: Video['sequelize'].models.Account,
808 Video['sequelize'].models.Tag
812 return Video.findAndCountAll(query).then(({ rows, count }) => {
820 listForApi = function (start: number, count: number, sort: string) {
825 order: [ getSort(sort), [ Video['sequelize'].models.Tag, 'name', 'ASC' ] ],
828 model: Video['sequelize'].models.VideoChannel,
831 model: Video['sequelize'].models.Account,
834 model: Video['sequelize'].models.Pod,
841 Video['sequelize'].models.Tag
843 where: createBaseVideosWhere()
846 return Video.findAndCountAll(query).then(({ rows, count }) => {
854 loadByHostAndUUID = function (fromHost: string, uuid: string, t?: Sequelize.Transaction) {
855 const query: Sequelize.FindOptions<VideoAttributes> = {
861 model: Video['sequelize'].models.VideoFile
864 model: Video['sequelize'].models.VideoChannel,
867 model: Video['sequelize'].models.Account,
870 model: Video['sequelize'].models.Pod,
883 if (t !== undefined) query.transaction = t
885 return Video.findOne(query)
888 listOwnedAndPopulateAccountAndTags = function () {
894 Video['sequelize'].models.VideoFile,
896 model: Video['sequelize'].models.VideoChannel,
897 include: [ Video['sequelize'].models.Account ]
899 Video['sequelize'].models.Tag
903 return Video.findAll(query)
906 listOwnedByAccount = function (account: string) {
913 model: Video['sequelize'].models.VideoFile
916 model: Video['sequelize'].models.VideoChannel,
919 model: Video['sequelize'].models.Account,
929 return Video.findAll(query)
932 load = function (id: number) {
933 return Video.findById(id)
936 loadByUUID = function (uuid: string, t?: Sequelize.Transaction) {
937 const query: Sequelize.FindOptions<VideoAttributes> = {
941 include: [ Video['sequelize'].models.VideoFile ]
944 if (t !== undefined) query.transaction = t
946 return Video.findOne(query)
949 loadLocalVideoByUUID = function (uuid: string, t?: Sequelize.Transaction) {
950 const query: Sequelize.FindOptions<VideoAttributes> = {
955 include: [ Video['sequelize'].models.VideoFile ]
958 if (t !== undefined) query.transaction = t
960 return Video.findOne(query)
963 loadAndPopulateAccount = function (id: number) {
966 Video['sequelize'].models.VideoFile,
968 model: Video['sequelize'].models.VideoChannel,
969 include: [ Video['sequelize'].models.Account ]
974 return Video.findById(id, options)
977 loadAndPopulateAccountAndPodAndTags = function (id: number) {
981 model: Video['sequelize'].models.VideoChannel,
984 model: Video['sequelize'].models.Account,
985 include: [ { model: Video['sequelize'].models.Pod, required: false } ]
989 Video['sequelize'].models.Tag,
990 Video['sequelize'].models.VideoFile
994 return Video.findById(id, options)
997 loadByUUIDAndPopulateAccountAndPodAndTags = function (uuid: string) {
1004 model: Video['sequelize'].models.VideoChannel,
1007 model: Video['sequelize'].models.Account,
1008 include: [ { model: Video['sequelize'].models.Pod, required: false } ]
1012 Video['sequelize'].models.Tag,
1013 Video['sequelize'].models.VideoFile
1017 return Video.findOne(options)
1020 searchAndPopulateAccountAndPodAndTags = function (value: string, field: string, start: number, count: number, sort: string) {
1021 const podInclude: Sequelize.IncludeOptions = {
1022 model: Video['sequelize'].models.Pod,
1026 const accountInclude: Sequelize.IncludeOptions = {
1027 model: Video['sequelize'].models.Account,
1028 include: [ podInclude ]
1031 const videoChannelInclude: Sequelize.IncludeOptions = {
1032 model: Video['sequelize'].models.VideoChannel,
1033 include: [ accountInclude ],
1037 const tagInclude: Sequelize.IncludeOptions = {
1038 model: Video['sequelize'].models.Tag
1041 const query: Sequelize.FindOptions<VideoAttributes> = {
1043 where: createBaseVideosWhere(),
1046 order: [ getSort(sort), [ Video['sequelize'].models.Tag, 'name', 'ASC' ] ]
1049 if (field === 'tags') {
1050 const escapedValue = Video['sequelize'].escape('%' + value + '%')
1051 query.where['id'][Sequelize.Op.in] = Video['sequelize'].literal(
1052 `(SELECT "VideoTags"."videoId"
1054 INNER JOIN "VideoTags" ON "Tags"."id" = "VideoTags"."tagId"
1055 WHERE name ILIKE ${escapedValue}
1058 } else if (field === 'host') {
1059 // FIXME: Include our pod? (not stored in the database)
1060 podInclude.where = {
1062 [Sequelize.Op.iLike]: '%' + value + '%'
1065 podInclude.required = true
1066 } else if (field === 'account') {
1067 accountInclude.where = {
1069 [Sequelize.Op.iLike]: '%' + value + '%'
1073 query.where[field] = {
1074 [Sequelize.Op.iLike]: '%' + value + '%'
1079 videoChannelInclude, tagInclude
1082 return Video.findAndCountAll(query).then(({ rows, count }) => {
1090 // ---------------------------------------------------------------------------
1092 function createBaseVideosWhere () {
1095 [Sequelize.Op.notIn]: Video['sequelize'].literal(
1096 '(SELECT "BlacklistedVideos"."videoId" FROM "BlacklistedVideos")'
1099 privacy: VideoPrivacy.PUBLIC
1103 function getBaseUrls (video: VideoInstance) {
1107 if (video.isOwned()) {
1108 baseUrlHttp = CONFIG.WEBSERVER.URL
1109 baseUrlWs = CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT
1111 baseUrlHttp = REMOTE_SCHEME.HTTP + '://' + video.VideoChannel.Account.Pod.host
1112 baseUrlWs = REMOTE_SCHEME.WS + '://' + video.VideoChannel.Account.Pod.host
1115 return { baseUrlHttp, baseUrlWs }
1118 function getThumbnailUrl (video: VideoInstance, baseUrlHttp: string) {
1119 return baseUrlHttp + STATIC_PATHS.THUMBNAILS + video.getThumbnailName()
1122 function getTorrentUrl (video: VideoInstance, videoFile: VideoFileInstance, baseUrlHttp: string) {
1123 return baseUrlHttp + STATIC_PATHS.TORRENTS + video.getTorrentFileName(videoFile)
1126 function getVideoFileUrl (video: VideoInstance, videoFile: VideoFileInstance, baseUrlHttp: string) {
1127 return baseUrlHttp + STATIC_PATHS.WEBSEED + video.getVideoFilename(videoFile)
1130 function generateMagnetUri (video: VideoInstance, videoFile: VideoFileInstance, baseUrlHttp: string, baseUrlWs: string) {
1131 const xs = getTorrentUrl(video, videoFile, baseUrlHttp)
1132 const announce = [ baseUrlWs + '/tracker/socket', baseUrlHttp + '/tracker/announce' ]
1133 const urlList = [ getVideoFileUrl(video, videoFile, baseUrlHttp) ]
1135 const magnetHash = {
1139 infoHash: videoFile.infoHash,
1143 return magnetUtil.encode(magnetHash)