]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/video.js
Server: video.list -> video.listForApi (with pagination, sort...)
[github/Chocobozzz/PeerTube.git] / server / models / video.js
index b111e29aa074201b25f0fa22594e4ac8fdeb45b1..a5540d127a157630b92f834d92dd4e78bcd7aac1 100644 (file)
@@ -1,15 +1,17 @@
 'use strict'
 
-const async = require('async')
 const config = require('config')
+const eachLimit = require('async/eachLimit')
 const ffmpeg = require('fluent-ffmpeg')
 const fs = require('fs')
+const parallel = require('async/parallel')
 const pathUtils = require('path')
 const mongoose = require('mongoose')
 
 const constants = require('../initializers/constants')
-const customValidators = require('../helpers/customValidators')
+const customVideosValidators = require('../helpers/custom-validators').videos
 const logger = require('../helpers/logger')
+const modelUtils = require('./utils')
 const utils = require('../helpers/utils')
 const webtorrent = require('../lib/webtorrent')
 
@@ -24,7 +26,7 @@ const thumbnailsDir = pathUtils.join(__dirname, '..', '..', config.get('storage.
 // TODO: add indexes on searchable columns
 const VideoSchema = mongoose.Schema({
   name: String,
-  namePath: String,
+  filename: String,
   description: String,
   magnetUri: String,
   podUrl: String,
@@ -38,18 +40,18 @@ const VideoSchema = mongoose.Schema({
   }
 })
 
-VideoSchema.path('name').validate(customValidators.isVideoNameValid)
-VideoSchema.path('description').validate(customValidators.isVideoDescriptionValid)
-VideoSchema.path('magnetUri').validate(customValidators.isVideoMagnetUriValid)
-VideoSchema.path('podUrl').validate(customValidators.isVideoPodUrlValid)
-VideoSchema.path('author').validate(customValidators.isVideoAuthorValid)
-VideoSchema.path('duration').validate(customValidators.isVideoDurationValid)
+VideoSchema.path('name').validate(customVideosValidators.isVideoNameValid)
+VideoSchema.path('description').validate(customVideosValidators.isVideoDescriptionValid)
+VideoSchema.path('magnetUri').validate(customVideosValidators.isVideoMagnetUriValid)
+VideoSchema.path('podUrl').validate(customVideosValidators.isVideoPodUrlValid)
+VideoSchema.path('author').validate(customVideosValidators.isVideoAuthorValid)
+VideoSchema.path('duration').validate(customVideosValidators.isVideoDurationValid)
 // The tumbnail can be the path or the data in base 64
 // The pre save hook will convert the base 64 data in a file on disk and replace the thumbnail key by the filename
 VideoSchema.path('thumbnail').validate(function (value) {
-  return customValidators.isVideoThumbnailValid(value) || customValidators.isVideoThumbnail64Valid(value)
+  return customVideosValidators.isVideoThumbnailValid(value) || customVideosValidators.isVideoThumbnail64Valid(value)
 })
-VideoSchema.path('tags').validate(customValidators.isVideoTagsValid)
+VideoSchema.path('tags').validate(customVideosValidators.isVideoTagsValid)
 
 VideoSchema.methods = {
   isOwned: isOwned,
@@ -59,10 +61,11 @@ VideoSchema.methods = {
 
 VideoSchema.statics = {
   getDurationFromFile: getDurationFromFile,
-  list: list,
+  listForApi: listForApi,
   listByUrlAndMagnet: listByUrlAndMagnet,
   listByUrls: listByUrls,
   listOwned: listOwned,
+  listOwnedByAuthor: listOwnedByAuthor,
   listRemotes: listRemotes,
   load: load,
   search: search,
@@ -90,7 +93,7 @@ VideoSchema.pre('remove', function (next) {
     )
   }
 
-  async.parallel(tasks, next)
+  parallel(tasks, next)
 })
 
 VideoSchema.pre('save', function (next) {
@@ -98,7 +101,7 @@ VideoSchema.pre('save', function (next) {
   const tasks = []
 
   if (video.isOwned()) {
-    const videoPath = pathUtils.join(uploadsDir, video.namePath)
+    const videoPath = pathUtils.join(uploadsDir, video.filename)
     this.podUrl = http + '://' + host + ':' + port
 
     tasks.push(
@@ -110,7 +113,7 @@ VideoSchema.pre('save', function (next) {
       }
     )
 
-    async.parallel(tasks, function (err, results) {
+    parallel(tasks, function (err, results) {
       if (err) return next(err)
 
       video.magnetUri = results[0].magnetURI
@@ -134,7 +137,7 @@ mongoose.model('Video', VideoSchema)
 // ------------------------------ METHODS ------------------------------
 
 function isOwned () {
-  return this.namePath !== null
+  return this.filename !== null
 }
 
 function toFormatedJSON () {
@@ -169,7 +172,7 @@ function toRemoteJSON (callback) {
       name: self.name,
       description: self.description,
       magnetUri: self.magnetUri,
-      namePath: null,
+      filename: null,
       author: self.author,
       duration: self.duration,
       thumbnailBase64: new Buffer(thumbnailData).toString('base64'),
@@ -192,9 +195,9 @@ function getDurationFromFile (videoPath, callback) {
   })
 }
 
-function list (start, count, sort, callback) {
+function listForApi (start, count, sort, callback) {
   const query = {}
-  return findWithCount.call(this, query, start, count, sort, callback)
+  return modelUtils.findWithCount.call(this, query, start, count, sort, callback)
 }
 
 function listByUrlAndMagnet (fromUrl, magnetUri, callback) {
@@ -206,12 +209,16 @@ function listByUrls (fromUrls, callback) {
 }
 
 function listOwned (callback) {
-  // If namePath is not null this is *our* video
-  this.find({ namePath: { $ne: null } }, callback)
+  // If filename is not null this is *our* video
+  this.find({ filename: { $ne: null } }, callback)
+}
+
+function listOwnedByAuthor (author, callback) {
+  this.find({ filename: { $ne: null }, author: author }, callback)
 }
 
 function listRemotes (callback) {
-  this.find({ namePath: null }, callback)
+  this.find({ filename: null }, callback)
 }
 
 function load (id, callback) {
@@ -227,15 +234,15 @@ function search (value, field, start, count, sort, callback) {
     query[field] = new RegExp(value)
   }
 
-  findWithCount.call(this, query, start, count, sort, callback)
+  modelUtils.findWithCount.call(this, query, start, count, sort, callback)
 }
 
 function seedAllExisting (callback) {
   listOwned.call(this, function (err, videos) {
     if (err) return callback(err)
 
-    async.each(videos, function (video, callbackEach) {
-      const videoPath = pathUtils.join(uploadsDir, video.namePath)
+    eachLimit(videos, constants.SEEDS_IN_PARALLEL, function (video, callbackEach) {
+      const videoPath = pathUtils.join(uploadsDir, video.filename)
       seed(videoPath, callbackEach)
     }, callback)
   })
@@ -243,31 +250,12 @@ function seedAllExisting (callback) {
 
 // ---------------------------------------------------------------------------
 
-function findWithCount (query, start, count, sort, callback) {
-  const self = this
-
-  async.parallel([
-    function (asyncCallback) {
-      self.find(query).skip(start).limit(start + count).sort(sort).exec(asyncCallback)
-    },
-    function (asyncCallback) {
-      self.count(query, asyncCallback)
-    }
-  ], function (err, results) {
-    if (err) return callback(err)
-
-    const videos = results[0]
-    const totalVideos = results[1]
-    return callback(null, videos, totalVideos)
-  })
-}
-
 function removeThumbnail (video, callback) {
   fs.unlink(thumbnailsDir + video.thumbnail, callback)
 }
 
 function removeFile (video, callback) {
-  fs.unlink(uploadsDir + video.namePath, callback)
+  fs.unlink(uploadsDir + video.filename, callback)
 }
 
 // Maybe the torrent is not seeded, but we catch the error to don't stop the removing process