]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/v1/videos.js
Server: put config in constants
[github/Chocobozzz/PeerTube.git] / server / controllers / api / v1 / videos.js
1 'use strict'
2
3 const express = require('express')
4 const mongoose = require('mongoose')
5 const multer = require('multer')
6 const waterfall = require('async/waterfall')
7
8 const constants = require('../../../initializers/constants')
9 const logger = require('../../../helpers/logger')
10 const friends = require('../../../lib/friends')
11 const middlewares = require('../../../middlewares')
12 const oAuth = middlewares.oauth
13 const pagination = middlewares.pagination
14 const validators = middlewares.validators
15 const validatorsPagination = validators.pagination
16 const validatorsSort = validators.sort
17 const validatorsVideos = validators.videos
18 const search = middlewares.search
19 const sort = middlewares.sort
20 const utils = require('../../../helpers/utils')
21
22 const router = express.Router()
23 const Video = mongoose.model('Video')
24
25 // multer configuration
26 const storage = multer.diskStorage({
27 destination: function (req, file, cb) {
28 cb(null, constants.CONFIG.STORAGE.UPLOAD_DIR)
29 },
30
31 filename: function (req, file, cb) {
32 let extension = ''
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'
36 utils.generateRandomString(16, function (err, randomString) {
37 const fieldname = err ? undefined : randomString
38 cb(null, fieldname + '.' + extension)
39 })
40 }
41 })
42
43 const reqFiles = multer({ storage: storage }).fields([{ name: 'videofile', maxCount: 1 }])
44
45 router.get('/',
46 validatorsPagination.pagination,
47 validatorsSort.videosSort,
48 sort.setVideosSort,
49 pagination.setPagination,
50 listVideos
51 )
52 router.post('/',
53 oAuth.authenticate,
54 reqFiles,
55 validatorsVideos.videosAdd,
56 addVideo
57 )
58 router.get('/:id',
59 validatorsVideos.videosGet,
60 getVideo
61 )
62 router.delete('/:id',
63 oAuth.authenticate,
64 validatorsVideos.videosRemove,
65 removeVideo
66 )
67 router.get('/search/:value',
68 validatorsVideos.videosSearch,
69 validatorsPagination.pagination,
70 validatorsSort.videosSort,
71 sort.setVideosSort,
72 pagination.setPagination,
73 search.setVideosSearch,
74 searchVideos
75 )
76
77 // ---------------------------------------------------------------------------
78
79 module.exports = router
80
81 // ---------------------------------------------------------------------------
82
83 function addVideo (req, res, next) {
84 const videoFile = req.files.videofile[0]
85 const videoInfos = req.body
86
87 waterfall([
88
89 function insertIntoDB (callback) {
90 const videoData = {
91 name: videoInfos.name,
92 filename: videoFile.filename,
93 description: videoInfos.description,
94 author: res.locals.oauth.token.user.username,
95 duration: videoFile.duration,
96 tags: videoInfos.tags
97 }
98
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)
103 })
104 },
105
106 function sendToFriends (video, callback) {
107 video.toRemoteJSON(function (err, remoteVideo) {
108 if (err) return callback(err)
109
110 // Now we'll add the video's meta data to our friends
111 friends.addVideoToFriends(remoteVideo)
112
113 return callback(null)
114 })
115 }
116
117 ], function andFinally (err) {
118 if (err) {
119 // TODO unseed the video
120 // TODO remove thumbnail
121 // TODO delete from DB
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()
128 })
129 }
130
131 function getVideo (req, res, next) {
132 Video.load(req.params.id, function (err, video) {
133 if (err) return next(err)
134
135 if (!video) {
136 return res.type('json').status(204).end()
137 }
138
139 res.json(video.toFormatedJSON())
140 })
141 }
142
143 function listVideos (req, res, next) {
144 Video.listForApi(req.query.start, req.query.count, req.query.sort, function (err, videosList, videosTotal) {
145 if (err) return next(err)
146
147 res.json(getFormatedVideos(videosList, videosTotal))
148 })
149 }
150
151 function removeVideo (req, res, next) {
152 const videoId = req.params.id
153
154 waterfall([
155 function getVideo (callback) {
156 Video.load(videoId, callback)
157 },
158
159 function removeFromDB (video, callback) {
160 video.remove(function (err) {
161 if (err) return callback(err)
162
163 return callback(null, video)
164 })
165 },
166
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 }
177 ], function andFinally (err) {
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()
184 })
185 }
186
187 function searchVideos (req, res, next) {
188 Video.search(req.params.value, req.query.field, req.query.start, req.query.count, req.query.sort,
189 function (err, videosList, videosTotal) {
190 if (err) return next(err)
191
192 res.json(getFormatedVideos(videosList, videosTotal))
193 })
194 }
195
196 // ---------------------------------------------------------------------------
197
198 function getFormatedVideos (videos, videosTotal) {
199 const formatedVideos = []
200
201 videos.forEach(function (video) {
202 formatedVideos.push(video.toFormatedJSON())
203 })
204
205 return {
206 total: videosTotal,
207 data: formatedVideos
208 }
209 }