'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 Videos = require('../../../models/videos')
-const videos = require('../../../lib/videos')
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
)
// We need to process in the same order to keep consistency
// TODO: optimization
- async.eachSeries(requests, function (request, callbackEach) {
- const video = request.data
+ eachSeries(requests, function (request, callbackEach) {
+ const videoData = request.data
if (request.type === 'add') {
- addRemoteVideo(video, callbackEach)
+ addRemoteVideo(videoData, callbackEach)
} else if (request.type === 'remove') {
- removeRemoteVideo(video, fromUrl, callbackEach)
+ 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 })
})
// We don't need to keep the other pod waiting
return res.type('json').status(204).end()
}
-function addRemoteVideo (videoToCreate, callback) {
- videos.createRemoteVideos([ videoToCreate ], function (err, remoteVideos) {
- if (err) {
- logger.error('Cannot create remote videos.', { error: err })
- // Don't break the process
- }
+function addRemoteVideo (videoToCreateData, callback) {
+ logger.debug('Adding remote video %s.', videoToCreateData.magnetUri)
- return callback()
- })
-}
+ // Mongoose pre hook will automatically create the thumbnail on disk
+ videoToCreateData.thumbnail = videoToCreateData.thumbnailBase64
-function removeRemoteVideo (videoToRemove, fromUrl, callback) {
- const magnetUris = [ videoToRemove.magnetUri ]
+ const video = new Video(videoToCreateData)
+ video.save(callback)
+}
+function removeRemoteVideo (videoToRemoveData, fromUrl, callback) {
// We need the list because we have to remove some other stuffs (thumbnail etc)
- Videos.listFromUrlAndMagnets(fromUrl, magnetUris, function (err, videosList) {
+ Video.listByUrlAndMagnet(fromUrl, videoToRemoveData.magnetUri, function (err, videosList) {
if (err) {
logger.error('Cannot list videos from url and magnets.', { error: err })
- // Don't break the process
- return callback()
+ return callback(err)
+ }
+
+ if (videosList.length === 0) {
+ logger.error('No remote video was found for this pod.', { magnetUri: videoToRemoveData.magnetUri, podUrl: fromUrl })
}
- videos.removeRemoteVideos(videosList, function (err) {
- if (err) {
- logger.error('Cannot remove remote videos.', { error: err })
- // Don't break the process
- }
+ each(videosList, function (video, callbackEach) {
+ logger.debug('Removing remote video %s.', video.magnetUri)
- return callback()
- })
+ video.remove(callbackEach)
+ }, callback)
})
}