]>
Commit | Line | Data |
---|---|---|
528a9efa C |
1 | 'use strict' |
2 | ||
3 | const async = require('async') | |
4 | const express = require('express') | |
aaf61f38 | 5 | const mongoose = require('mongoose') |
528a9efa C |
6 | |
7 | const middlewares = require('../../../middlewares') | |
8 | const secureMiddleware = middlewares.secure | |
fc51fde0 | 9 | const validators = middlewares.validators.remote |
528a9efa | 10 | const logger = require('../../../helpers/logger') |
528a9efa C |
11 | |
12 | const router = express.Router() | |
aaf61f38 | 13 | const Video = mongoose.model('Video') |
528a9efa C |
14 | |
15 | router.post('/videos', | |
fc51fde0 C |
16 | validators.signature, |
17 | validators.dataToDecrypt, | |
528a9efa | 18 | secureMiddleware.decryptBody, |
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 | |
31 | const fromUrl = req.body.signature.url | |
32 | ||
33 | // We need to process in the same order to keep consistency | |
34 | // TODO: optimization | |
35 | async.eachSeries(requests, function (request, callbackEach) { | |
aaf61f38 | 36 | const videoData = request.data |
528a9efa C |
37 | |
38 | if (request.type === 'add') { | |
aaf61f38 | 39 | addRemoteVideo(videoData, callbackEach) |
528a9efa | 40 | } else if (request.type === 'remove') { |
aaf61f38 | 41 | removeRemoteVideo(videoData, fromUrl, callbackEach) |
528a9efa | 42 | } |
aaf61f38 C |
43 | }, function (err) { |
44 | if (err) logger.error('Error managing remote videos.', { error: err }) | |
528a9efa C |
45 | }) |
46 | ||
47 | // We don't need to keep the other pod waiting | |
48 | return res.type('json').status(204).end() | |
49 | } | |
50 | ||
aaf61f38 C |
51 | function addRemoteVideo (videoToCreateData, callback) { |
52 | // Mongoose pre hook will automatically create the thumbnail on disk | |
53 | videoToCreateData.thumbnail = videoToCreateData.thumbnailBase64 | |
528a9efa | 54 | |
aaf61f38 C |
55 | const video = new Video(videoToCreateData) |
56 | video.save(callback) | |
528a9efa C |
57 | } |
58 | ||
aaf61f38 | 59 | function removeRemoteVideo (videoToRemoveData, fromUrl, callback) { |
528a9efa | 60 | // We need the list because we have to remove some other stuffs (thumbnail etc) |
aaf61f38 | 61 | Video.listByUrlAndMagnet(fromUrl, videoToRemoveData.magnetUri, function (err, videosList) { |
528a9efa C |
62 | if (err) { |
63 | logger.error('Cannot list videos from url and magnets.', { error: err }) | |
aaf61f38 | 64 | return callback(err) |
528a9efa C |
65 | } |
66 | ||
aaf61f38 C |
67 | async.each(videosList, function (video, callbackEach) { |
68 | video.remove(callbackEach) | |
69 | }, callback) | |
528a9efa C |
70 | }) |
71 | } |