2016-06-24 10:42:51 -05:00
|
|
|
'use strict'
|
|
|
|
|
2016-07-26 15:30:46 -05:00
|
|
|
const createTorrent = require('create-torrent')
|
2016-06-24 10:42:51 -05:00
|
|
|
const ffmpeg = require('fluent-ffmpeg')
|
|
|
|
const fs = require('fs')
|
2016-11-11 08:20:03 -06:00
|
|
|
const magnetUtil = require('magnet-uri')
|
2016-07-18 10:17:52 -05:00
|
|
|
const parallel = require('async/parallel')
|
2016-07-26 15:30:46 -05:00
|
|
|
const parseTorrent = require('parse-torrent')
|
2016-06-24 10:42:51 -05:00
|
|
|
const pathUtils = require('path')
|
|
|
|
const mongoose = require('mongoose')
|
|
|
|
|
|
|
|
const constants = require('../initializers/constants')
|
2016-07-31 13:58:43 -05:00
|
|
|
const customVideosValidators = require('../helpers/custom-validators').videos
|
2016-06-24 10:42:51 -05:00
|
|
|
const logger = require('../helpers/logger')
|
2016-08-16 14:51:04 -05:00
|
|
|
const modelUtils = require('./utils')
|
2016-06-24 10:42:51 -05:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// TODO: add indexes on searchable columns
|
|
|
|
const VideoSchema = mongoose.Schema({
|
|
|
|
name: String,
|
2016-11-11 06:47:50 -06:00
|
|
|
extname: {
|
|
|
|
type: String,
|
|
|
|
enum: [ '.mp4', '.webm', '.ogv' ]
|
|
|
|
},
|
|
|
|
remoteId: mongoose.Schema.Types.ObjectId,
|
2016-06-24 10:42:51 -05:00
|
|
|
description: String,
|
2016-11-11 08:20:03 -06:00
|
|
|
magnet: {
|
|
|
|
infoHash: String
|
|
|
|
},
|
2016-11-14 13:03:04 -06:00
|
|
|
podHost: String,
|
2016-06-24 10:42:51 -05:00
|
|
|
author: String,
|
|
|
|
duration: Number,
|
|
|
|
tags: [ String ],
|
|
|
|
createdDate: {
|
|
|
|
type: Date,
|
|
|
|
default: Date.now
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2016-07-31 13:58:43 -05:00
|
|
|
VideoSchema.path('name').validate(customVideosValidators.isVideoNameValid)
|
|
|
|
VideoSchema.path('description').validate(customVideosValidators.isVideoDescriptionValid)
|
2016-11-14 13:03:04 -06:00
|
|
|
VideoSchema.path('podHost').validate(customVideosValidators.isVideoPodHostValid)
|
2016-07-31 13:58:43 -05:00
|
|
|
VideoSchema.path('author').validate(customVideosValidators.isVideoAuthorValid)
|
|
|
|
VideoSchema.path('duration').validate(customVideosValidators.isVideoDurationValid)
|
|
|
|
VideoSchema.path('tags').validate(customVideosValidators.isVideoTagsValid)
|
2016-06-24 10:42:51 -05:00
|
|
|
|
|
|
|
VideoSchema.methods = {
|
2016-11-11 08:20:03 -06:00
|
|
|
generateMagnetUri,
|
|
|
|
getVideoFilename,
|
|
|
|
getThumbnailName,
|
|
|
|
getPreviewName,
|
2016-11-11 06:47:50 -06:00
|
|
|
getTorrentName,
|
2016-10-02 05:19:02 -05:00
|
|
|
isOwned,
|
|
|
|
toFormatedJSON,
|
|
|
|
toRemoteJSON
|
2016-06-24 10:42:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
VideoSchema.statics = {
|
2016-11-16 14:16:41 -06:00
|
|
|
generateThumbnailFromBase64,
|
2016-10-02 05:19:02 -05:00
|
|
|
getDurationFromFile,
|
|
|
|
listForApi,
|
2016-11-14 13:03:04 -06:00
|
|
|
listByHostAndRemoteId,
|
|
|
|
listByHost,
|
2016-10-02 05:19:02 -05:00
|
|
|
listOwned,
|
|
|
|
listOwnedByAuthor,
|
|
|
|
listRemotes,
|
|
|
|
load,
|
2016-10-02 08:39:09 -05:00
|
|
|
search
|
2016-06-24 10:42:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
VideoSchema.pre('remove', function (next) {
|
|
|
|
const video = this
|
|
|
|
const tasks = []
|
|
|
|
|
|
|
|
tasks.push(
|
|
|
|
function (callback) {
|
|
|
|
removeThumbnail(video, callback)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
if (video.isOwned()) {
|
|
|
|
tasks.push(
|
|
|
|
function (callback) {
|
|
|
|
removeFile(video, callback)
|
|
|
|
},
|
|
|
|
function (callback) {
|
|
|
|
removeTorrent(video, callback)
|
2016-11-11 04:52:24 -06:00
|
|
|
},
|
|
|
|
function (callback) {
|
|
|
|
removePreview(video, callback)
|
2016-06-24 10:42:51 -05:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2016-07-18 10:17:52 -05:00
|
|
|
parallel(tasks, next)
|
2016-06-24 10:42:51 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
VideoSchema.pre('save', function (next) {
|
|
|
|
const video = this
|
|
|
|
const tasks = []
|
|
|
|
|
|
|
|
if (video.isOwned()) {
|
2016-11-11 08:20:03 -06:00
|
|
|
const videoPath = pathUtils.join(constants.CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename())
|
2016-11-14 13:03:04 -06:00
|
|
|
this.podHost = constants.CONFIG.WEBSERVER.HOST
|
2016-06-24 10:42:51 -05:00
|
|
|
|
|
|
|
tasks.push(
|
2016-07-26 15:30:46 -05:00
|
|
|
// TODO: refractoring
|
2016-06-24 10:42:51 -05:00
|
|
|
function (callback) {
|
2016-10-17 14:10:29 -05:00
|
|
|
const options = {
|
|
|
|
announceList: [
|
2016-10-23 12:41:17 -05:00
|
|
|
[ constants.CONFIG.WEBSERVER.WS + '://' + constants.CONFIG.WEBSERVER.HOSTNAME + ':' + constants.CONFIG.WEBSERVER.PORT + '/tracker/socket' ]
|
2016-10-17 14:10:29 -05:00
|
|
|
],
|
|
|
|
urlList: [
|
2016-11-11 08:20:03 -06:00
|
|
|
constants.CONFIG.WEBSERVER.URL + constants.STATIC_PATHS.WEBSEED + video.getVideoFilename()
|
2016-10-17 14:10:29 -05:00
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
createTorrent(videoPath, options, function (err, torrent) {
|
2016-07-26 15:30:46 -05:00
|
|
|
if (err) return callback(err)
|
|
|
|
|
2016-11-11 06:47:50 -06:00
|
|
|
fs.writeFile(constants.CONFIG.STORAGE.TORRENTS_DIR + video.getTorrentName(), torrent, function (err) {
|
2016-07-26 15:30:46 -05:00
|
|
|
if (err) return callback(err)
|
|
|
|
|
|
|
|
const parsedTorrent = parseTorrent(torrent)
|
2016-11-11 08:20:03 -06:00
|
|
|
video.magnet.infoHash = parsedTorrent.infoHash
|
2016-07-26 15:30:46 -05:00
|
|
|
|
|
|
|
callback(null)
|
|
|
|
})
|
|
|
|
})
|
2016-06-24 10:42:51 -05:00
|
|
|
},
|
|
|
|
function (callback) {
|
2016-11-11 06:47:50 -06:00
|
|
|
createThumbnail(video, videoPath, callback)
|
2016-11-11 04:52:24 -06:00
|
|
|
},
|
|
|
|
function (callback) {
|
2016-11-11 06:47:50 -06:00
|
|
|
createPreview(video, videoPath, callback)
|
2016-06-24 10:42:51 -05:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2016-11-16 14:16:41 -06:00
|
|
|
return parallel(tasks, next)
|
2016-06-24 10:42:51 -05:00
|
|
|
}
|
2016-11-16 14:16:41 -06:00
|
|
|
|
|
|
|
return next()
|
2016-06-24 10:42:51 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
mongoose.model('Video', VideoSchema)
|
|
|
|
|
|
|
|
// ------------------------------ METHODS ------------------------------
|
|
|
|
|
2016-11-11 08:20:03 -06:00
|
|
|
function generateMagnetUri () {
|
|
|
|
let baseUrlHttp, baseUrlWs
|
|
|
|
|
|
|
|
if (this.isOwned()) {
|
|
|
|
baseUrlHttp = constants.CONFIG.WEBSERVER.URL
|
|
|
|
baseUrlWs = constants.CONFIG.WEBSERVER.WS + '://' + constants.CONFIG.WEBSERVER.HOSTNAME + ':' + constants.CONFIG.WEBSERVER.PORT
|
|
|
|
} else {
|
2016-11-14 13:03:04 -06:00
|
|
|
baseUrlHttp = constants.REMOTE_SCHEME.HTTP + '://' + this.podHost
|
2016-11-23 06:34:07 -06:00
|
|
|
baseUrlWs = constants.REMOTE_SCHEME.WS + '://' + this.podHost
|
2016-11-11 08:20:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
const xs = baseUrlHttp + constants.STATIC_PATHS.TORRENTS + this.getTorrentName()
|
|
|
|
const announce = baseUrlWs + '/tracker/socket'
|
|
|
|
const urlList = [ baseUrlHttp + constants.STATIC_PATHS.WEBSEED + this.getVideoFilename() ]
|
|
|
|
|
|
|
|
const magnetHash = {
|
|
|
|
xs,
|
|
|
|
announce,
|
|
|
|
urlList,
|
|
|
|
infoHash: this.magnet.infoHash,
|
|
|
|
name: this.name
|
|
|
|
}
|
|
|
|
|
|
|
|
return magnetUtil.encode(magnetHash)
|
2016-11-11 06:47:50 -06:00
|
|
|
}
|
|
|
|
|
2016-11-11 08:20:03 -06:00
|
|
|
function getVideoFilename () {
|
|
|
|
if (this.isOwned()) return this._id + this.extname
|
|
|
|
|
|
|
|
return this.remoteId + this.extname
|
|
|
|
}
|
|
|
|
|
|
|
|
function getThumbnailName () {
|
|
|
|
// We always have a copy of the thumbnail
|
2016-11-11 06:47:50 -06:00
|
|
|
return this._id + '.jpg'
|
|
|
|
}
|
|
|
|
|
2016-11-11 08:20:03 -06:00
|
|
|
function getPreviewName () {
|
|
|
|
const extension = '.jpg'
|
|
|
|
|
|
|
|
if (this.isOwned()) return this._id + extension
|
|
|
|
|
|
|
|
return this.remoteId + extension
|
|
|
|
}
|
|
|
|
|
2016-11-11 06:47:50 -06:00
|
|
|
function getTorrentName () {
|
2016-11-11 08:20:03 -06:00
|
|
|
const extension = '.torrent'
|
|
|
|
|
|
|
|
if (this.isOwned()) return this._id + extension
|
|
|
|
|
|
|
|
return this.remoteId + extension
|
2016-11-11 06:47:50 -06:00
|
|
|
}
|
|
|
|
|
2016-06-24 10:42:51 -05:00
|
|
|
function isOwned () {
|
2016-11-11 06:47:50 -06:00
|
|
|
return this.remoteId === null
|
2016-06-24 10:42:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function toFormatedJSON () {
|
|
|
|
const json = {
|
|
|
|
id: this._id,
|
|
|
|
name: this.name,
|
|
|
|
description: this.description,
|
2016-11-14 13:03:04 -06:00
|
|
|
podHost: this.podHost,
|
2016-06-24 10:42:51 -05:00
|
|
|
isLocal: this.isOwned(),
|
2016-11-11 08:20:03 -06:00
|
|
|
magnetUri: this.generateMagnetUri(),
|
2016-06-24 10:42:51 -05:00
|
|
|
author: this.author,
|
|
|
|
duration: this.duration,
|
|
|
|
tags: this.tags,
|
2016-11-11 08:20:03 -06:00
|
|
|
thumbnailPath: constants.STATIC_PATHS.THUMBNAILS + '/' + this.getThumbnailName(),
|
2016-06-24 10:42:51 -05:00
|
|
|
createdDate: this.createdDate
|
|
|
|
}
|
|
|
|
|
|
|
|
return json
|
|
|
|
}
|
|
|
|
|
|
|
|
function toRemoteJSON (callback) {
|
|
|
|
const self = this
|
|
|
|
|
|
|
|
// Convert thumbnail to base64
|
2016-11-11 08:20:03 -06:00
|
|
|
const thumbnailPath = pathUtils.join(constants.CONFIG.STORAGE.THUMBNAILS_DIR, this.getThumbnailName())
|
2016-11-11 06:47:50 -06:00
|
|
|
fs.readFile(thumbnailPath, function (err, thumbnailData) {
|
2016-06-24 10:42:51 -05:00
|
|
|
if (err) {
|
|
|
|
logger.error('Cannot read the thumbnail of the video')
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
const remoteVideo = {
|
|
|
|
name: self.name,
|
|
|
|
description: self.description,
|
2016-11-11 08:20:03 -06:00
|
|
|
magnet: self.magnet,
|
2016-11-11 06:47:50 -06:00
|
|
|
remoteId: self._id,
|
2016-06-24 10:42:51 -05:00
|
|
|
author: self.author,
|
|
|
|
duration: self.duration,
|
|
|
|
thumbnailBase64: new Buffer(thumbnailData).toString('base64'),
|
|
|
|
tags: self.tags,
|
|
|
|
createdDate: self.createdDate,
|
2016-11-22 15:34:59 -06:00
|
|
|
podHost: self.podHost,
|
|
|
|
extname: self.extname
|
2016-06-24 10:42:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return callback(null, remoteVideo)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------ STATICS ------------------------------
|
|
|
|
|
2016-11-16 14:16:41 -06:00
|
|
|
function generateThumbnailFromBase64 (video, thumbnailData, callback) {
|
|
|
|
// Creating the thumbnail for a remote video
|
|
|
|
|
|
|
|
const thumbnailName = video.getThumbnailName()
|
|
|
|
const thumbnailPath = constants.CONFIG.STORAGE.THUMBNAILS_DIR + thumbnailName
|
|
|
|
fs.writeFile(thumbnailPath, thumbnailData, { encoding: 'base64' }, function (err) {
|
|
|
|
if (err) return callback(err)
|
|
|
|
|
|
|
|
return callback(null, thumbnailName)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-06-24 10:42:51 -05:00
|
|
|
function getDurationFromFile (videoPath, callback) {
|
|
|
|
ffmpeg.ffprobe(videoPath, function (err, metadata) {
|
|
|
|
if (err) return callback(err)
|
|
|
|
|
|
|
|
return callback(null, Math.floor(metadata.format.duration))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-08-16 14:51:04 -05:00
|
|
|
function listForApi (start, count, sort, callback) {
|
2016-06-24 10:42:51 -05:00
|
|
|
const query = {}
|
2016-08-16 15:31:45 -05:00
|
|
|
return modelUtils.listForApiWithCount.call(this, query, start, count, sort, callback)
|
2016-06-24 10:42:51 -05:00
|
|
|
}
|
|
|
|
|
2016-11-14 13:03:04 -06:00
|
|
|
function listByHostAndRemoteId (fromHost, remoteId, callback) {
|
|
|
|
this.find({ podHost: fromHost, remoteId: remoteId }, callback)
|
2016-06-24 10:42:51 -05:00
|
|
|
}
|
|
|
|
|
2016-11-14 13:03:04 -06:00
|
|
|
function listByHost (fromHost, callback) {
|
|
|
|
this.find({ podHost: fromHost }, callback)
|
2016-06-24 10:42:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function listOwned (callback) {
|
2016-11-11 06:47:50 -06:00
|
|
|
// If remoteId is null this is *our* video
|
|
|
|
this.find({ remoteId: null }, callback)
|
2016-06-24 10:42:51 -05:00
|
|
|
}
|
|
|
|
|
2016-08-04 15:32:36 -05:00
|
|
|
function listOwnedByAuthor (author, callback) {
|
2016-11-11 06:47:50 -06:00
|
|
|
this.find({ remoteId: null, author: author }, callback)
|
2016-08-04 15:32:36 -05:00
|
|
|
}
|
|
|
|
|
2016-06-24 10:42:51 -05:00
|
|
|
function listRemotes (callback) {
|
2016-11-11 06:47:50 -06:00
|
|
|
this.find({ remoteId: { $ne: null } }, callback)
|
2016-06-24 10:42:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function load (id, callback) {
|
|
|
|
this.findById(id, callback)
|
|
|
|
}
|
|
|
|
|
|
|
|
function search (value, field, start, count, sort, callback) {
|
|
|
|
const query = {}
|
|
|
|
// Make an exact search with the magnet
|
2016-11-11 08:24:36 -06:00
|
|
|
if (field === 'magnetUri') {
|
|
|
|
const infoHash = magnetUtil.decode(value).infoHash
|
|
|
|
query.magnet = {
|
|
|
|
infoHash
|
|
|
|
}
|
|
|
|
} else if (field === 'tags') {
|
2016-06-24 10:42:51 -05:00
|
|
|
query[field] = value
|
|
|
|
} else {
|
2016-10-26 13:37:27 -05:00
|
|
|
query[field] = new RegExp(value, 'i')
|
2016-06-24 10:42:51 -05:00
|
|
|
}
|
|
|
|
|
2016-08-16 15:31:45 -05:00
|
|
|
modelUtils.listForApiWithCount.call(this, query, start, count, sort, callback)
|
2016-06-24 10:42:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
function removeThumbnail (video, callback) {
|
2016-11-11 08:20:03 -06:00
|
|
|
fs.unlink(constants.CONFIG.STORAGE.THUMBNAILS_DIR + video.getThumbnailName(), callback)
|
2016-06-24 10:42:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function removeFile (video, callback) {
|
2016-11-11 08:20:03 -06:00
|
|
|
fs.unlink(constants.CONFIG.STORAGE.VIDEOS_DIR + video.getVideoFilename(), callback)
|
2016-06-24 10:42:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function removeTorrent (video, callback) {
|
2016-11-11 06:47:50 -06:00
|
|
|
fs.unlink(constants.CONFIG.STORAGE.TORRENTS_DIR + video.getTorrentName(), callback)
|
2016-06-24 10:42:51 -05:00
|
|
|
}
|
|
|
|
|
2016-11-11 04:52:24 -06:00
|
|
|
function removePreview (video, callback) {
|
|
|
|
// Same name than video thumnail
|
2016-11-11 08:20:03 -06:00
|
|
|
fs.unlink(constants.CONFIG.STORAGE.PREVIEWS_DIR + video.getPreviewName(), callback)
|
2016-11-11 04:52:24 -06:00
|
|
|
}
|
|
|
|
|
2016-11-11 06:47:50 -06:00
|
|
|
function createPreview (video, videoPath, callback) {
|
2016-11-11 08:20:03 -06:00
|
|
|
generateImage(video, videoPath, constants.CONFIG.STORAGE.PREVIEWS_DIR, video.getPreviewName(), callback)
|
2016-11-11 04:52:24 -06:00
|
|
|
}
|
|
|
|
|
2016-11-11 06:47:50 -06:00
|
|
|
function createThumbnail (video, videoPath, callback) {
|
2016-11-11 08:20:03 -06:00
|
|
|
generateImage(video, videoPath, constants.CONFIG.STORAGE.THUMBNAILS_DIR, video.getThumbnailName(), constants.THUMBNAILS_SIZE, callback)
|
2016-06-24 10:42:51 -05:00
|
|
|
}
|
|
|
|
|
2016-11-11 08:20:03 -06:00
|
|
|
function generateImage (video, videoPath, folder, imageName, size, callback) {
|
2016-11-11 06:47:50 -06:00
|
|
|
const options = {
|
2016-11-11 08:20:03 -06:00
|
|
|
filename: imageName,
|
2016-11-11 06:47:50 -06:00
|
|
|
count: 1,
|
|
|
|
folder
|
|
|
|
}
|
2016-06-24 10:42:51 -05:00
|
|
|
|
2016-11-11 06:47:50 -06:00
|
|
|
if (!callback) {
|
|
|
|
callback = size
|
|
|
|
} else {
|
|
|
|
options.size = size
|
|
|
|
}
|
|
|
|
|
|
|
|
ffmpeg(videoPath)
|
|
|
|
.on('error', callback)
|
|
|
|
.on('end', function () {
|
2016-11-11 08:20:03 -06:00
|
|
|
callback(null, imageName)
|
2016-06-24 10:42:51 -05:00
|
|
|
})
|
2016-11-11 06:47:50 -06:00
|
|
|
.thumbnail(options)
|
2016-06-24 10:42:51 -05:00
|
|
|
}
|