PeerTube/server/controllers/api/v1/remoteVideos.js

70 lines
1.8 KiB
JavaScript
Raw Normal View History

'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')
2016-03-16 16:29:27 -05:00
const middleware = require('../../../middlewares')
const secureMiddleware = middleware.secure
const cacheMiddleware = middleware.cache
const reqValidator = middleware.reqValidators.remote
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
router.post('/add',
reqValidator.secureRequest,
secureMiddleware.decryptBody,
reqValidator.remoteVideosAdd,
cacheMiddleware.cache(false),
addRemoteVideos
)
2016-01-31 04:23:52 -06:00
router.post('/remove',
reqValidator.secureRequest,
secureMiddleware.decryptBody,
reqValidator.remoteVideosRemove,
cacheMiddleware.cache(false),
removeRemoteVideo
)
2016-01-31 04:23:52 -06:00
// ---------------------------------------------------------------------------
2016-01-31 04:23:52 -06:00
module.exports = router
2016-01-31 04:23:52 -06:00
// ---------------------------------------------------------------------------
2016-01-31 04:23:52 -06:00
function addRemoteVideos (req, res, next) {
const videos_to_create = req.body.data
videos.createRemoteVideos(videos_to_create, function (err, remote_videos) {
if (err) {
logger.error('Cannot create remote videos.', { error: err })
return next(err)
}
2015-06-09 10:41:40 -05:00
res.type('json').status(201).end()
})
}
2015-06-09 10:41:40 -05:00
function removeRemoteVideo (req, res, next) {
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
Videos.listFromUrlAndMagnets(fromUrl, magnetUris, function (err, videos_list) {
if (err) {
logger.error('Cannot list videos from url and magnets.', { error: err })
return next(err)
}
2015-06-09 10:41:40 -05:00
videos.removeRemoteVideos(videos_list, function (err) {
if (err) {
logger.error('Cannot remove remote videos.', { error: err })
return next(err)
}
res.type('json').status(204).end()
})
})
}