3 const config = require('config')
4 const eachLimit = require('async/eachLimit')
5 const ffmpeg = require('fluent-ffmpeg')
6 const fs = require('fs')
7 const parallel = require('async/parallel')
8 const pathUtils = require('path')
9 const mongoose = require('mongoose')
11 const constants = require('../initializers/constants')
12 const customVideosValidators = require('../helpers/custom-validators').videos
13 const logger = require('../helpers/logger')
14 const utils = require('../helpers/utils')
15 const webtorrent = require('../lib/webtorrent')
17 const http = config.get('webserver.https') === true ? 'https' : 'http'
18 const host = config.get('webserver.host')
19 const port = config.get('webserver.port')
20 const uploadsDir = pathUtils.join(__dirname, '..', '..', config.get('storage.uploads'))
21 const thumbnailsDir = pathUtils.join(__dirname, '..', '..', config.get('storage.thumbnails'))
23 // ---------------------------------------------------------------------------
25 // TODO: add indexes on searchable columns
26 const VideoSchema = mongoose.Schema({
42 VideoSchema.path('name').validate(customVideosValidators.isVideoNameValid)
43 VideoSchema.path('description').validate(customVideosValidators.isVideoDescriptionValid)
44 VideoSchema.path('magnetUri').validate(customVideosValidators.isVideoMagnetUriValid)
45 VideoSchema.path('podUrl').validate(customVideosValidators.isVideoPodUrlValid)
46 VideoSchema.path('author').validate(customVideosValidators.isVideoAuthorValid)
47 VideoSchema.path('duration').validate(customVideosValidators.isVideoDurationValid)
48 // The tumbnail can be the path or the data in base 64
49 // The pre save hook will convert the base 64 data in a file on disk and replace the thumbnail key by the filename
50 VideoSchema.path('thumbnail').validate(function (value) {
51 return customVideosValidators.isVideoThumbnailValid(value) || customVideosValidators.isVideoThumbnail64Valid(value)
53 VideoSchema.path('tags').validate(customVideosValidators.isVideoTagsValid)
55 VideoSchema.methods = {
57 toFormatedJSON: toFormatedJSON,
58 toRemoteJSON: toRemoteJSON
61 VideoSchema.statics = {
62 getDurationFromFile: getDurationFromFile,
64 listByUrlAndMagnet: listByUrlAndMagnet,
65 listByUrls: listByUrls,
67 listOwnedByAuthor: listOwnedByAuthor,
68 listRemotes: listRemotes,
71 seedAllExisting: seedAllExisting
74 VideoSchema.pre('remove', function (next) {
80 removeThumbnail(video, callback)
84 if (video.isOwned()) {
87 removeFile(video, callback)
90 removeTorrent(video, callback)
98 VideoSchema.pre('save', function (next) {
102 if (video.isOwned()) {
103 const videoPath = pathUtils.join(uploadsDir, video.filename)
104 this.podUrl = http + '://' + host + ':' + port
107 function (callback) {
108 seed(videoPath, callback)
110 function (callback) {
111 createThumbnail(videoPath, callback)
115 parallel(tasks, function (err, results) {
116 if (err) return next(err)
118 video.magnetUri = results[0].magnetURI
119 video.thumbnail = results[1]
124 generateThumbnailFromBase64(video.thumbnail, function (err, thumbnailName) {
125 if (err) return next(err)
127 video.thumbnail = thumbnailName
134 mongoose.model('Video', VideoSchema)
136 // ------------------------------ METHODS ------------------------------
138 function isOwned () {
139 return this.filename !== null
142 function toFormatedJSON () {
146 description: this.description,
147 podUrl: this.podUrl.replace(/^https?:\/\//, ''),
148 isLocal: this.isOwned(),
149 magnetUri: this.magnetUri,
151 duration: this.duration,
153 thumbnailPath: constants.THUMBNAILS_STATIC_PATH + '/' + this.thumbnail,
154 createdDate: this.createdDate
160 function toRemoteJSON (callback) {
163 // Convert thumbnail to base64
164 fs.readFile(pathUtils.join(thumbnailsDir, this.thumbnail), function (err, thumbnailData) {
166 logger.error('Cannot read the thumbnail of the video')
170 const remoteVideo = {
172 description: self.description,
173 magnetUri: self.magnetUri,
176 duration: self.duration,
177 thumbnailBase64: new Buffer(thumbnailData).toString('base64'),
179 createdDate: self.createdDate,
183 return callback(null, remoteVideo)
187 // ------------------------------ STATICS ------------------------------
189 function getDurationFromFile (videoPath, callback) {
190 ffmpeg.ffprobe(videoPath, function (err, metadata) {
191 if (err) return callback(err)
193 return callback(null, Math.floor(metadata.format.duration))
197 function list (start, count, sort, callback) {
199 return findWithCount.call(this, query, start, count, sort, callback)
202 function listByUrlAndMagnet (fromUrl, magnetUri, callback) {
203 this.find({ podUrl: fromUrl, magnetUri: magnetUri }, callback)
206 function listByUrls (fromUrls, callback) {
207 this.find({ podUrl: { $in: fromUrls } }, callback)
210 function listOwned (callback) {
211 // If filename is not null this is *our* video
212 this.find({ filename: { $ne: null } }, callback)
215 function listOwnedByAuthor (author, callback) {
216 this.find({ filename: { $ne: null }, author: author }, callback)
219 function listRemotes (callback) {
220 this.find({ filename: null }, callback)
223 function load (id, callback) {
224 this.findById(id, callback)
227 function search (value, field, start, count, sort, callback) {
229 // Make an exact search with the magnet
230 if (field === 'magnetUri' || field === 'tags') {
233 query[field] = new RegExp(value)
236 findWithCount.call(this, query, start, count, sort, callback)
239 function seedAllExisting (callback) {
240 listOwned.call(this, function (err, videos) {
241 if (err) return callback(err)
243 eachLimit(videos, constants.SEEDS_IN_PARALLEL, function (video, callbackEach) {
244 const videoPath = pathUtils.join(uploadsDir, video.filename)
245 seed(videoPath, callbackEach)
250 // ---------------------------------------------------------------------------
252 function findWithCount (query, start, count, sort, callback) {
256 function (asyncCallback) {
257 self.find(query).skip(start).limit(count).sort(sort).exec(asyncCallback)
259 function (asyncCallback) {
260 self.count(query, asyncCallback)
262 ], function (err, results) {
263 if (err) return callback(err)
265 const videos = results[0]
266 const totalVideos = results[1]
267 return callback(null, videos, totalVideos)
271 function removeThumbnail (video, callback) {
272 fs.unlink(thumbnailsDir + video.thumbnail, callback)
275 function removeFile (video, callback) {
276 fs.unlink(uploadsDir + video.filename, callback)
279 // Maybe the torrent is not seeded, but we catch the error to don't stop the removing process
280 function removeTorrent (video, callback) {
282 webtorrent.remove(video.magnetUri, callback)
284 logger.warn('Cannot remove the torrent from WebTorrent', { err: err })
285 return callback(null)
289 function createThumbnail (videoPath, callback) {
290 const filename = pathUtils.basename(videoPath) + '.jpg'
292 .on('error', callback)
293 .on('end', function () {
294 callback(null, filename)
298 folder: thumbnailsDir,
299 size: constants.THUMBNAILS_SIZE,
304 function seed (path, callback) {
305 logger.info('Seeding %s...', path)
307 webtorrent.seed(path, function (torrent) {
308 logger.info('%s seeded (%s).', path, torrent.magnetURI)
310 return callback(null, torrent)
314 function generateThumbnailFromBase64 (data, callback) {
315 // Creating the thumbnail for this remote video
316 utils.generateRandomString(16, function (err, randomString) {
317 if (err) return callback(err)
319 const thumbnailName = randomString + '.jpg'
320 fs.writeFile(thumbnailsDir + thumbnailName, data, { encoding: 'base64' }, function (err) {
321 if (err) return callback(err)
323 return callback(null, thumbnailName)