]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/videos.js
Server: propagate video update to other pods
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos.js
1 'use strict'
2
3 const express = require('express')
4 const fs = require('fs')
5 const multer = require('multer')
6 const path = require('path')
7 const waterfall = require('async/waterfall')
8
9 const constants = require('../../initializers/constants')
10 const db = require('../../initializers/database')
11 const logger = require('../../helpers/logger')
12 const friends = require('../../lib/friends')
13 const middlewares = require('../../middlewares')
14 const oAuth = middlewares.oauth
15 const pagination = middlewares.pagination
16 const validators = middlewares.validators
17 const validatorsPagination = validators.pagination
18 const validatorsSort = validators.sort
19 const validatorsVideos = validators.videos
20 const search = middlewares.search
21 const sort = middlewares.sort
22 const utils = require('../../helpers/utils')
23
24 const router = express.Router()
25
26 // multer configuration
27 const storage = multer.diskStorage({
28 destination: function (req, file, cb) {
29 cb(null, constants.CONFIG.STORAGE.VIDEOS_DIR)
30 },
31
32 filename: function (req, file, cb) {
33 let extension = ''
34 if (file.mimetype === 'video/webm') extension = 'webm'
35 else if (file.mimetype === 'video/mp4') extension = 'mp4'
36 else if (file.mimetype === 'video/ogg') extension = 'ogv'
37 utils.generateRandomString(16, function (err, randomString) {
38 const fieldname = err ? undefined : randomString
39 cb(null, fieldname + '.' + extension)
40 })
41 }
42 })
43
44 const reqFiles = multer({ storage: storage }).fields([{ name: 'videofile', maxCount: 1 }])
45
46 router.get('/',
47 validatorsPagination.pagination,
48 validatorsSort.videosSort,
49 sort.setVideosSort,
50 pagination.setPagination,
51 listVideos
52 )
53 router.put('/:id',
54 oAuth.authenticate,
55 reqFiles,
56 validatorsVideos.videosUpdate,
57 updateVideo
58 )
59 router.post('/',
60 oAuth.authenticate,
61 reqFiles,
62 validatorsVideos.videosAdd,
63 addVideo
64 )
65 router.get('/:id',
66 validatorsVideos.videosGet,
67 getVideo
68 )
69 router.delete('/:id',
70 oAuth.authenticate,
71 validatorsVideos.videosRemove,
72 removeVideo
73 )
74 router.get('/search/:value',
75 validatorsVideos.videosSearch,
76 validatorsPagination.pagination,
77 validatorsSort.videosSort,
78 sort.setVideosSort,
79 pagination.setPagination,
80 search.setVideosSearch,
81 searchVideos
82 )
83
84 // ---------------------------------------------------------------------------
85
86 module.exports = router
87
88 // ---------------------------------------------------------------------------
89
90 function addVideo (req, res, next) {
91 const videoFile = req.files.videofile[0]
92 const videoInfos = req.body
93
94 waterfall([
95
96 function startTransaction (callback) {
97 db.sequelize.transaction().asCallback(function (err, t) {
98 return callback(err, t)
99 })
100 },
101
102 function findOrCreateAuthor (t, callback) {
103 const user = res.locals.oauth.token.User
104
105 const name = user.username
106 // null because it is OUR pod
107 const podId = null
108 const userId = user.id
109
110 db.Author.findOrCreateAuthor(name, podId, userId, t, function (err, authorInstance) {
111 return callback(err, t, authorInstance)
112 })
113 },
114
115 function findOrCreateTags (t, author, callback) {
116 const tags = videoInfos.tags
117
118 db.Tag.findOrCreateTags(tags, t, function (err, tagInstances) {
119 return callback(err, t, author, tagInstances)
120 })
121 },
122
123 function createVideoObject (t, author, tagInstances, callback) {
124 const videoData = {
125 name: videoInfos.name,
126 remoteId: null,
127 extname: path.extname(videoFile.filename),
128 description: videoInfos.description,
129 duration: videoFile.duration,
130 authorId: author.id
131 }
132
133 const video = db.Video.build(videoData)
134
135 return callback(null, t, author, tagInstances, video)
136 },
137
138 // Set the videoname the same as the id
139 function renameVideoFile (t, author, tagInstances, video, callback) {
140 const videoDir = constants.CONFIG.STORAGE.VIDEOS_DIR
141 const source = path.join(videoDir, videoFile.filename)
142 const destination = path.join(videoDir, video.getVideoFilename())
143
144 fs.rename(source, destination, function (err) {
145 return callback(err, t, author, tagInstances, video)
146 })
147 },
148
149 function insertVideoIntoDB (t, author, tagInstances, video, callback) {
150 const options = { transaction: t }
151
152 // Add tags association
153 video.save(options).asCallback(function (err, videoCreated) {
154 if (err) return callback(err)
155
156 // Do not forget to add Author informations to the created video
157 videoCreated.Author = author
158
159 return callback(err, t, tagInstances, videoCreated)
160 })
161 },
162
163 function associateTagsToVideo (t, tagInstances, video, callback) {
164 const options = { transaction: t }
165
166 video.setTags(tagInstances, options).asCallback(function (err) {
167 video.Tags = tagInstances
168
169 return callback(err, t, video)
170 })
171 },
172
173 function sendToFriends (t, video, callback) {
174 video.toAddRemoteJSON(function (err, remoteVideo) {
175 if (err) return callback(err)
176
177 // Now we'll add the video's meta data to our friends
178 friends.addVideoToFriends(remoteVideo)
179
180 return callback(null, t)
181 })
182 }
183
184 ], function andFinally (err, t) {
185 if (err) {
186 logger.error('Cannot insert the video.')
187
188 // Abort transaction?
189 if (t) t.rollback()
190
191 return next(err)
192 }
193
194 // Commit transaction
195 t.commit()
196
197 // TODO : include Location of the new video -> 201
198 return res.type('json').status(204).end()
199 })
200 }
201
202 function updateVideo (req, res, next) {
203 let videoInstance = res.locals.video
204 const videoInfosToUpdate = req.body
205
206 waterfall([
207
208 function startTransaction (callback) {
209 db.sequelize.transaction().asCallback(function (err, t) {
210 return callback(err, t)
211 })
212 },
213
214 function findOrCreateTags (t, callback) {
215 if (videoInfosToUpdate.tags) {
216 db.Tag.findOrCreateTags(videoInfosToUpdate.tags, t, function (err, tagInstances) {
217 return callback(err, t, tagInstances)
218 })
219 } else {
220 return callback(null, t, null)
221 }
222 },
223
224 function updateVideoIntoDB (t, tagInstances, callback) {
225 const options = { transaction: t }
226
227 if (videoInfosToUpdate.name) videoInstance.set('name', videoInfosToUpdate.name)
228 if (videoInfosToUpdate.description) videoInstance.set('description', videoInfosToUpdate.description)
229
230 // Add tags association
231 videoInstance.save(options).asCallback(function (err) {
232 return callback(err, t, tagInstances)
233 })
234 },
235
236 function associateTagsToVideo (t, tagInstances, callback) {
237 if (tagInstances) {
238 const options = { transaction: t }
239
240 videoInstance.setTags(tagInstances, options).asCallback(function (err) {
241 videoInstance.Tags = tagInstances
242
243 return callback(err, t)
244 })
245 } else {
246 return callback(null, t)
247 }
248 },
249
250 function sendToFriends (t, callback) {
251 const json = videoInstance.toUpdateRemoteJSON()
252
253 // Now we'll update the video's meta data to our friends
254 friends.updateVideoToFriends(json)
255
256 return callback(null, t)
257 }
258
259 ], function andFinally (err, t) {
260 if (err) {
261 logger.error('Cannot insert the video.')
262
263 // Abort transaction?
264 if (t) t.rollback()
265
266 return next(err)
267 }
268
269 // Commit transaction
270 t.commit()
271
272 // TODO : include Location of the new video -> 201
273 return res.type('json').status(204).end()
274 })
275 }
276
277 function getVideo (req, res, next) {
278 db.Video.loadAndPopulateAuthorAndPodAndTags(req.params.id, function (err, video) {
279 if (err) return next(err)
280
281 if (!video) {
282 return res.type('json').status(204).end()
283 }
284
285 res.json(video.toFormatedJSON())
286 })
287 }
288
289 function listVideos (req, res, next) {
290 db.Video.listForApi(req.query.start, req.query.count, req.query.sort, function (err, videosList, videosTotal) {
291 if (err) return next(err)
292
293 res.json(getFormatedVideos(videosList, videosTotal))
294 })
295 }
296
297 function removeVideo (req, res, next) {
298 const videoId = req.params.id
299
300 waterfall([
301 function loadVideo (callback) {
302 db.Video.load(videoId, function (err, video) {
303 return callback(err, video)
304 })
305 },
306
307 function deleteVideo (video, callback) {
308 // Informations to other pods will be sent by the afterDestroy video hook
309 video.destroy().asCallback(callback)
310 }
311 ], function andFinally (err) {
312 if (err) {
313 logger.error('Errors when removed the video.', { error: err })
314 return next(err)
315 }
316
317 return res.type('json').status(204).end()
318 })
319 }
320
321 function searchVideos (req, res, next) {
322 db.Video.searchAndPopulateAuthorAndPodAndTags(
323 req.params.value, req.query.field, req.query.start, req.query.count, req.query.sort,
324 function (err, videosList, videosTotal) {
325 if (err) return next(err)
326
327 res.json(getFormatedVideos(videosList, videosTotal))
328 }
329 )
330 }
331
332 // ---------------------------------------------------------------------------
333
334 function getFormatedVideos (videos, videosTotal) {
335 const formatedVideos = []
336
337 videos.forEach(function (video) {
338 formatedVideos.push(video.toFormatedJSON())
339 })
340
341 return {
342 total: videosTotal,
343 data: formatedVideos
344 }
345 }