]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/remote.js
Server: rename Pods -> Pod
[github/Chocobozzz/PeerTube.git] / server / controllers / api / remote.js
1 'use strict'
2
3 const each = require('async/each')
4 const eachSeries = require('async/eachSeries')
5 const express = require('express')
6 const waterfall = require('async/waterfall')
7
8 const db = require('../../initializers/database')
9 const middlewares = require('../../middlewares')
10 const secureMiddleware = middlewares.secure
11 const validators = middlewares.validators.remote
12 const logger = require('../../helpers/logger')
13
14 const router = express.Router()
15
16 router.post('/videos',
17 validators.signature,
18 secureMiddleware.checkSignature,
19 validators.remoteVideos,
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 fromHost = req.body.signature.host
32
33 // We need to process in the same order to keep consistency
34 // TODO: optimization
35 eachSeries(requests, function (request, callbackEach) {
36 const videoData = request.data
37
38 if (request.type === 'add') {
39 addRemoteVideo(videoData, fromHost, callbackEach)
40 } else if (request.type === 'remove') {
41 removeRemoteVideo(videoData, fromHost, callbackEach)
42 } else {
43 logger.error('Unkown remote request type %s.', request.type)
44 }
45 }, function (err) {
46 if (err) logger.error('Error managing remote videos.', { error: err })
47 })
48
49 // We don't need to keep the other pod waiting
50 return res.type('json').status(204).end()
51 }
52
53 function addRemoteVideo (videoToCreateData, fromHost, callback) {
54 logger.debug('Adding remote video "%s".', videoToCreateData.name)
55
56 waterfall([
57
58 function findOrCreatePod (callback) {
59 const query = {
60 where: {
61 host: fromHost
62 },
63 defaults: {
64 host: fromHost
65 }
66 }
67
68 db.Pod.findOrCreate(query).asCallback(function (err, result) {
69 // [ instance, wasCreated ]
70 return callback(err, result[0])
71 })
72 },
73
74 function findOrCreateAuthor (pod, callback) {
75 const username = videoToCreateData.author
76
77 const query = {
78 where: {
79 name: username,
80 podId: pod.id
81 },
82 defaults: {
83 name: username,
84 podId: pod.id
85 }
86 }
87
88 db.Author.findOrCreate(query).asCallback(function (err, result) {
89 // [ instance, wasCreated ]
90 return callback(err, result[0])
91 })
92 },
93
94 function createVideoObject (author, callback) {
95 const videoData = {
96 name: videoToCreateData.name,
97 remoteId: videoToCreateData.remoteId,
98 extname: videoToCreateData.extname,
99 infoHash: videoToCreateData.infoHash,
100 description: videoToCreateData.description,
101 authorId: author.id,
102 duration: videoToCreateData.duration,
103 tags: videoToCreateData.tags
104 }
105
106 const video = db.Video.build(videoData)
107
108 return callback(null, video)
109 },
110
111 function generateThumbnail (video, callback) {
112 db.Video.generateThumbnailFromBase64(video, videoToCreateData.thumbnailBase64, function (err) {
113 if (err) {
114 logger.error('Cannot generate thumbnail from base 64 data.', { error: err })
115 return callback(err)
116 }
117
118 video.save().asCallback(callback)
119 })
120 },
121
122 function insertIntoDB (video, callback) {
123 video.save().asCallback(callback)
124 }
125
126 ], callback)
127 }
128
129 function removeRemoteVideo (videoToRemoveData, fromHost, callback) {
130 // TODO: use bulkDestroy?
131
132 // We need the list because we have to remove some other stuffs (thumbnail etc)
133 db.Video.listByHostAndRemoteId(fromHost, videoToRemoveData.remoteId, function (err, videosList) {
134 if (err) {
135 logger.error('Cannot list videos from host and remote id.', { error: err.message })
136 return callback(err)
137 }
138
139 if (videosList.length === 0) {
140 logger.error('No remote video was found for this pod.', { remoteId: videoToRemoveData.remoteId, podHost: fromHost })
141 }
142
143 each(videosList, function (video, callbackEach) {
144 logger.debug('Removing remote video %s.', video.remoteId)
145
146 video.destroy().asCallback(callbackEach)
147 }, callback)
148 })
149 }