]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/remote.js
Server: rename Pods -> Pod
[github/Chocobozzz/PeerTube.git] / server / controllers / api / 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')
feb4bdfd 6const waterfall = require('async/waterfall')
528a9efa 7
feb4bdfd 8const db = require('../../initializers/database')
f253b1c1 9const middlewares = require('../../middlewares')
528a9efa 10const secureMiddleware = middlewares.secure
fc51fde0 11const validators = middlewares.validators.remote
f253b1c1 12const logger = require('../../helpers/logger')
528a9efa
C
13
14const router = express.Router()
15
16router.post('/videos',
fc51fde0 17 validators.signature,
0eb78d53 18 secureMiddleware.checkSignature,
fc51fde0 19 validators.remoteVideos,
528a9efa
C
20 remoteVideos
21)
22
23// ---------------------------------------------------------------------------
24
25module.exports = router
26
27// ---------------------------------------------------------------------------
28
29function 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 53function addRemoteVideo (videoToCreateData, fromHost, callback) {
a078c155 54 logger.debug('Adding remote video "%s".', videoToCreateData.name)
6666aad4 55
feb4bdfd
C
56 waterfall([
57
58 function findOrCreatePod (callback) {
feb4bdfd
C
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)
c77fa067
C
124 }
125
feb4bdfd 126 ], callback)
528a9efa
C
127}
128
49abbbbe 129function removeRemoteVideo (videoToRemoveData, fromHost, callback) {
feb4bdfd
C
130 // TODO: use bulkDestroy?
131
528a9efa 132 // We need the list because we have to remove some other stuffs (thumbnail etc)
feb4bdfd 133 db.Video.listByHostAndRemoteId(fromHost, videoToRemoveData.remoteId, function (err, videosList) {
528a9efa 134 if (err) {
feb4bdfd 135 logger.error('Cannot list videos from host and remote id.', { error: err.message })
aaf61f38 136 return callback(err)
528a9efa
C
137 }
138
6666aad4 139 if (videosList.length === 0) {
feb4bdfd 140 logger.error('No remote video was found for this pod.', { remoteId: videoToRemoveData.remoteId, podHost: fromHost })
6666aad4
C
141 }
142
1a42c9e2 143 each(videosList, function (video, callbackEach) {
feb4bdfd 144 logger.debug('Removing remote video %s.', video.remoteId)
6666aad4 145
feb4bdfd 146 video.destroy().asCallback(callbackEach)
aaf61f38 147 }, callback)
528a9efa
C
148 })
149}