aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/v1/remoteVideos.js
blob: 8ff212b7f06b2a258bb3c2255a2de2ee6189699e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
'use strict'

const express = require('express')
const map = require('lodash/map')

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')

const router = express.Router()

router.post('/add',
  reqValidator.secureRequest,
  secureMiddleware.decryptBody,
  reqValidator.remoteVideosAdd,
  cacheMiddleware.cache(false),
  addRemoteVideos
)

router.post('/remove',
  reqValidator.secureRequest,
  secureMiddleware.decryptBody,
  reqValidator.remoteVideosRemove,
  cacheMiddleware.cache(false),
  removeRemoteVideo
)

// ---------------------------------------------------------------------------

module.exports = router

// ---------------------------------------------------------------------------

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)
    }

    res.type('json').status(201).end()
  })
}

function removeRemoteVideo (req, res, next) {
  const fromUrl = req.body.signature.url
  const magnetUris = map(req.body.data, 'magnetUri')

  Videos.listFromUrlAndMagnets(fromUrl, magnetUris, function (err, videos_list) {
    if (err) {
      logger.error('Cannot list videos from url and magnets.', { error: err })
      return next(err)
    }

    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()
    })
  })
}