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