]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/videos.js
Extends the search feature by customizing the search field (name,
[github/Chocobozzz/PeerTube.git] / server / models / videos.js
index 436c08bfd473d62474c8fb2375cafd12980f11b4..7bd41f7eea7836bfc3ec5d1309a36a53bdb89500 100644 (file)
@@ -1,94 +1,73 @@
 'use strict'
 
-var async = require('async')
-var config = require('config')
-var dz = require('dezalgo')
-var fs = require('fs')
-var mongoose = require('mongoose')
-var path = require('path')
+const async = require('async')
+const config = require('config')
+const mongoose = require('mongoose')
 
-var logger = require('../helpers/logger')
+const logger = require('../helpers/logger')
 
-var http = config.get('webserver.https') === true ? 'https' : 'http'
-var host = config.get('webserver.host')
-var port = config.get('webserver.port')
-var uploadDir = path.join(__dirname, '..', '..', config.get('storage.uploads'))
+const http = config.get('webserver.https') === true ? 'https' : 'http'
+const host = config.get('webserver.host')
+const port = config.get('webserver.port')
 
 // ---------------------------------------------------------------------------
 
-var videosSchema = mongoose.Schema({
+const videosSchema = mongoose.Schema({
   name: String,
   namePath: String,
   description: String,
   magnetUri: String,
-  podUrl: String
+  podUrl: String,
+  author: String,
+  duration: Number,
+  thumbnail: String,
+  createdDate: {
+    type: Date,
+    default: Date.now
+  }
 })
-var VideosDB = mongoose.model('videos', videosSchema)
+const VideosDB = mongoose.model('videos', videosSchema)
 
 // ---------------------------------------------------------------------------
 
-var Videos = {
+const Videos = {
   add: add,
   addRemotes: addRemotes,
   get: get,
-  getVideoState: getVideoState,
-  isOwned: isOwned,
   list: list,
+  listFromUrl: listFromUrl,
+  listFromUrls: listFromUrls,
+  listFromUrlAndMagnets: listFromUrlAndMagnets,
+  listFromRemotes: listFromRemotes,
   listOwned: listOwned,
   removeOwned: removeOwned,
-  removeAllRemotes: removeAllRemotes,
-  removeAllRemotesOf: removeAllRemotesOf,
-  removeRemotesOfByMagnetUris: removeRemotesOfByMagnetUris,
+  removeByIds: removeByIds,
   search: search
 }
 
 function add (video, callback) {
   logger.info('Adding %s video to database.', video.name)
 
-  var params = video
+  const params = video
   params.podUrl = http + '://' + host + ':' + port
 
-  VideosDB.create(params, function (err, video) {
+  VideosDB.create(params, function (err, insertedVideo) {
     if (err) {
       logger.error('Cannot insert this video into database.')
       return callback(err)
     }
 
-    callback(null)
+    callback(null, insertedVideo)
   })
 }
 
-// TODO: avoid doublons
 function addRemotes (videos, callback) {
-  if (!callback) callback = function () {}
-
-  var to_add = []
-
-  async.each(videos, function (video, callback_each) {
-    callback_each = dz(callback_each)
-    logger.debug('Add remote video from pod: %s', video.podUrl)
-
-    var params = {
-      name: video.name,
-      namePath: null,
-      description: video.description,
-      magnetUri: video.magnetUri,
-      podUrl: video.podUrl
-    }
-
-    to_add.push(params)
-
-    callback_each()
-  }, function () {
-    VideosDB.create(to_add, function (err, videos) {
-      if (err) {
-        logger.error('Cannot insert this remote video.')
-        return callback(err)
-      }
-
-      return callback(null, videos)
-    })
+  videos.forEach(function (video) {
+    // Ensure they are remote videos
+    video.namePath = null
   })
+
+  VideosDB.create(videos, callback)
 }
 
 function get (id, callback) {
@@ -102,133 +81,80 @@ function get (id, callback) {
   })
 }
 
-function getVideoState (id, callback) {
-  get(id, function (err, video) {
-    if (err) return callback(err)
-
-    var exist = (video !== null)
-    var owned = false
-    if (exist === true) {
-      owned = (video.namePath !== null)
-    }
-
-    return callback(null, { exist: exist, owned: owned })
-  })
+function list (start, count, sort, callback) {
+  const query = {}
+  return findWithCount(query, start, count, sort, callback)
 }
 
-function isOwned (id, callback) {
-  VideosDB.findById(id, function (err, video) {
-    if (err || !video) {
-      if (!err) err = new Error('Cannot find this video.')
-      logger.error('Cannot find this video.')
-      return callback(err)
-    }
-
-    if (video.namePath === null) {
-      var error_string = 'Cannot remove the video of another pod.'
-      logger.error(error_string)
-      return callback(new Error(error_string), false, video)
-    }
+function listFromUrl (fromUrl, callback) {
+  VideosDB.find({ podUrl: fromUrl }, callback)
+}
 
-    callback(null, true, video)
-  })
+function listFromUrls (fromUrls, callback) {
+  VideosDB.find({ podUrl: { $in: fromUrls } }, callback)
 }
 
-function list (callback) {
-  VideosDB.find(function (err, videos_list) {
-    if (err) {
-      logger.error('Cannot get the list of the videos.')
-      return callback(err)
-    }
+function listFromUrlAndMagnets (fromUrl, magnets, callback) {
+  VideosDB.find({ podUrl: fromUrl, magnetUri: { $in: magnets } }, callback)
+}
 
-    return callback(null, videos_list)
-  })
+function listFromRemotes (callback) {
+  VideosDB.find({ namePath: null }, callback)
 }
 
 function listOwned (callback) {
   // If namePath is not null this is *our* video
-  VideosDB.find({ namePath: { $ne: null } }, function (err, videos_list) {
+  VideosDB.find({ namePath: { $ne: null } }, function (err, videosList) {
     if (err) {
       logger.error('Cannot get the list of owned videos.')
       return callback(err)
     }
 
-    return callback(null, videos_list)
+    return callback(null, videosList)
   })
 }
 
+// Return the video in the callback
 function removeOwned (id, callback) {
-  VideosDB.findByIdAndRemove(id, function (err, video) {
-    if (err) {
-      logger.error('Cannot remove the torrent.')
-      return callback(err)
-    }
-
-    fs.unlink(uploadDir + video.namePath, function (err) {
-      if (err) {
-        logger.error('Cannot remove this video file.')
-        return callback(err)
-      }
-
-      callback(null)
-    })
-  })
+  VideosDB.findByIdAndRemove(id, callback)
 }
 
-function removeAllRemotes (callback) {
-  VideosDB.remove({ namePath: null }, callback)
+// Use the magnet Uri because the _id field is not the same on different servers
+function removeByIds (ids, callback) {
+  VideosDB.remove({ _id: { $in: ids } }, callback)
 }
 
-function removeAllRemotesOf (fromUrl, callback) {
-  VideosDB.remove({ podUrl: fromUrl }, callback)
+function search (value, field, start, count, sort, callback) {
+  const query = {}
+  // Make an exact search with the magnet
+  if (field === 'magnetUri') {
+    query[field] = value
+  } else {
+    query[field] = new RegExp(value)
+  }
+
+  findWithCount(query, start, count, sort, callback)
 }
 
-// Use the magnet Uri because the _id field is not the same on different servers
-function removeRemotesOfByMagnetUris (fromUrl, magnetUris, callback) {
-  if (callback === undefined) callback = function () {}
+// ---------------------------------------------------------------------------
 
-  VideosDB.find({ magnetUri: { $in: magnetUris } }, function (err, videos) {
-    if (err || !videos) {
-      logger.error('Cannot find the torrent URI of these remote videos.')
-      return callback(err)
-    }
+module.exports = Videos
 
-    var to_remove = []
-    async.each(videos, function (video, callback_async) {
-      callback_async = dz(callback_async)
-
-      if (video.podUrl !== fromUrl) {
-        logger.error('The pod %s has not the rights on the video of %s.', fromUrl, video.podUrl)
-      } else {
-        to_remove.push(video._id)
-      }
-
-      callback_async()
-    }, function () {
-      VideosDB.remove({ _id: { $in: to_remove } }, function (err) {
-        if (err) {
-          logger.error('Cannot remove the remote videos.')
-          return callback(err)
-        }
-
-        logger.info('Removed remote videos from %s.', fromUrl)
-        callback(null)
-      })
-    })
-  })
-}
+// ---------------------------------------------------------------------------
 
-function search (name, callback) {
-  VideosDB.find({ name: new RegExp(name) }, function (err, videos) {
-    if (err) {
-      logger.error('Cannot search the videos.')
-      return callback(err)
+function findWithCount (query, start, count, sort, callback) {
+  async.parallel([
+    function (asyncCallback) {
+      VideosDB.find(query).skip(start).limit(start + count).sort(sort).exec(asyncCallback)
+    },
+    function (asyncCallback) {
+      VideosDB.count(query, asyncCallback)
     }
+  ], function (err, results) {
+    if (err) return callback(err)
 
-    return callback(null, videos)
+    const videos = results[0]
+    const totalVideos = results[1]
+    return callback(null, videos, totalVideos)
   })
 }
-
-// ---------------------------------------------------------------------------
-
-module.exports = Videos