3 const createTorrent = require('create-torrent')
4 const ffmpeg = require('fluent-ffmpeg')
5 const fs = require('fs')
6 const magnetUtil = require('magnet-uri')
7 const parallel = require('async/parallel')
8 const parseTorrent = require('parse-torrent')
9 const pathUtils = require('path')
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')
17 // ---------------------------------------------------------------------------
19 // TODO: add indexes on searchable columns
20 const VideoSchema = mongoose.Schema({
24 enum: [ '.mp4', '.webm', '.ogv' ]
26 remoteId: mongoose.Schema.Types.ObjectId,
42 VideoSchema.path('name').validate(customVideosValidators.isVideoNameValid)
43 VideoSchema.path('description').validate(customVideosValidators.isVideoDescriptionValid)
44 VideoSchema.path('podUrl').validate(customVideosValidators.isVideoPodUrlValid)
45 VideoSchema.path('author').validate(customVideosValidators.isVideoAuthorValid)
46 VideoSchema.path('duration').validate(customVideosValidators.isVideoDurationValid)
47 // The tumbnail can be the path or the data in base 64
48 // The pre save hook will convert the base 64 data in a file on disk and replace the thumbnail key by the filename
49 VideoSchema.path('thumbnail').validate(function (value) {
50 return customVideosValidators.isVideoThumbnailValid(value) || customVideosValidators.isVideoThumbnail64Valid(value)
52 VideoSchema.path('tags').validate(customVideosValidators.isVideoTagsValid)
54 VideoSchema.methods = {
65 VideoSchema.statics = {
77 VideoSchema.pre('remove', function (next) {
83 removeThumbnail(video, callback)
87 if (video.isOwned()) {
90 removeFile(video, callback)
93 removeTorrent(video, callback)
96 removePreview(video, callback)
101 parallel(tasks, next)
104 VideoSchema.pre('save', function (next) {
108 if (video.isOwned()) {
109 const videoPath = pathUtils.join(constants.CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename())
110 this.podUrl = constants.CONFIG.WEBSERVER.HOSTNAME + ':' + constants.CONFIG.WEBSERVER.PORT
113 // TODO: refractoring
114 function (callback) {
117 [ constants.CONFIG.WEBSERVER.WS + '://' + constants.CONFIG.WEBSERVER.HOSTNAME + ':' + constants.CONFIG.WEBSERVER.PORT + '/tracker/socket' ]
120 constants.CONFIG.WEBSERVER.URL + constants.STATIC_PATHS.WEBSEED + video.getVideoFilename()
124 createTorrent(videoPath, options, function (err, torrent) {
125 if (err) return callback(err)
127 fs.writeFile(constants.CONFIG.STORAGE.TORRENTS_DIR + video.getTorrentName(), torrent, function (err) {
128 if (err) return callback(err)
130 const parsedTorrent = parseTorrent(torrent)
131 video.magnet.infoHash = parsedTorrent.infoHash
133 console.log(parsedTorrent)
138 function (callback) {
139 createThumbnail(video, videoPath, callback)
141 function (callback) {
142 createPreview(video, videoPath, callback)
146 parallel(tasks, next)
148 generateThumbnailFromBase64(video, video.thumbnail, next)
152 mongoose.model('Video', VideoSchema)
154 // ------------------------------ METHODS ------------------------------
156 function generateMagnetUri () {
157 let baseUrlHttp, baseUrlWs
159 if (this.isOwned()) {
160 baseUrlHttp = constants.CONFIG.WEBSERVER.URL
161 baseUrlWs = constants.CONFIG.WEBSERVER.WS + '://' + constants.CONFIG.WEBSERVER.HOSTNAME + ':' + constants.CONFIG.WEBSERVER.PORT
163 baseUrlHttp = constants.REMOTE_SCHEME.HTTP + this.podUrl
164 baseUrlWs = constants.REMOTE_SCHEME.WS + this.podUrl
167 const xs = baseUrlHttp + constants.STATIC_PATHS.TORRENTS + this.getTorrentName()
168 const announce = baseUrlWs + '/tracker/socket'
169 const urlList = [ baseUrlHttp + constants.STATIC_PATHS.WEBSEED + this.getVideoFilename() ]
175 infoHash: this.magnet.infoHash,
179 return magnetUtil.encode(magnetHash)
182 function getVideoFilename () {
183 if (this.isOwned()) return this._id + this.extname
185 return this.remoteId + this.extname
188 function getThumbnailName () {
189 // We always have a copy of the thumbnail
190 return this._id + '.jpg'
193 function getPreviewName () {
194 const extension = '.jpg'
196 if (this.isOwned()) return this._id + extension
198 return this.remoteId + extension
201 function getTorrentName () {
202 const extension = '.torrent'
204 if (this.isOwned()) return this._id + extension
206 return this.remoteId + extension
209 function isOwned () {
210 return this.remoteId === null
213 function toFormatedJSON () {
217 description: this.description,
219 isLocal: this.isOwned(),
220 magnetUri: this.generateMagnetUri(),
222 duration: this.duration,
224 thumbnailPath: constants.STATIC_PATHS.THUMBNAILS + '/' + this.getThumbnailName(),
225 createdDate: this.createdDate
231 function toRemoteJSON (callback) {
234 // Convert thumbnail to base64
235 const thumbnailPath = pathUtils.join(constants.CONFIG.STORAGE.THUMBNAILS_DIR, this.getThumbnailName())
236 fs.readFile(thumbnailPath, function (err, thumbnailData) {
238 logger.error('Cannot read the thumbnail of the video')
242 const remoteVideo = {
244 description: self.description,
248 duration: self.duration,
249 thumbnailBase64: new Buffer(thumbnailData).toString('base64'),
251 createdDate: self.createdDate,
255 return callback(null, remoteVideo)
259 // ------------------------------ STATICS ------------------------------
261 function getDurationFromFile (videoPath, callback) {
262 ffmpeg.ffprobe(videoPath, function (err, metadata) {
263 if (err) return callback(err)
265 return callback(null, Math.floor(metadata.format.duration))
269 function listForApi (start, count, sort, callback) {
271 return modelUtils.listForApiWithCount.call(this, query, start, count, sort, callback)
274 function listByUrlAndRemoteId (fromUrl, remoteId, callback) {
275 this.find({ podUrl: fromUrl, remoteId: remoteId }, callback)
278 function listByUrl (fromUrl, callback) {
279 this.find({ podUrl: fromUrl }, callback)
282 function listOwned (callback) {
283 // If remoteId is null this is *our* video
284 this.find({ remoteId: null }, callback)
287 function listOwnedByAuthor (author, callback) {
288 this.find({ remoteId: null, author: author }, callback)
291 function listRemotes (callback) {
292 this.find({ remoteId: { $ne: null } }, callback)
295 function load (id, callback) {
296 this.findById(id, callback)
299 function search (value, field, start, count, sort, callback) {
301 // Make an exact search with the magnet
302 if (field === 'magnetUri' || field === 'tags') {
305 query[field] = new RegExp(value, 'i')
308 modelUtils.listForApiWithCount.call(this, query, start, count, sort, callback)
311 // ---------------------------------------------------------------------------
313 function removeThumbnail (video, callback) {
314 fs.unlink(constants.CONFIG.STORAGE.THUMBNAILS_DIR + video.getThumbnailName(), callback)
317 function removeFile (video, callback) {
318 fs.unlink(constants.CONFIG.STORAGE.VIDEOS_DIR + video.getVideoFilename(), callback)
321 function removeTorrent (video, callback) {
322 fs.unlink(constants.CONFIG.STORAGE.TORRENTS_DIR + video.getTorrentName(), callback)
325 function removePreview (video, callback) {
326 // Same name than video thumnail
327 fs.unlink(constants.CONFIG.STORAGE.PREVIEWS_DIR + video.getPreviewName(), callback)
330 function createPreview (video, videoPath, callback) {
331 generateImage(video, videoPath, constants.CONFIG.STORAGE.PREVIEWS_DIR, video.getPreviewName(), callback)
334 function createThumbnail (video, videoPath, callback) {
335 generateImage(video, videoPath, constants.CONFIG.STORAGE.THUMBNAILS_DIR, video.getThumbnailName(), constants.THUMBNAILS_SIZE, callback)
338 function generateThumbnailFromBase64 (video, thumbnailData, callback) {
339 // Creating the thumbnail for this remote video)
341 const thumbnailName = video.getThumbnailName()
342 const thumbnailPath = constants.CONFIG.STORAGE.THUMBNAILS_DIR + thumbnailName
343 fs.writeFile(thumbnailPath, thumbnailData, { encoding: 'base64' }, function (err) {
344 if (err) return callback(err)
346 return callback(null, thumbnailName)
350 function generateImage (video, videoPath, folder, imageName, size, callback) {
364 .on('error', callback)
365 .on('end', function () {
366 callback(null, imageName)