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