]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/v1/videos.js
Server: udpate async to 2.0.0
[github/Chocobozzz/PeerTube.git] / server / controllers / api / v1 / videos.js
CommitLineData
9f10b292
C
1'use strict'
2
f0f5567b 3const config = require('config')
f0f5567b 4const express = require('express')
aaf61f38 5const mongoose = require('mongoose')
f0f5567b 6const multer = require('multer')
1a42c9e2 7const waterfall = require('async/waterfall')
f0f5567b
C
8
9const logger = require('../../../helpers/logger')
10const friends = require('../../../lib/friends')
b3b92647 11const middlewares = require('../../../middlewares')
69b0a27c 12const oAuth = middlewares.oauth
fbf1134e 13const pagination = middlewares.pagination
fc51fde0
C
14const validators = middlewares.validators
15const validatorsPagination = validators.pagination
16const validatorsSort = validators.sort
17const validatorsVideos = validators.videos
46246b5f 18const search = middlewares.search
a877d5ac 19const sort = middlewares.sort
cbe2f7c3 20const utils = require('../../../helpers/utils')
f0f5567b
C
21
22const router = express.Router()
23const uploads = config.get('storage.uploads')
aaf61f38 24const Video = mongoose.model('Video')
9f10b292
C
25
26// multer configuration
f0f5567b 27const storage = multer.diskStorage({
9f10b292
C
28 destination: function (req, file, cb) {
29 cb(null, uploads)
30 },
31
32 filename: function (req, file, cb) {
f0f5567b 33 let extension = ''
9f10b292
C
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'
bc503c2a
C
37 utils.generateRandomString(16, function (err, randomString) {
38 const fieldname = err ? undefined : randomString
9f10b292
C
39 cb(null, fieldname + '.' + extension)
40 })
41 }
42})
43
8c9c1942 44const reqFiles = multer({ storage: storage }).fields([{ name: 'videofile', maxCount: 1 }])
8c308c2b 45
fbf1134e 46router.get('/',
fc51fde0
C
47 validatorsPagination.pagination,
48 validatorsSort.videosSort,
a877d5ac 49 sort.setVideosSort,
fbf1134e
C
50 pagination.setPagination,
51 listVideos
52)
53router.post('/',
69b0a27c 54 oAuth.authenticate,
fbf1134e 55 reqFiles,
fc51fde0 56 validatorsVideos.videosAdd,
fbf1134e
C
57 addVideo
58)
59router.get('/:id',
fc51fde0 60 validatorsVideos.videosGet,
68ce3ae0 61 getVideo
fbf1134e
C
62)
63router.delete('/:id',
69b0a27c 64 oAuth.authenticate,
fc51fde0 65 validatorsVideos.videosRemove,
fbf1134e
C
66 removeVideo
67)
46246b5f 68router.get('/search/:value',
fc51fde0
C
69 validatorsVideos.videosSearch,
70 validatorsPagination.pagination,
71 validatorsSort.videosSort,
a877d5ac 72 sort.setVideosSort,
fbf1134e 73 pagination.setPagination,
46246b5f 74 search.setVideosSearch,
fbf1134e
C
75 searchVideos
76)
8c308c2b 77
9f10b292 78// ---------------------------------------------------------------------------
c45f7f84 79
9f10b292 80module.exports = router
c45f7f84 81
9f10b292 82// ---------------------------------------------------------------------------
c45f7f84 83
9f10b292 84function addVideo (req, res, next) {
bc503c2a
C
85 const videoFile = req.files.videofile[0]
86 const videoInfos = req.body
9f10b292 87
1a42c9e2 88 waterfall([
807df9e6 89
aaf61f38 90 function insertIntoDB (callback) {
807df9e6
C
91 const videoData = {
92 name: videoInfos.name,
5189d08a 93 filename: videoFile.filename,
807df9e6 94 description: videoInfos.description,
807df9e6 95 author: res.locals.oauth.token.user.username,
67100f1f 96 duration: videoFile.duration,
be587647 97 tags: videoInfos.tags
807df9e6
C
98 }
99
aaf61f38
C
100 const video = new Video(videoData)
101 video.save(function (err, video) {
102 // Assert there are only one argument sent to the next function (video)
103 return callback(err, video)
3a8a8b51 104 })
807df9e6
C
105 },
106
aaf61f38
C
107 function sendToFriends (video, callback) {
108 video.toRemoteJSON(function (err, remoteVideo) {
109 if (err) return callback(err)
807df9e6 110
528a9efa
C
111 // Now we'll add the video's meta data to our friends
112 friends.addVideoToFriends(remoteVideo)
807df9e6 113
528a9efa
C
114 return callback(null)
115 })
807df9e6
C
116 }
117
be587647 118 ], function andFinally (err) {
807df9e6 119 if (err) {
aaf61f38
C
120 // TODO unseed the video
121 // TODO remove thumbnail
122 // TODO delete from DB
807df9e6
C
123 logger.error('Cannot insert the video.')
124 return next(err)
125 }
126
127 // TODO : include Location of the new video -> 201
128 return res.type('json').status(204).end()
9f10b292
C
129 })
130}
8c308c2b 131
68ce3ae0 132function getVideo (req, res, next) {
aaf61f38 133 Video.load(req.params.id, function (err, video) {
9f10b292 134 if (err) return next(err)
8c308c2b 135
aaf61f38 136 if (!video) {
2df82d42 137 return res.type('json').status(204).end()
9f10b292 138 }
8c308c2b 139
aaf61f38 140 res.json(video.toFormatedJSON())
9f10b292
C
141 })
142}
8c308c2b 143
9f10b292 144function listVideos (req, res, next) {
aaf61f38 145 Video.list(req.query.start, req.query.count, req.query.sort, function (err, videosList, videosTotal) {
9f10b292 146 if (err) return next(err)
c45f7f84 147
aaf61f38 148 res.json(getFormatedVideos(videosList, videosTotal))
9f10b292
C
149 })
150}
c45f7f84 151
9f10b292 152function removeVideo (req, res, next) {
bc503c2a 153 const videoId = req.params.id
8c308c2b 154
1a42c9e2 155 waterfall([
807df9e6 156 function getVideo (callback) {
aaf61f38 157 Video.load(videoId, callback)
807df9e6
C
158 },
159
160 function removeFromDB (video, callback) {
aaf61f38 161 video.remove(function (err) {
807df9e6 162 if (err) return callback(err)
c173e565 163
807df9e6
C
164 return callback(null, video)
165 })
166 },
cbe2f7c3 167
807df9e6
C
168 function sendInformationToFriends (video, callback) {
169 const params = {
170 name: video.name,
171 magnetUri: video.magnetUri
172 }
173
174 friends.removeVideoToFriends(params)
175
176 return callback(null)
177 }
be587647 178 ], function andFinally (err) {
807df9e6
C
179 if (err) {
180 logger.error('Errors when removed the video.', { error: err })
181 return next(err)
182 }
183
184 return res.type('json').status(204).end()
9f10b292
C
185 })
186}
8c308c2b 187
9f10b292 188function searchVideos (req, res, next) {
aaf61f38
C
189 Video.search(req.params.value, req.query.field, req.query.start, req.query.count, req.query.sort,
190 function (err, videosList, videosTotal) {
9f10b292 191 if (err) return next(err)
8c308c2b 192
aaf61f38 193 res.json(getFormatedVideos(videosList, videosTotal))
9f10b292
C
194 })
195}
c173e565 196
9f10b292 197// ---------------------------------------------------------------------------
c173e565 198
aaf61f38 199function getFormatedVideos (videos, videosTotal) {
bc503c2a 200 const formatedVideos = []
2df82d42 201
aaf61f38
C
202 videos.forEach(function (video) {
203 formatedVideos.push(video.toFormatedJSON())
2df82d42
C
204 })
205
68ce3ae0 206 return {
aaf61f38 207 total: videosTotal,
68ce3ae0
C
208 data: formatedVideos
209 }
2df82d42 210}