aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/video.js
diff options
context:
space:
mode:
Diffstat (limited to 'server/models/video.js')
-rw-r--r--server/models/video.js85
1 files changed, 36 insertions, 49 deletions
diff --git a/server/models/video.js b/server/models/video.js
index 14e0df6f2..7d073cffa 100644
--- a/server/models/video.js
+++ b/server/models/video.js
@@ -11,8 +11,9 @@ const magnet = require('magnet-uri')
11const mongoose = require('mongoose') 11const mongoose = require('mongoose')
12 12
13const constants = require('../initializers/constants') 13const constants = require('../initializers/constants')
14const customValidators = require('../helpers/custom-validators') 14const customVideosValidators = require('../helpers/custom-validators').videos
15const logger = require('../helpers/logger') 15const logger = require('../helpers/logger')
16const modelUtils = require('./utils')
16const utils = require('../helpers/utils') 17const utils = require('../helpers/utils')
17 18
18const http = config.get('webserver.https') === true ? 'https' : 'http' 19const http = config.get('webserver.https') === true ? 'https' : 'http'
@@ -42,34 +43,35 @@ const VideoSchema = mongoose.Schema({
42 } 43 }
43}) 44})
44 45
45VideoSchema.path('name').validate(customValidators.isVideoNameValid) 46VideoSchema.path('name').validate(customVideosValidators.isVideoNameValid)
46VideoSchema.path('description').validate(customValidators.isVideoDescriptionValid) 47VideoSchema.path('description').validate(customVideosValidators.isVideoDescriptionValid)
47VideoSchema.path('magnetUri').validate(customValidators.isVideoMagnetUriValid) 48VideoSchema.path('magnetUri').validate(customVideosValidators.isVideoMagnetUriValid)
48VideoSchema.path('podUrl').validate(customValidators.isVideoPodUrlValid) 49VideoSchema.path('podUrl').validate(customVideosValidators.isVideoPodUrlValid)
49VideoSchema.path('author').validate(customValidators.isVideoAuthorValid) 50VideoSchema.path('author').validate(customVideosValidators.isVideoAuthorValid)
50VideoSchema.path('duration').validate(customValidators.isVideoDurationValid) 51VideoSchema.path('duration').validate(customVideosValidators.isVideoDurationValid)
51// The tumbnail can be the path or the data in base 64 52// The tumbnail can be the path or the data in base 64
52// The pre save hook will convert the base 64 data in a file on disk and replace the thumbnail key by the filename 53// The pre save hook will convert the base 64 data in a file on disk and replace the thumbnail key by the filename
53VideoSchema.path('thumbnail').validate(function (value) { 54VideoSchema.path('thumbnail').validate(function (value) {
54 return customValidators.isVideoThumbnailValid(value) || customValidators.isVideoThumbnail64Valid(value) 55 return customVideosValidators.isVideoThumbnailValid(value) || customVideosValidators.isVideoThumbnail64Valid(value)
55}) 56})
56VideoSchema.path('tags').validate(customValidators.isVideoTagsValid) 57VideoSchema.path('tags').validate(customVideosValidators.isVideoTagsValid)
57 58
58VideoSchema.methods = { 59VideoSchema.methods = {
59 isOwned: isOwned, 60 isOwned,
60 toFormatedJSON: toFormatedJSON, 61 toFormatedJSON,
61 toRemoteJSON: toRemoteJSON 62 toRemoteJSON
62} 63}
63 64
64VideoSchema.statics = { 65VideoSchema.statics = {
65 getDurationFromFile: getDurationFromFile, 66 getDurationFromFile,
66 list: list, 67 listForApi,
67 listByUrlAndMagnet: listByUrlAndMagnet, 68 listByUrlAndMagnet,
68 listByUrls: listByUrls, 69 listByUrls,
69 listOwned: listOwned, 70 listOwned,
70 listRemotes: listRemotes, 71 listOwnedByAuthor,
71 load: load, 72 listRemotes,
72 search: search 73 load,
74 search
73} 75}
74 76
75VideoSchema.pre('remove', function (next) { 77VideoSchema.pre('remove', function (next) {
@@ -101,8 +103,8 @@ VideoSchema.pre('save', function (next) {
101 const tasks = [] 103 const tasks = []
102 104
103 if (video.isOwned()) { 105 if (video.isOwned()) {
104 const videoPath = pathUtils.join(uploadsDir, video.filename) 106 const videoPath = pathUtils.join(constants.CONFIG.STORAGE.UPLOAD_DIR, video.filename)
105 this.podUrl = http + '://' + host + ':' + port 107 this.podUrl = constants.CONFIG.WEBSERVER.URL
106 108
107 tasks.push( 109 tasks.push(
108 // TODO: refractoring 110 // TODO: refractoring
@@ -174,7 +176,7 @@ function toRemoteJSON (callback) {
174 const self = this 176 const self = this
175 177
176 // Convert thumbnail to base64 178 // Convert thumbnail to base64
177 fs.readFile(pathUtils.join(thumbnailsDir, this.thumbnail), function (err, thumbnailData) { 179 fs.readFile(pathUtils.join(constants.CONFIG.STORAGE.THUMBNAILS_DIR, this.thumbnail), function (err, thumbnailData) {
178 if (err) { 180 if (err) {
179 logger.error('Cannot read the thumbnail of the video') 181 logger.error('Cannot read the thumbnail of the video')
180 return callback(err) 182 return callback(err)
@@ -207,9 +209,9 @@ function getDurationFromFile (videoPath, callback) {
207 }) 209 })
208} 210}
209 211
210function list (start, count, sort, callback) { 212function listForApi (start, count, sort, callback) {
211 const query = {} 213 const query = {}
212 return findWithCount.call(this, query, start, count, sort, callback) 214 return modelUtils.listForApiWithCount.call(this, query, start, count, sort, callback)
213} 215}
214 216
215function listByUrlAndMagnet (fromUrl, magnetUri, callback) { 217function listByUrlAndMagnet (fromUrl, magnetUri, callback) {
@@ -225,6 +227,10 @@ function listOwned (callback) {
225 this.find({ filename: { $ne: null } }, callback) 227 this.find({ filename: { $ne: null } }, callback)
226} 228}
227 229
230function listOwnedByAuthor (author, callback) {
231 this.find({ filename: { $ne: null }, author: author }, callback)
232}
233
228function listRemotes (callback) { 234function listRemotes (callback) {
229 this.find({ filename: null }, callback) 235 this.find({ filename: null }, callback)
230} 236}
@@ -242,36 +248,17 @@ function search (value, field, start, count, sort, callback) {
242 query[field] = new RegExp(value) 248 query[field] = new RegExp(value)
243 } 249 }
244 250
245 findWithCount.call(this, query, start, count, sort, callback) 251 modelUtils.listForApiWithCount.call(this, query, start, count, sort, callback)
246} 252}
247 253
248// --------------------------------------------------------------------------- 254// ---------------------------------------------------------------------------
249 255
250function findWithCount (query, start, count, sort, callback) {
251 const self = this
252
253 parallel([
254 function (asyncCallback) {
255 self.find(query).skip(start).limit(count).sort(sort).exec(asyncCallback)
256 },
257 function (asyncCallback) {
258 self.count(query, asyncCallback)
259 }
260 ], function (err, results) {
261 if (err) return callback(err)
262
263 const videos = results[0]
264 const totalVideos = results[1]
265 return callback(null, videos, totalVideos)
266 })
267}
268
269function removeThumbnail (video, callback) { 256function removeThumbnail (video, callback) {
270 fs.unlink(thumbnailsDir + video.thumbnail, callback) 257 fs.unlink(constants.CONFIG.STORAGE.THUMBNAILS_DIR + video.thumbnail, callback)
271} 258}
272 259
273function removeFile (video, callback) { 260function removeFile (video, callback) {
274 fs.unlink(uploadsDir + video.filename, callback) 261 fs.unlink(constants.CONFIG.STORAGE.UPLOAD_DIR + video.filename, callback)
275} 262}
276 263
277// Maybe the torrent is not seeded, but we catch the error to don't stop the removing process 264// Maybe the torrent is not seeded, but we catch the error to don't stop the removing process
@@ -288,7 +275,7 @@ function createThumbnail (videoPath, callback) {
288 }) 275 })
289 .thumbnail({ 276 .thumbnail({
290 count: 1, 277 count: 1,
291 folder: thumbnailsDir, 278 folder: constants.CONFIG.STORAGE.THUMBNAILS_DIR,
292 size: constants.THUMBNAILS_SIZE, 279 size: constants.THUMBNAILS_SIZE,
293 filename: filename 280 filename: filename
294 }) 281 })
@@ -300,7 +287,7 @@ function generateThumbnailFromBase64 (data, callback) {
300 if (err) return callback(err) 287 if (err) return callback(err)
301 288
302 const thumbnailName = randomString + '.jpg' 289 const thumbnailName = randomString + '.jpg'
303 fs.writeFile(thumbnailsDir + thumbnailName, data, { encoding: 'base64' }, function (err) { 290 fs.writeFile(constants.CONFIG.STORAGE.THUMBNAILS_DIR + thumbnailName, data, { encoding: 'base64' }, function (err) {
304 if (err) return callback(err) 291 if (err) return callback(err)
305 292
306 return callback(null, thumbnailName) 293 return callback(null, thumbnailName)