3 const createTorrent = require('create-torrent')
4 const ffmpeg = require('fluent-ffmpeg')
5 const fs = require('fs')
6 const parallel = require('async/parallel')
7 const parseTorrent = require('parse-torrent')
8 const pathUtils = require('path')
9 const magnet = require('magnet-uri')
10 const mongoose = require('mongoose')
12 const constants = require('../initializers/constants')
13 const customVideosValidators = require('../helpers/custom-validators').videos
14 const logger = require('../helpers/logger')
15 const modelUtils = require('./utils')
16 const utils = require('../helpers/utils')
18 // ---------------------------------------------------------------------------
20 // TODO: add indexes on searchable columns
21 const VideoSchema = mongoose.Schema({
37 VideoSchema.path('name').validate(customVideosValidators.isVideoNameValid)
38 VideoSchema.path('description').validate(customVideosValidators.isVideoDescriptionValid)
39 VideoSchema.path('magnetUri').validate(customVideosValidators.isVideoMagnetUriValid)
40 VideoSchema.path('podUrl').validate(customVideosValidators.isVideoPodUrlValid)
41 VideoSchema.path('author').validate(customVideosValidators.isVideoAuthorValid)
42 VideoSchema.path('duration').validate(customVideosValidators.isVideoDurationValid)
43 // The tumbnail can be the path or the data in base 64
44 // The pre save hook will convert the base 64 data in a file on disk and replace the thumbnail key by the filename
45 VideoSchema.path('thumbnail').validate(function (value) {
46 return customVideosValidators.isVideoThumbnailValid(value) || customVideosValidators.isVideoThumbnail64Valid(value)
48 VideoSchema.path('tags').validate(customVideosValidators.isVideoTagsValid)
50 VideoSchema.methods = {
56 VideoSchema.statics = {
68 VideoSchema.pre('remove', function (next) {
74 removeThumbnail(video, callback)
78 if (video.isOwned()) {
81 removeFile(video, callback)
84 removeTorrent(video, callback)
87 removePreview(video, callback)
95 VideoSchema.pre('save', function (next) {
99 if (video.isOwned()) {
100 const videoPath = pathUtils.join(constants.CONFIG.STORAGE.VIDEOS_DIR, video.filename)
101 this.podUrl = constants.CONFIG.WEBSERVER.URL
104 // TODO: refractoring
105 function (callback) {
108 [ constants.CONFIG.WEBSERVER.WS + '://' + constants.CONFIG.WEBSERVER.HOSTNAME + ':' + constants.CONFIG.WEBSERVER.PORT + '/tracker/socket' ]
111 constants.CONFIG.WEBSERVER.URL + constants.STATIC_PATHS.WEBSEED + video.filename
115 createTorrent(videoPath, options, function (err, torrent) {
116 if (err) return callback(err)
118 fs.writeFile(constants.CONFIG.STORAGE.TORRENTS_DIR + video.filename + '.torrent', torrent, function (err) {
119 if (err) return callback(err)
121 const parsedTorrent = parseTorrent(torrent)
122 parsedTorrent.xs = video.podUrl + constants.STATIC_PATHS.TORRENTS + video.filename + '.torrent'
123 video.magnetUri = magnet.encode(parsedTorrent)
129 function (callback) {
130 createThumbnail(videoPath, callback)
132 function (callback) {
133 createPreview(videoPath, callback)
137 parallel(tasks, function (err, results) {
138 if (err) return next(err)
140 video.thumbnail = results[1]
145 generateThumbnailFromBase64(video.thumbnail, function (err, thumbnailName) {
146 if (err) return next(err)
148 video.thumbnail = thumbnailName
155 mongoose.model('Video', VideoSchema)
157 // ------------------------------ METHODS ------------------------------
159 function isOwned () {
160 return this.filename !== null
163 function toFormatedJSON () {
167 description: this.description,
168 podUrl: this.podUrl.replace(/^https?:\/\//, ''),
169 isLocal: this.isOwned(),
170 magnetUri: this.magnetUri,
172 duration: this.duration,
174 thumbnailPath: constants.STATIC_PATHS.THUMBNAILS + '/' + this.thumbnail,
175 createdDate: this.createdDate
181 function toRemoteJSON (callback) {
184 // Convert thumbnail to base64
185 fs.readFile(pathUtils.join(constants.CONFIG.STORAGE.THUMBNAILS_DIR, this.thumbnail), function (err, thumbnailData) {
187 logger.error('Cannot read the thumbnail of the video')
191 const remoteVideo = {
193 description: self.description,
194 magnetUri: self.magnetUri,
197 duration: self.duration,
198 thumbnailBase64: new Buffer(thumbnailData).toString('base64'),
200 createdDate: self.createdDate,
204 return callback(null, remoteVideo)
208 // ------------------------------ STATICS ------------------------------
210 function getDurationFromFile (videoPath, callback) {
211 ffmpeg.ffprobe(videoPath, function (err, metadata) {
212 if (err) return callback(err)
214 return callback(null, Math.floor(metadata.format.duration))
218 function listForApi (start, count, sort, callback) {
220 return modelUtils.listForApiWithCount.call(this, query, start, count, sort, callback)
223 function listByUrlAndMagnet (fromUrl, magnetUri, callback) {
224 this.find({ podUrl: fromUrl, magnetUri: magnetUri }, callback)
227 function listByUrl (fromUrl, callback) {
228 this.find({ podUrl: fromUrl }, callback)
231 function listOwned (callback) {
232 // If filename is not null this is *our* video
233 this.find({ filename: { $ne: null } }, callback)
236 function listOwnedByAuthor (author, callback) {
237 this.find({ filename: { $ne: null }, author: author }, callback)
240 function listRemotes (callback) {
241 this.find({ filename: null }, callback)
244 function load (id, callback) {
245 this.findById(id, callback)
248 function search (value, field, start, count, sort, callback) {
250 // Make an exact search with the magnet
251 if (field === 'magnetUri' || field === 'tags') {
254 query[field] = new RegExp(value, 'i')
257 modelUtils.listForApiWithCount.call(this, query, start, count, sort, callback)
260 // ---------------------------------------------------------------------------
262 function removeThumbnail (video, callback) {
263 fs.unlink(constants.CONFIG.STORAGE.THUMBNAILS_DIR + video.thumbnail, callback)
266 function removeFile (video, callback) {
267 fs.unlink(constants.CONFIG.STORAGE.VIDEOS_DIR + video.filename, callback)
270 function removeTorrent (video, callback) {
271 fs.unlink(constants.CONFIG.STORAGE.TORRENTS_DIR + video.filename + '.torrent', callback)
274 function removePreview (video, callback) {
275 // Same name than video thumnail
276 // TODO: refractoring
277 fs.unlink(constants.CONFIG.STORAGE.PREVIEWS_DIR + video.thumbnail, callback)
280 function createPreview (videoPath, callback) {
281 const filename = pathUtils.basename(videoPath) + '.jpg'
283 .on('error', callback)
284 .on('end', function () {
285 callback(null, filename)
289 folder: constants.CONFIG.STORAGE.PREVIEWS_DIR,
294 function createThumbnail (videoPath, callback) {
295 const filename = pathUtils.basename(videoPath) + '.jpg'
297 .on('error', callback)
298 .on('end', function () {
299 callback(null, filename)
303 folder: constants.CONFIG.STORAGE.THUMBNAILS_DIR,
304 size: constants.THUMBNAILS_SIZE,
309 function generateThumbnailFromBase64 (data, callback) {
310 // Creating the thumbnail for this remote video
311 utils.generateRandomString(16, function (err, randomString) {
312 if (err) return callback(err)
314 const thumbnailName = randomString + '.jpg'
315 fs.writeFile(constants.CONFIG.STORAGE.THUMBNAILS_DIR + thumbnailName, data, { encoding: 'base64' }, function (err) {
316 if (err) return callback(err)
318 return callback(null, thumbnailName)