]>
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 | 7 | |
f253b1c1 | 8 | const middlewares = require('../../middlewares') |
528a9efa | 9 | const secureMiddleware = middlewares.secure |
fc51fde0 | 10 | const validators = middlewares.validators.remote |
f253b1c1 | 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 | 17 | validators.signature, |
0eb78d53 | 18 | secureMiddleware.checkSignature, |
fc51fde0 | 19 | validators.remoteVideos, |
528a9efa C |
20 | remoteVideos |
21 | ) | |
22 | ||
23 | // --------------------------------------------------------------------------- | |
24 | ||
25 | module.exports = router | |
26 | ||
27 | // --------------------------------------------------------------------------- | |
28 | ||
29 | function remoteVideos (req, res, next) { | |
30 | const requests = req.body.data | |
49abbbbe | 31 | const fromHost = req.body.signature.host |
528a9efa C |
32 | |
33 | // We need to process in the same order to keep consistency | |
34 | // TODO: optimization | |
1a42c9e2 | 35 | eachSeries(requests, function (request, callbackEach) { |
aaf61f38 | 36 | const videoData = request.data |
528a9efa C |
37 | |
38 | if (request.type === 'add') { | |
437cf8b5 | 39 | addRemoteVideo(videoData, fromHost, callbackEach) |
528a9efa | 40 | } else if (request.type === 'remove') { |
49abbbbe | 41 | removeRemoteVideo(videoData, fromHost, callbackEach) |
6666aad4 C |
42 | } else { |
43 | logger.error('Unkown remote request type %s.', request.type) | |
528a9efa | 44 | } |
aaf61f38 C |
45 | }, function (err) { |
46 | if (err) logger.error('Error managing remote videos.', { error: err }) | |
528a9efa C |
47 | }) |
48 | ||
49 | // We don't need to keep the other pod waiting | |
50 | return res.type('json').status(204).end() | |
51 | } | |
52 | ||
437cf8b5 | 53 | function addRemoteVideo (videoToCreateData, fromHost, callback) { |
a078c155 | 54 | logger.debug('Adding remote video "%s".', videoToCreateData.name) |
6666aad4 | 55 | |
aaf61f38 | 56 | const video = new Video(videoToCreateData) |
437cf8b5 | 57 | video.podHost = fromHost |
c77fa067 C |
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 | }) | |
528a9efa C |
66 | } |
67 | ||
49abbbbe | 68 | function removeRemoteVideo (videoToRemoveData, fromHost, callback) { |
528a9efa | 69 | // We need the list because we have to remove some other stuffs (thumbnail etc) |
49abbbbe | 70 | Video.listByHostAndRemoteId(fromHost, videoToRemoveData.remoteId, function (err, videosList) { |
528a9efa | 71 | if (err) { |
49abbbbe | 72 | logger.error('Cannot list videos from host and magnets.', { error: err }) |
aaf61f38 | 73 | return callback(err) |
528a9efa C |
74 | } |
75 | ||
6666aad4 | 76 | if (videosList.length === 0) { |
49abbbbe | 77 | logger.error('No remote video was found for this pod.', { magnetUri: videoToRemoveData.magnetUri, podHost: fromHost }) |
6666aad4 C |
78 | } |
79 | ||
1a42c9e2 | 80 | each(videosList, function (video, callbackEach) { |
6666aad4 C |
81 | logger.debug('Removing remote video %s.', video.magnetUri) |
82 | ||
aaf61f38 C |
83 | video.remove(callbackEach) |
84 | }, callback) | |
528a9efa C |
85 | }) |
86 | } |