]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/api/v1/remote.js
Server: put config in constants
[github/Chocobozzz/PeerTube.git] / server / controllers / api / v1 / remote.js
index 2d71c605d9852e4623b92bf17b7d4a361baf2895..f452986b8e951aa0498fb5b35b7a4ffca0f6dde3 100644 (file)
@@ -1,22 +1,23 @@
 'use strict'
 
-const async = require('async')
+const each = require('async/each')
+const eachSeries = require('async/eachSeries')
 const express = require('express')
 const mongoose = require('mongoose')
 
 const middlewares = require('../../../middlewares')
 const secureMiddleware = middlewares.secure
-const reqValidator = middlewares.reqValidators.remote
+const validators = middlewares.validators.remote
 const logger = require('../../../helpers/logger')
 
 const router = express.Router()
 const Video = mongoose.model('Video')
 
 router.post('/videos',
-  reqValidator.signature,
-  reqValidator.dataToDecrypt,
+  validators.signature,
+  validators.dataToDecrypt,
   secureMiddleware.decryptBody,
-  reqValidator.remoteVideos,
+  validators.remoteVideos,
   remoteVideos
 )
 
@@ -32,13 +33,15 @@ function remoteVideos (req, res, next) {
 
   // We need to process in the same order to keep consistency
   // TODO: optimization
-  async.eachSeries(requests, function (request, callbackEach) {
+  eachSeries(requests, function (request, callbackEach) {
     const videoData = request.data
 
     if (request.type === 'add') {
       addRemoteVideo(videoData, callbackEach)
     } else if (request.type === 'remove') {
       removeRemoteVideo(videoData, fromUrl, callbackEach)
+    } else {
+      logger.error('Unkown remote request type %s.', request.type)
     }
   }, function (err) {
     if (err) logger.error('Error managing remote videos.', { error: err })
@@ -49,6 +52,8 @@ function remoteVideos (req, res, next) {
 }
 
 function addRemoteVideo (videoToCreateData, callback) {
+  logger.debug('Adding remote video %s.', videoToCreateData.magnetUri)
+
   // Mongoose pre hook will automatically create the thumbnail on disk
   videoToCreateData.thumbnail = videoToCreateData.thumbnailBase64
 
@@ -64,7 +69,13 @@ function removeRemoteVideo (videoToRemoveData, fromUrl, callback) {
       return callback(err)
     }
 
-    async.each(videosList, function (video, callbackEach) {
+    if (videosList.length === 0) {
+      logger.error('No remote video was found for this pod.', { magnetUri: videoToRemoveData.magnetUri, podUrl: fromUrl })
+    }
+
+    each(videosList, function (video, callbackEach) {
+      logger.debug('Removing remote video %s.', video.magnetUri)
+
       video.remove(callbackEach)
     }, callback)
   })