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