import { join } from 'path'
import * as Sequelize from 'sequelize'
import * as Promise from 'bluebird'
-import { maxBy } from 'lodash'
+import { maxBy, truncate } from 'lodash'
import { TagInstance } from './tag-interface'
import {
VIDEO_CATEGORIES,
VIDEO_LICENCES,
VIDEO_LANGUAGES,
- THUMBNAILS_SIZE
+ THUMBNAILS_SIZE,
+ PREVIEWS_SIZE,
+ CONSTRAINTS_FIELDS,
+ API_VERSION
} from '../../initializers'
import { removeVideoToFriends } from '../../lib'
import { VideoResolution } from '../../../shared'
VideoMethods
} from './video-interface'
-import { PREVIEWS_SIZE } from '../../initializers/constants'
let Video: Sequelize.Model<VideoInstance, VideoAttributes>
let getOriginalFile: VideoMethods.GetOriginalFile
-let generateMagnetUri: VideoMethods.GenerateMagnetUri
let getVideoFilename: VideoMethods.GetVideoFilename
let getThumbnailName: VideoMethods.GetThumbnailName
let getThumbnailPath: VideoMethods.GetThumbnailPath
let getTorrentFileName: VideoMethods.GetTorrentFileName
let isOwned: VideoMethods.IsOwned
let toFormattedJSON: VideoMethods.ToFormattedJSON
+let toFormattedDetailsJSON: VideoMethods.ToFormattedDetailsJSON
let toAddRemoteJSON: VideoMethods.ToAddRemoteJSON
let toUpdateRemoteJSON: VideoMethods.ToUpdateRemoteJSON
let optimizeOriginalVideofile: VideoMethods.OptimizeOriginalVideofile
let createTorrentAndSetInfoHash: VideoMethods.CreateTorrentAndSetInfoHash
let getOriginalFileHeight: VideoMethods.GetOriginalFileHeight
let getEmbedPath: VideoMethods.GetEmbedPath
+let getDescriptionPath: VideoMethods.GetDescriptionPath
+let getTruncatedDescription: VideoMethods.GetTruncatedDescription
let generateThumbnailFromData: VideoMethods.GenerateThumbnailFromData
let list: VideoMethods.List
let listOwnedByAuthor: VideoMethods.ListOwnedByAuthor
let load: VideoMethods.Load
let loadByUUID: VideoMethods.LoadByUUID
+let loadLocalVideoByUUID: VideoMethods.LoadLocalVideoByUUID
let loadAndPopulateAuthor: VideoMethods.LoadAndPopulateAuthor
let loadAndPopulateAuthorAndPodAndTags: VideoMethods.LoadAndPopulateAuthorAndPodAndTags
let loadByUUIDAndPopulateAuthorAndPodAndTags: VideoMethods.LoadByUUIDAndPopulateAuthorAndPodAndTags
}
},
description: {
- type: DataTypes.STRING,
+ type: DataTypes.STRING(CONSTRAINTS_FIELDS.VIDEOS.DESCRIPTION.max),
allowNull: false,
validate: {
descriptionValid: value => {
},
{
indexes: [
- {
- fields: [ 'authorId' ]
- },
{
fields: [ 'name' ]
},
},
{
fields: [ 'uuid' ]
+ },
+ {
+ fields: [ 'channelId' ]
}
],
hooks: {
loadAndPopulateAuthorAndPodAndTags,
loadByHostAndUUID,
loadByUUID,
+ loadLocalVideoByUUID,
loadByUUIDAndPopulateAuthorAndPodAndTags,
searchAndPopulateAuthorAndPodAndTags
]
createPreview,
createThumbnail,
createTorrentAndSetInfoHash,
- generateMagnetUri,
getPreviewName,
getPreviewPath,
getThumbnailName,
removeTorrent,
toAddRemoteJSON,
toFormattedJSON,
+ toFormattedDetailsJSON,
toUpdateRemoteJSON,
optimizeOriginalVideofile,
transcodeOriginalVideofile,
getOriginalFileHeight,
- getEmbedPath
+ getEmbedPath,
+ getTruncatedDescription,
+ getDescriptionPath
]
addMethodsToModel(Video, classMethods, instanceMethods)
// ------------------------------ METHODS ------------------------------
function associate (models) {
- Video.belongsTo(models.Author, {
+ Video.belongsTo(models.VideoChannel, {
foreignKey: {
- name: 'authorId',
+ name: 'channelId',
allowNull: false
},
onDelete: 'cascade'
})
}
-function afterDestroy (video: VideoInstance, options: { transaction: Sequelize.Transaction }) {
+function afterDestroy (video: VideoInstance) {
const tasks = []
tasks.push(
tasks.push(
video.removePreview(),
- removeVideoToFriends(removeVideoToFriendsParams, options.transaction)
+ removeVideoToFriends(removeVideoToFriendsParams)
)
// Remove physical files and torrents
video.VideoFiles.forEach(file => {
- video.removeFile(file),
- video.removeTorrent(file)
+ tasks.push(video.removeFile(file))
+ tasks.push(video.removeTorrent(file))
})
}
return Promise.all(tasks)
+ .catch(err => {
+ logger.error('Some errors when removing files of video %s in after destroy hook.', video.uuid, err)
+ })
}
getOriginalFile = function (this: VideoInstance) {
})
}
-generateMagnetUri = function (this: VideoInstance, videoFile: VideoFileInstance) {
- let baseUrlHttp
- let baseUrlWs
-
- if (this.isOwned()) {
- baseUrlHttp = CONFIG.WEBSERVER.URL
- baseUrlWs = CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT
- } else {
- baseUrlHttp = REMOTE_SCHEME.HTTP + '://' + this.Author.Pod.host
- baseUrlWs = REMOTE_SCHEME.WS + '://' + this.Author.Pod.host
- }
-
- const xs = baseUrlHttp + STATIC_PATHS.TORRENTS + this.getTorrentFileName(videoFile)
- const announce = [ baseUrlWs + '/tracker/socket' ]
- const urlList = [ baseUrlHttp + STATIC_PATHS.WEBSEED + this.getVideoFilename(videoFile) ]
-
- const magnetHash = {
- xs,
- announce,
- urlList,
- infoHash: videoFile.infoHash,
- name: this.name
- }
-
- return magnetUtil.encode(magnetHash)
-}
-
getEmbedPath = function (this: VideoInstance) {
return '/videos/embed/' + this.uuid
}
toFormattedJSON = function (this: VideoInstance) {
let podHost
- if (this.Author.Pod) {
- podHost = this.Author.Pod.host
+ if (this.VideoChannel.Author.Pod) {
+ podHost = this.VideoChannel.Author.Pod.host
} else {
// It means it's our video
podHost = CONFIG.WEBSERVER.HOST
language: this.language,
languageLabel,
nsfw: this.nsfw,
- description: this.description,
+ description: this.getTruncatedDescription(),
podHost,
isLocal: this.isOwned(),
- author: this.Author.name,
+ author: this.VideoChannel.Author.name,
duration: this.duration,
views: this.views,
likes: this.likes,
previewPath: this.getPreviewPath(),
embedPath: this.getEmbedPath(),
createdAt: this.createdAt,
- updatedAt: this.updatedAt,
+ updatedAt: this.updatedAt
+ }
+
+ return json
+}
+
+toFormattedDetailsJSON = function (this: VideoInstance) {
+ const formattedJson = this.toFormattedJSON()
+
+ const detailsJson = {
+ descriptionPath: this.getDescriptionPath(),
+ channel: this.VideoChannel.toFormattedJSON(),
files: []
}
// Format and sort video files
- json.files = this.VideoFiles
+ const { baseUrlHttp, baseUrlWs } = getBaseUrls(this)
+ detailsJson.files = this.VideoFiles
.map(videoFile => {
let resolutionLabel = videoFile.resolution + 'p'
const videoFileJson = {
resolution: videoFile.resolution,
resolutionLabel,
- magnetUri: this.generateMagnetUri(videoFile),
- size: videoFile.size
+ magnetUri: generateMagnetUri(this, videoFile, baseUrlHttp, baseUrlWs),
+ size: videoFile.size,
+ torrentUrl: getTorrentUrl(this, videoFile, baseUrlHttp),
+ fileUrl: getVideoFileUrl(this, videoFile, baseUrlHttp)
}
return videoFileJson
return -1
})
- return json
+ return Object.assign(formattedJson, detailsJson)
}
toAddRemoteJSON = function (this: VideoInstance) {
licence: this.licence,
language: this.language,
nsfw: this.nsfw,
- description: this.description,
- author: this.Author.name,
+ truncatedDescription: this.getTruncatedDescription(),
+ channelUUID: this.VideoChannel.uuid,
duration: this.duration,
thumbnailData: thumbnailData.toString('binary'),
tags: map<TagInstance, string>(this.Tags, 'name'),
licence: this.licence,
language: this.language,
nsfw: this.nsfw,
- description: this.description,
- author: this.Author.name,
+ truncatedDescription: this.getTruncatedDescription(),
duration: this.duration,
tags: map<TagInstance, string>(this.Tags, 'name'),
createdAt: this.createdAt,
return json
}
+getTruncatedDescription = function (this: VideoInstance) {
+ const options = {
+ length: CONSTRAINTS_FIELDS.VIDEOS.TRUNCATED_DESCRIPTION.max
+ }
+
+ return truncate(this.description, options)
+}
+
optimizeOriginalVideofile = function (this: VideoInstance) {
const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
const newExtname = '.mp4'
return getVideoFileHeight(originalFilePath)
}
+getDescriptionPath = function (this: VideoInstance) {
+ return `/api/${API_VERSION}/videos/${this.uuid}/description`
+}
+
removeThumbnail = function (this: VideoInstance) {
const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, this.getThumbnailName())
return unlinkPromise(thumbnailPath)
order: [ getSort(sort), [ Video['sequelize'].models.Tag, 'name', 'ASC' ] ],
include: [
{
- model: Video['sequelize'].models.Author,
- include: [ { model: Video['sequelize'].models.Pod, required: false } ]
+ model: Video['sequelize'].models.VideoChannel,
+ include: [
+ {
+ model: Video['sequelize'].models.Author,
+ include: [
+ {
+ model: Video['sequelize'].models.Pod,
+ required: false
+ }
+ ]
+ }
+ ]
},
Video['sequelize'].models.Tag,
Video['sequelize'].models.VideoFile
})
}
-loadByHostAndUUID = function (fromHost: string, uuid: string) {
- const query = {
+loadByHostAndUUID = function (fromHost: string, uuid: string, t?: Sequelize.Transaction) {
+ const query: Sequelize.FindOptions<VideoAttributes> = {
where: {
uuid
},
model: Video['sequelize'].models.VideoFile
},
{
- model: Video['sequelize'].models.Author,
+ model: Video['sequelize'].models.VideoChannel,
include: [
{
- model: Video['sequelize'].models.Pod,
- required: true,
- where: {
- host: fromHost
- }
+ model: Video['sequelize'].models.Author,
+ include: [
+ {
+ model: Video['sequelize'].models.Pod,
+ required: true,
+ where: {
+ host: fromHost
+ }
+ }
+ ]
}
]
}
]
}
+ if (t !== undefined) query.transaction = t
+
return Video.findOne(query)
}
},
include: [
Video['sequelize'].models.VideoFile,
- Video['sequelize'].models.Author,
+ {
+ model: Video['sequelize'].models.VideoChannel,
+ include: [ Video['sequelize'].models.Author ]
+ },
Video['sequelize'].models.Tag
]
}
model: Video['sequelize'].models.VideoFile
},
{
- model: Video['sequelize'].models.Author,
- where: {
- name: author
- }
+ model: Video['sequelize'].models.VideoChannel,
+ include: [
+ {
+ model: Video['sequelize'].models.Author,
+ where: {
+ name: author
+ }
+ }
+ ]
}
]
}
return Video.findById(id)
}
-loadByUUID = function (uuid: string) {
- const query = {
+loadByUUID = function (uuid: string, t?: Sequelize.Transaction) {
+ const query: Sequelize.FindOptions<VideoAttributes> = {
where: {
uuid
},
include: [ Video['sequelize'].models.VideoFile ]
}
+
+ if (t !== undefined) query.transaction = t
+
+ return Video.findOne(query)
+}
+
+loadLocalVideoByUUID = function (uuid: string, t?: Sequelize.Transaction) {
+ const query: Sequelize.FindOptions<VideoAttributes> = {
+ where: {
+ uuid,
+ remote: false
+ },
+ include: [ Video['sequelize'].models.VideoFile ]
+ }
+
+ if (t !== undefined) query.transaction = t
+
return Video.findOne(query)
}
loadAndPopulateAuthor = function (id: number) {
const options = {
- include: [ Video['sequelize'].models.VideoFile, Video['sequelize'].models.Author ]
+ include: [
+ Video['sequelize'].models.VideoFile,
+ {
+ model: Video['sequelize'].models.VideoChannel,
+ include: [ Video['sequelize'].models.Author ]
+ }
+ ]
}
return Video.findById(id, options)
const options = {
include: [
{
- model: Video['sequelize'].models.Author,
- include: [ { model: Video['sequelize'].models.Pod, required: false } ]
+ model: Video['sequelize'].models.VideoChannel,
+ include: [
+ {
+ model: Video['sequelize'].models.Author,
+ include: [ { model: Video['sequelize'].models.Pod, required: false } ]
+ }
+ ]
},
Video['sequelize'].models.Tag,
Video['sequelize'].models.VideoFile
},
include: [
{
- model: Video['sequelize'].models.Author,
- include: [ { model: Video['sequelize'].models.Pod, required: false } ]
+ model: Video['sequelize'].models.VideoChannel,
+ include: [
+ {
+ model: Video['sequelize'].models.Author,
+ include: [ { model: Video['sequelize'].models.Pod, required: false } ]
+ }
+ ]
},
Video['sequelize'].models.Tag,
Video['sequelize'].models.VideoFile
const authorInclude: Sequelize.IncludeOptions = {
model: Video['sequelize'].models.Author,
- include: [
- podInclude
- ]
+ include: [ podInclude ]
+ }
+
+ const videoChannelInclude: Sequelize.IncludeOptions = {
+ model: Video['sequelize'].models.VideoChannel,
+ include: [ authorInclude ],
+ required: true
}
const tagInclude: Sequelize.IncludeOptions = {
}
} else if (field === 'tags') {
const escapedValue = Video['sequelize'].escape('%' + value + '%')
- query.where['id'].$in = Video['sequelize'].literal(
+ query.where['id'][Sequelize.Op.in] = Video['sequelize'].literal(
`(SELECT "VideoTags"."videoId"
FROM "Tags"
INNER JOIN "VideoTags" ON "Tags"."id" = "VideoTags"."tagId"
// FIXME: Include our pod? (not stored in the database)
podInclude.where = {
host: {
- $iLike: '%' + value + '%'
+ [Sequelize.Op.iLike]: '%' + value + '%'
}
}
podInclude.required = true
} else if (field === 'author') {
authorInclude.where = {
name: {
- $iLike: '%' + value + '%'
+ [Sequelize.Op.iLike]: '%' + value + '%'
}
}
-
- // authorInclude.or = true
} else {
query.where[field] = {
- $iLike: '%' + value + '%'
+ [Sequelize.Op.iLike]: '%' + value + '%'
}
}
query.include = [
- authorInclude, tagInclude, videoFileInclude
+ videoChannelInclude, tagInclude, videoFileInclude
]
return Video.findAndCountAll(query).then(({ rows, count }) => {
function createBaseVideosWhere () {
return {
id: {
- $notIn: Video['sequelize'].literal(
+ [Sequelize.Op.notIn]: Video['sequelize'].literal(
'(SELECT "BlacklistedVideos"."videoId" FROM "BlacklistedVideos")'
)
}
}
}
+
+function getBaseUrls (video: VideoInstance) {
+ let baseUrlHttp
+ let baseUrlWs
+
+ if (video.isOwned()) {
+ baseUrlHttp = CONFIG.WEBSERVER.URL
+ baseUrlWs = CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT
+ } else {
+ baseUrlHttp = REMOTE_SCHEME.HTTP + '://' + video.VideoChannel.Author.Pod.host
+ baseUrlWs = REMOTE_SCHEME.WS + '://' + video.VideoChannel.Author.Pod.host
+ }
+
+ return { baseUrlHttp, baseUrlWs }
+}
+
+function getTorrentUrl (video: VideoInstance, videoFile: VideoFileInstance, baseUrlHttp: string) {
+ return baseUrlHttp + STATIC_PATHS.TORRENTS + video.getTorrentFileName(videoFile)
+}
+
+function getVideoFileUrl (video: VideoInstance, videoFile: VideoFileInstance, baseUrlHttp: string) {
+ return baseUrlHttp + STATIC_PATHS.WEBSEED + video.getVideoFilename(videoFile)
+}
+
+function generateMagnetUri (video: VideoInstance, videoFile: VideoFileInstance, baseUrlHttp: string, baseUrlWs: string) {
+ const xs = getTorrentUrl(video, videoFile, baseUrlHttp)
+ const announce = [ baseUrlWs + '/tracker/socket', baseUrlHttp + '/tracker/announce' ]
+ const urlList = [ getVideoFileUrl(video, videoFile, baseUrlHttp) ]
+
+ const magnetHash = {
+ xs,
+ announce,
+ urlList,
+ infoHash: videoFile.infoHash,
+ name: video.name
+ }
+
+ return magnetUtil.encode(magnetHash)
+}