2016-02-07 04:23:23 -06:00
|
|
|
'use strict'
|
2015-06-09 10:41:40 -05:00
|
|
|
|
2016-03-16 16:29:27 -05:00
|
|
|
const express = require('express')
|
2016-04-26 14:56:46 -05:00
|
|
|
const map = require('lodash/map')
|
2015-12-04 09:13:32 -06:00
|
|
|
|
2016-05-13 09:31:14 -05:00
|
|
|
const middlewares = require('../../../middlewares')
|
|
|
|
const secureMiddleware = middlewares.secure
|
|
|
|
const reqValidator = middlewares.reqValidators.remote
|
2016-05-10 14:19:24 -05:00
|
|
|
const logger = require('../../../helpers/logger')
|
|
|
|
const Videos = require('../../../models/videos')
|
|
|
|
const videos = require('../../../lib/videos')
|
2015-06-09 10:41:40 -05:00
|
|
|
|
2016-03-16 16:29:27 -05:00
|
|
|
const router = express.Router()
|
2016-01-31 04:23:52 -06:00
|
|
|
|
2016-02-07 04:23:23 -06:00
|
|
|
router.post('/add',
|
|
|
|
reqValidator.secureRequest,
|
|
|
|
secureMiddleware.decryptBody,
|
|
|
|
reqValidator.remoteVideosAdd,
|
|
|
|
addRemoteVideos
|
|
|
|
)
|
2016-01-31 04:23:52 -06:00
|
|
|
|
2016-02-07 04:23:23 -06:00
|
|
|
router.post('/remove',
|
|
|
|
reqValidator.secureRequest,
|
|
|
|
secureMiddleware.decryptBody,
|
|
|
|
reqValidator.remoteVideosRemove,
|
|
|
|
removeRemoteVideo
|
|
|
|
)
|
2016-01-31 04:23:52 -06:00
|
|
|
|
2016-02-07 04:23:23 -06:00
|
|
|
// ---------------------------------------------------------------------------
|
2016-01-31 04:23:52 -06:00
|
|
|
|
2016-02-07 04:23:23 -06:00
|
|
|
module.exports = router
|
2016-01-31 04:23:52 -06:00
|
|
|
|
2016-02-07 04:23:23 -06:00
|
|
|
// ---------------------------------------------------------------------------
|
2016-01-31 04:23:52 -06:00
|
|
|
|
2016-02-07 04:23:23 -06:00
|
|
|
function addRemoteVideos (req, res, next) {
|
2016-05-11 14:19:34 -05:00
|
|
|
const videosToCreate = req.body.data
|
|
|
|
videos.createRemoteVideos(videosToCreate, function (err, remoteVideos) {
|
2016-05-10 14:19:24 -05:00
|
|
|
if (err) {
|
|
|
|
logger.error('Cannot create remote videos.', { error: err })
|
|
|
|
return next(err)
|
|
|
|
}
|
2015-06-09 10:41:40 -05:00
|
|
|
|
2016-05-10 14:19:24 -05:00
|
|
|
res.type('json').status(201).end()
|
2016-02-07 04:23:23 -06:00
|
|
|
})
|
|
|
|
}
|
2015-06-09 10:41:40 -05:00
|
|
|
|
2016-02-07 04:23:23 -06:00
|
|
|
function removeRemoteVideo (req, res, next) {
|
2016-05-10 14:19:24 -05:00
|
|
|
const fromUrl = req.body.signature.url
|
2016-03-18 10:34:50 -05:00
|
|
|
const magnetUris = map(req.body.data, 'magnetUri')
|
2016-02-04 14:10:33 -06:00
|
|
|
|
2016-05-11 14:19:34 -05:00
|
|
|
Videos.listFromUrlAndMagnets(fromUrl, magnetUris, function (err, videosList) {
|
2016-05-10 14:19:24 -05:00
|
|
|
if (err) {
|
|
|
|
logger.error('Cannot list videos from url and magnets.', { error: err })
|
|
|
|
return next(err)
|
|
|
|
}
|
2015-06-09 10:41:40 -05:00
|
|
|
|
2016-05-11 14:19:34 -05:00
|
|
|
videos.removeRemoteVideos(videosList, function (err) {
|
2016-05-10 14:19:24 -05:00
|
|
|
if (err) {
|
|
|
|
logger.error('Cannot remove remote videos.', { error: err })
|
|
|
|
return next(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
res.type('json').status(204).end()
|
|
|
|
})
|
2016-02-07 04:23:23 -06:00
|
|
|
})
|
|
|
|
}
|