]>
Commit | Line | Data |
---|---|---|
1 | 'use strict' | |
2 | ||
3 | const each = require('async/each') | |
4 | const eachSeries = require('async/eachSeries') | |
5 | const express = require('express') | |
6 | const mongoose = require('mongoose') | |
7 | ||
8 | const middlewares = require('../../../middlewares') | |
9 | const secureMiddleware = middlewares.secure | |
10 | const validators = middlewares.validators.remote | |
11 | const logger = require('../../../helpers/logger') | |
12 | ||
13 | const router = express.Router() | |
14 | const Video = mongoose.model('Video') | |
15 | ||
16 | router.post('/videos', | |
17 | validators.signature, | |
18 | validators.dataToDecrypt, | |
19 | secureMiddleware.decryptBody, | |
20 | validators.remoteVideos, | |
21 | remoteVideos | |
22 | ) | |
23 | ||
24 | // --------------------------------------------------------------------------- | |
25 | ||
26 | module.exports = router | |
27 | ||
28 | // --------------------------------------------------------------------------- | |
29 | ||
30 | function remoteVideos (req, res, next) { | |
31 | const requests = req.body.data | |
32 | const fromUrl = req.body.signature.url | |
33 | ||
34 | // We need to process in the same order to keep consistency | |
35 | // TODO: optimization | |
36 | eachSeries(requests, function (request, callbackEach) { | |
37 | const videoData = request.data | |
38 | ||
39 | if (request.type === 'add') { | |
40 | addRemoteVideo(videoData, callbackEach) | |
41 | } else if (request.type === 'remove') { | |
42 | removeRemoteVideo(videoData, fromUrl, callbackEach) | |
43 | } else { | |
44 | logger.error('Unkown remote request type %s.', request.type) | |
45 | } | |
46 | }, function (err) { | |
47 | if (err) logger.error('Error managing remote videos.', { error: err }) | |
48 | }) | |
49 | ||
50 | // We don't need to keep the other pod waiting | |
51 | return res.type('json').status(204).end() | |
52 | } | |
53 | ||
54 | function addRemoteVideo (videoToCreateData, callback) { | |
55 | logger.debug('Adding remote video %s.', videoToCreateData.magnetUri) | |
56 | ||
57 | // Mongoose pre hook will automatically create the thumbnail on disk | |
58 | videoToCreateData.thumbnail = videoToCreateData.thumbnailBase64 | |
59 | ||
60 | const video = new Video(videoToCreateData) | |
61 | video.save(callback) | |
62 | } | |
63 | ||
64 | function removeRemoteVideo (videoToRemoveData, fromUrl, callback) { | |
65 | // We need the list because we have to remove some other stuffs (thumbnail etc) | |
66 | Video.listByUrlAndMagnet(fromUrl, videoToRemoveData.magnetUri, function (err, videosList) { | |
67 | if (err) { | |
68 | logger.error('Cannot list videos from url and magnets.', { error: err }) | |
69 | return callback(err) | |
70 | } | |
71 | ||
72 | if (videosList.length === 0) { | |
73 | logger.error('No remote video was found for this pod.', { magnetUri: videoToRemoveData.magnetUri, podUrl: fromUrl }) | |
74 | } | |
75 | ||
76 | each(videosList, function (video, callbackEach) { | |
77 | logger.debug('Removing remote video %s.', video.magnetUri) | |
78 | ||
79 | video.remove(callbackEach) | |
80 | }, callback) | |
81 | }) | |
82 | } |