]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - src/videos.js
Update node modules
[github/Chocobozzz/PeerTube.git] / src / videos.js
index f787ae49cddacfec8ba153244d6ea84170806720..32f26abe7f17539f793433e04df3920051ab10d7 100644 (file)
@@ -1,27 +1,28 @@
 ;(function () {
   'use strict'
 
+  var async = require('async')
+  var config = require('config')
+  var dz = require('dezalgo')
   var fs = require('fs')
   var webtorrent = require('./webTorrentNode')
-  var config = require('config')
-  var async = require('async')
 
   var logger = require('./logger')
-  var VideosDB = require('./database').VideosDB
   var pods = require('./pods')
+  var VideosDB = require('./database').VideosDB
 
   var videos = {}
-  // Public url
+
   var http = config.get('webserver.https') === true ? 'https' : 'http'
   var host = config.get('webserver.host')
   var port = config.get('webserver.port')
 
   // ----------- Private functions -----------
   function seedVideo (path, callback) {
-    logger.debug('Seeding : %s', path)
+    logger.info('Seeding %s...', path)
 
     webtorrent.seed(path, function (torrent) {
-      logger.debug('Seeded : %s', torrent.magnetURI)
+      logger.info('%s seeded (%s).', path, torrent.magnetURI)
 
       return callback(null, torrent)
     })
@@ -46,7 +47,7 @@
     var video_file = data.video
     var video_data = data.data
 
-    logger.debug('Path: %s', video_file.path)
+    logger.info('Adding %s video.', video_file.path)
     seedVideo(video_file.path, function (err, torrent) {
       if (err) {
         logger.error('Cannot seed this video.', { error: err })
@@ -55,7 +56,7 @@
 
       var params = {
         name: video_data.name,
-        namePath: video_file.name,
+        namePath: video_file.filename,
         description: video_data.description,
         magnetUri: torrent.magnetURI,
         podUrl: http + '://' + host + ':' + port
           return callback(err)
         }
 
-        // Now we'll send the video's meta data
+        // Now we'll add the video's meta data to our friends
         params.namePath = null
 
-        logger.debug('Sending this video Uri to friends...')
-
-        var data = {
-          path: '/api/' + global.API_VERSION + '/remotevideos/add',
-          method: 'POST',
-          data: params
-        }
-
-        pods.makeSecureRequest(data, function (err) {
-          if (err) {
-            logger.error('Somes issues when sending this video to friends.', { error: err })
-            return callback(err)
-          }
-
-          return callback(null)
-        })
+        pods.addVideoToFriends(params)
+        callback(null)
       })
     })
   }
 
   videos.remove = function (id, callback) {
-    // Maybe the torrent is not seeding, it doesn't have importance
+    // Maybe the torrent is not seeded, but we catch the error to don't stop the removing process
     function removeTorrent (magnetUri, callback) {
       try {
         webtorrent.remove(magnetUri, callback)
         return callback(new Error(error_string))
       }
 
-      logger.debug('Removing video %s', video.magnetUri)
+      logger.info('Removing %s video', video.name)
 
       removeTorrent(video.magnetUri, function () {
         VideosDB.findByIdAndRemove(id, function (err) {
               return callback(err)
             }
 
-            var data = {
-              path: '/api/' + global.API_VERSION + '/remotevideos/remove',
-              method: 'POST',
-              data: {
-                magnetUri: video.magnetUri
-              }
+            var params = {
+              name: video.name,
+              magnetUri: video.magnetUri
             }
 
-            // Yes this is a POST request because we add some informations in the body (signature, encrypt etc)
-            pods.makeSecureRequest(data, function (err) {
-              if (err) {
-                logger.error('Somes issues when sending we want to remove the video to friends.', { error: err })
-                return callback(err)
-              }
-
-              callback(null)
-            })
+            pods.removeVideoToFriends(params)
+            callback(null)
           })
         })
       })
   }
 
   // Use the magnet Uri because the _id field is not the same on different servers
-  videos.removeRemote = function (fromUrl, magnetUri, callback) {
-    // TODO : check if the remote server has the rights to remove this video
-
-    VideosDB.findOne({ magnetUri: magnetUri }, function (err, video) {
-      if (err || !video) {
-        logger.error('Cannot find the torrent URI of this remote video.')
+  videos.removeRemotes = function (fromUrl, magnetUris, callback) {
+    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)
       }
 
-      if (video.podUrl !== fromUrl) {
-        logger.error('The pod has not rights on this video.')
-        return callback(err)
-      }
+      var to_remove = []
+      async.each(videos, function (video, callback_async) {
+        callback_async = dz(callback_async)
 
-      VideosDB.findByIdAndRemove(video._id, function (err) {
-        if (err) {
-          logger.error('Cannot remove the remote video.')
-          return callback(err)
+        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(null)
+        callback_async()
+      }, function () {
+        VideosDB.remove({ _id: { $in: to_remove } }, function (err) {
+          if (err) {
+            logger.error('Cannot remove the remote videos.')
+            return callback(err)
+          }
+
+          callback(null)
+        })
       })
     })
   }
 
   // { name, magnetUri, podUrl }
-  videos.addRemote = function (data, callback) {
-    logger.debug('Add remote video from pod: %s', data.podUrl)
-
-    var params = {
-      name: data.name,
-      namePath: null,
-      description: data.description,
-      magnetUri: data.magnetUri,
-      podUrl: data.podUrl
-    }
+  videos.addRemotes = function (videos, callback) {
+    var to_add = []
 
-    VideosDB.create(params, function (err, video) {
-      if (err) {
-        logger.error('Cannot insert this remote video.', { error: err })
-        return callback(err)
+    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
       }
 
-      return callback(null, video)
+      to_add.push(params)
+
+      callback_each()
+    }, function () {
+      VideosDB.create(to_add, function (err, videos) {
+        if (err) {
+          logger.error('Cannot insert this remote video.', { error: err })
+          return callback(err)
+        }
+
+        return callback(null, videos)
+      })
     })
   }
 
     })
   }
 
-  videos.seedAll = function (final_callback) {
+  videos.seedAll = function (callback) {
     VideosDB.find({ namePath: { $ne: null } }, function (err, videos_list) {
       if (err) {
         logger.error('Cannot get list of the videos to seed.', { error: err })
-        return final_callback(err)
+        return callback(err)
       }
 
-      async.each(videos_list, function (video, callback) {
+      async.each(videos_list, function (video, each_callback) {
         seedVideo(videos.uploadDir + video.namePath, function (err) {
           if (err) {
             logger.error('Cannot seed this video.', { error: err })
             return callback(err)
           }
 
-          callback(null)
+          each_callback(null)
         })
-      }, final_callback)
+      }, callback)
     })
   }