aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/remote.js
diff options
context:
space:
mode:
Diffstat (limited to 'server/controllers/api/remote.js')
-rw-r--r--server/controllers/api/remote.js86
1 files changed, 0 insertions, 86 deletions
diff --git a/server/controllers/api/remote.js b/server/controllers/api/remote.js
deleted file mode 100644
index f1046c534..000000000
--- a/server/controllers/api/remote.js
+++ /dev/null
@@ -1,86 +0,0 @@
1'use strict'
2
3const each = require('async/each')
4const eachSeries = require('async/eachSeries')
5const express = require('express')
6const mongoose = require('mongoose')
7
8const middlewares = require('../../middlewares')
9const secureMiddleware = middlewares.secure
10const validators = middlewares.validators.remote
11const logger = require('../../helpers/logger')
12
13const router = express.Router()
14const Video = mongoose.model('Video')
15
16router.post('/videos',
17 validators.signature,
18 secureMiddleware.checkSignature,
19 validators.remoteVideos,
20 remoteVideos
21)
22
23// ---------------------------------------------------------------------------
24
25module.exports = router
26
27// ---------------------------------------------------------------------------
28
29function remoteVideos (req, res, next) {
30 const requests = req.body.data
31 const fromHost = req.body.signature.host
32
33 // We need to process in the same order to keep consistency
34 // TODO: optimization
35 eachSeries(requests, function (request, callbackEach) {
36 const videoData = request.data
37
38 if (request.type === 'add') {
39 addRemoteVideo(videoData, fromHost, callbackEach)
40 } else if (request.type === 'remove') {
41 removeRemoteVideo(videoData, fromHost, callbackEach)
42 } else {
43 logger.error('Unkown remote request type %s.', request.type)
44 }
45 }, function (err) {
46 if (err) logger.error('Error managing remote videos.', { error: err })
47 })
48
49 // We don't need to keep the other pod waiting
50 return res.type('json').status(204).end()
51}
52
53function addRemoteVideo (videoToCreateData, fromHost, callback) {
54 logger.debug('Adding remote video "%s".', videoToCreateData.name)
55
56 const video = new Video(videoToCreateData)
57 video.podHost = fromHost
58 Video.generateThumbnailFromBase64(video, videoToCreateData.thumbnailBase64, function (err) {
59 if (err) {
60 logger.error('Cannot generate thumbnail from base 64 data.', { error: err })
61 return callback(err)
62 }
63
64 video.save(callback)
65 })
66}
67
68function removeRemoteVideo (videoToRemoveData, fromHost, callback) {
69 // We need the list because we have to remove some other stuffs (thumbnail etc)
70 Video.listByHostAndRemoteId(fromHost, videoToRemoveData.remoteId, function (err, videosList) {
71 if (err) {
72 logger.error('Cannot list videos from host and magnets.', { error: err })
73 return callback(err)
74 }
75
76 if (videosList.length === 0) {
77 logger.error('No remote video was found for this pod.', { magnetUri: videoToRemoveData.magnetUri, podHost: fromHost })
78 }
79
80 each(videosList, function (video, callbackEach) {
81 logger.debug('Removing remote video %s.', video.magnetUri)
82
83 video.remove(callbackEach)
84 }, callback)
85 })
86}