import * as safeBuffer from 'safe-buffer'
const Buffer = safeBuffer.Buffer
-import * as ffmpeg from 'fluent-ffmpeg'
import * as magnetUtil from 'magnet-uri'
import { map } from 'lodash'
import * as parseTorrent from 'parse-torrent'
import { join } from 'path'
import * as Sequelize from 'sequelize'
import * as Promise from 'bluebird'
+import { maxBy, truncate } from 'lodash'
import { TagInstance } from './tag-interface'
-import { UserInstance } from '../user/user-interface'
import {
logger,
isVideoNameValid,
unlinkPromise,
renamePromise,
writeFilePromise,
- createTorrentPromise
+ createTorrentPromise,
+ statPromise,
+ generateImageFromVideoFile,
+ transcode,
+ getVideoFileHeight
} from '../../helpers'
import {
CONFIG,
VIDEO_LICENCES,
VIDEO_LANGUAGES,
THUMBNAILS_SIZE,
- VIDEO_FILE_RESOLUTIONS
+ PREVIEWS_SIZE,
+ CONSTRAINTS_FIELDS,
+ API_VERSION
} from '../../initializers'
import { removeVideoToFriends } from '../../lib'
-import { VideoFileInstance } from './video-file-interface'
+import { VideoResolution } from '../../../shared'
+import { VideoFileInstance, VideoFileModel } from './video-file-interface'
import { addMethodsToModel, getSort } from '../utils'
import {
} from './video-interface'
let Video: Sequelize.Model<VideoInstance, VideoAttributes>
-let generateMagnetUri: VideoMethods.GenerateMagnetUri
+let getOriginalFile: VideoMethods.GetOriginalFile
let getVideoFilename: VideoMethods.GetVideoFilename
let getThumbnailName: VideoMethods.GetThumbnailName
+let getThumbnailPath: VideoMethods.GetThumbnailPath
let getPreviewName: VideoMethods.GetPreviewName
+let getPreviewPath: VideoMethods.GetPreviewPath
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 transcodeVideofile: VideoMethods.TranscodeVideofile
+let optimizeOriginalVideofile: VideoMethods.OptimizeOriginalVideofile
+let transcodeOriginalVideofile: VideoMethods.TranscodeOriginalVideofile
let createPreview: VideoMethods.CreatePreview
let createThumbnail: VideoMethods.CreateThumbnail
let getVideoFilePath: VideoMethods.GetVideoFilePath
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 getDurationFromFile: VideoMethods.GetDurationFromFile
let list: VideoMethods.List
let listForApi: VideoMethods.ListForApi
let loadByHostAndUUID: VideoMethods.LoadByHostAndUUID
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: {
associate,
generateThumbnailFromData,
- getDurationFromFile,
list,
listForApi,
listOwnedAndPopulateAuthorAndTags,
loadAndPopulateAuthorAndPodAndTags,
loadByHostAndUUID,
loadByUUID,
+ loadLocalVideoByUUID,
loadByUUIDAndPopulateAuthorAndPodAndTags,
searchAndPopulateAuthorAndPodAndTags
]
createPreview,
createThumbnail,
createTorrentAndSetInfoHash,
- generateMagnetUri,
getPreviewName,
+ getPreviewPath,
getThumbnailName,
+ getThumbnailPath,
getTorrentFileName,
getVideoFilename,
getVideoFilePath,
+ getOriginalFile,
isOwned,
removeFile,
removePreview,
removeTorrent,
toAddRemoteJSON,
toFormattedJSON,
+ toFormattedDetailsJSON,
toUpdateRemoteJSON,
- transcodeVideofile
+ optimizeOriginalVideofile,
+ transcodeOriginalVideofile,
+ getOriginalFileHeight,
+ 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'
removeVideoToFriends(removeVideoToFriendsParams)
)
- // TODO: check files is populated
+ // 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) {
+ if (Array.isArray(this.VideoFiles) === false) return undefined
+
+ // The original file is the file that have the higher resolution
+ return maxBy(this.VideoFiles, file => file.resolution)
}
getVideoFilename = function (this: VideoInstance, videoFile: VideoFileInstance) {
- // return this.uuid + '-' + VIDEO_FILE_RESOLUTIONS[videoFile.resolution] + videoFile.extname
- return this.uuid + videoFile.extname
+ return this.uuid + '-' + videoFile.resolution + videoFile.extname
}
getThumbnailName = function (this: VideoInstance) {
getTorrentFileName = function (this: VideoInstance, videoFile: VideoFileInstance) {
const extension = '.torrent'
- // return this.uuid + '-' + VIDEO_FILE_RESOLUTIONS[videoFile.resolution] + extension
- return this.uuid + extension
+ return this.uuid + '-' + videoFile.resolution + extension
}
isOwned = function (this: VideoInstance) {
}
createPreview = function (this: VideoInstance, videoFile: VideoFileInstance) {
- return generateImage(this, this.getVideoFilePath(videoFile), CONFIG.STORAGE.PREVIEWS_DIR, this.getPreviewName(), null)
+ const imageSize = PREVIEWS_SIZE.width + 'x' + PREVIEWS_SIZE.height
+
+ return generateImageFromVideoFile(
+ this.getVideoFilePath(videoFile),
+ CONFIG.STORAGE.PREVIEWS_DIR,
+ this.getPreviewName(),
+ imageSize
+ )
}
createThumbnail = function (this: VideoInstance, videoFile: VideoFileInstance) {
- return generateImage(this, this.getVideoFilePath(videoFile), CONFIG.STORAGE.THUMBNAILS_DIR, this.getThumbnailName(), THUMBNAILS_SIZE)
+ const imageSize = THUMBNAILS_SIZE.width + 'x' + THUMBNAILS_SIZE.height
+
+ return generateImageFromVideoFile(
+ this.getVideoFilePath(videoFile),
+ CONFIG.STORAGE.THUMBNAILS_DIR,
+ this.getThumbnailName(),
+ imageSize
+ )
}
getVideoFilePath = function (this: VideoInstance, videoFile: VideoFileInstance) {
return createTorrentPromise(this.getVideoFilePath(videoFile), options)
.then(torrent => {
const filePath = join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile))
+ logger.info('Creating torrent %s.', filePath)
+
return writeFilePromise(filePath, torrent).then(() => torrent)
})
.then(torrent => {
})
}
-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) ]
+getEmbedPath = function (this: VideoInstance) {
+ return '/videos/embed/' + this.uuid
+}
- const magnetHash = {
- xs,
- announce,
- urlList,
- infoHash: videoFile.infoHash,
- name: this.name
- }
+getThumbnailPath = function (this: VideoInstance) {
+ return join(STATIC_PATHS.THUMBNAILS, this.getThumbnailName())
+}
- return magnetUtil.encode(magnetHash)
+getPreviewPath = function (this: VideoInstance) {
+ return join(STATIC_PATHS.PREVIEWS, this.getPreviewName())
}
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,
dislikes: this.dislikes,
tags: map<TagInstance, string>(this.Tags, 'name'),
- thumbnailPath: join(STATIC_PATHS.THUMBNAILS, this.getThumbnailName()),
- previewPath: join(STATIC_PATHS.PREVIEWS, this.getPreviewName()),
+ thumbnailPath: this.getThumbnailPath(),
+ previewPath: this.getPreviewPath(),
+ embedPath: this.getEmbedPath(),
createdAt: this.createdAt,
- updatedAt: this.updatedAt,
- files: []
+ updatedAt: this.updatedAt
}
- this.VideoFiles.forEach(videoFile => {
- let resolutionLabel = VIDEO_FILE_RESOLUTIONS[videoFile.resolution]
- if (!resolutionLabel) resolutionLabel = 'Unknown'
+ return json
+}
- const videoFileJson = {
- resolution: videoFile.resolution,
- resolutionLabel,
- magnetUri: this.generateMagnetUri(videoFile),
- size: videoFile.size
- }
+toFormattedDetailsJSON = function (this: VideoInstance) {
+ const formattedJson = this.toFormattedJSON()
- json.files.push(videoFileJson)
- })
+ const detailsJson = {
+ descriptionPath: this.getDescriptionPath(),
+ channel: this.VideoChannel.toFormattedJSON(),
+ files: []
+ }
- return json
+ // Format and sort video files
+ const { baseUrlHttp, baseUrlWs } = getBaseUrls(this)
+ detailsJson.files = this.VideoFiles
+ .map(videoFile => {
+ let resolutionLabel = videoFile.resolution + 'p'
+
+ const videoFileJson = {
+ resolution: videoFile.resolution,
+ resolutionLabel,
+ magnetUri: generateMagnetUri(this, videoFile, baseUrlHttp, baseUrlWs),
+ size: videoFile.size,
+ torrentUrl: getTorrentUrl(this, videoFile, baseUrlHttp),
+ fileUrl: getVideoFileUrl(this, videoFile, baseUrlHttp)
+ }
+
+ return videoFileJson
+ })
+ .sort((a, b) => {
+ if (a.resolution < b.resolution) return 1
+ if (a.resolution === b.resolution) return 0
+ return -1
+ })
+
+ 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
}
-transcodeVideofile = function (this: VideoInstance, inputVideoFile: VideoFileInstance) {
+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'
+ const inputVideoFile = this.getOriginalFile()
const videoInputPath = join(videosDirectory, this.getVideoFilename(inputVideoFile))
const videoOutputPath = join(videosDirectory, this.id + '-transcoded' + newExtname)
- return new Promise<void>((res, rej) => {
- ffmpeg(videoInputPath)
- .output(videoOutputPath)
- .videoCodec('libx264')
- .outputOption('-threads ' + CONFIG.TRANSCODING.THREADS)
- .outputOption('-movflags faststart')
- .on('error', rej)
- .on('end', () => {
-
- return unlinkPromise(videoInputPath)
- .then(() => {
- // Important to do this before getVideoFilename() to take in account the new file extension
- inputVideoFile.set('extname', newExtname)
-
- return renamePromise(videoOutputPath, this.getVideoFilePath(inputVideoFile))
- })
- .then(() => {
- return this.createTorrentAndSetInfoHash(inputVideoFile)
- })
- .then(() => {
- return inputVideoFile.save()
- })
- .then(() => {
- return res()
- })
- .catch(err => {
- // Auto destruction...
- this.destroy().catch(err => logger.error('Cannot destruct video after transcoding failure.', err))
-
- return rej(err)
- })
- })
- .run()
+ const transcodeOptions = {
+ inputPath: videoInputPath,
+ outputPath: videoOutputPath
+ }
+
+ return transcode(transcodeOptions)
+ .then(() => {
+ return unlinkPromise(videoInputPath)
+ })
+ .then(() => {
+ // Important to do this before getVideoFilename() to take in account the new file extension
+ inputVideoFile.set('extname', newExtname)
+
+ return renamePromise(videoOutputPath, this.getVideoFilePath(inputVideoFile))
+ })
+ .then(() => {
+ return statPromise(this.getVideoFilePath(inputVideoFile))
+ })
+ .then(stats => {
+ return inputVideoFile.set('size', stats.size)
+ })
+ .then(() => {
+ return this.createTorrentAndSetInfoHash(inputVideoFile)
+ })
+ .then(() => {
+ return inputVideoFile.save()
+ })
+ .then(() => {
+ return undefined
+ })
+ .catch(err => {
+ // Auto destruction...
+ this.destroy().catch(err => logger.error('Cannot destruct video after transcoding failure.', err))
+
+ throw err
+ })
+}
+
+transcodeOriginalVideofile = function (this: VideoInstance, resolution: VideoResolution) {
+ const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
+ const extname = '.mp4'
+
+ // We are sure it's x264 in mp4 because optimizeOriginalVideofile was already executed
+ const videoInputPath = join(videosDirectory, this.getVideoFilename(this.getOriginalFile()))
+
+ const newVideoFile = (Video['sequelize'].models.VideoFile as VideoFileModel).build({
+ resolution,
+ extname,
+ size: 0,
+ videoId: this.id
})
+ const videoOutputPath = join(videosDirectory, this.getVideoFilename(newVideoFile))
+
+ const transcodeOptions = {
+ inputPath: videoInputPath,
+ outputPath: videoOutputPath,
+ resolution
+ }
+ return transcode(transcodeOptions)
+ .then(() => {
+ return statPromise(videoOutputPath)
+ })
+ .then(stats => {
+ newVideoFile.set('size', stats.size)
+
+ return undefined
+ })
+ .then(() => {
+ return this.createTorrentAndSetInfoHash(newVideoFile)
+ })
+ .then(() => {
+ return newVideoFile.save()
+ })
+ .then(() => {
+ return this.VideoFiles.push(newVideoFile)
+ })
+ .then(() => undefined)
+}
+
+getOriginalFileHeight = function (this: VideoInstance) {
+ const originalFilePath = this.getVideoFilePath(this.getOriginalFile())
+
+ return getVideoFileHeight(originalFilePath)
+}
+
+getDescriptionPath = function (this: VideoInstance) {
+ return `/api/${API_VERSION}/videos/${this.uuid}/description`
}
removeThumbnail = function (this: VideoInstance) {
})
}
-getDurationFromFile = function (videoPath: string) {
- return new Promise<number>((res, rej) => {
- ffmpeg.ffprobe(videoPath, (err, metadata) => {
- if (err) return rej(err)
-
- return res(Math.floor(metadata.format.duration))
- })
- })
-}
-
list = function () {
const query = {
include: [ Video['sequelize'].models.VideoFile ]
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 generateImage (video: VideoInstance, videoPath: string, folder: string, imageName: string, size: string) {
- const options = {
- filename: imageName,
- count: 1,
- folder
+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
}
- if (size) {
- options['size'] = size
+ 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 new Promise<string>((res, rej) => {
- ffmpeg(videoPath)
- .on('error', rej)
- .on('end', () => res(imageName))
- .thumbnail(options)
- })
+ return magnetUtil.encode(magnetHash)
}