]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/v1/videos.js
3 const config
= require('config')
4 const express
= require('express')
5 const mongoose
= require('mongoose')
6 const multer
= require('multer')
7 const waterfall
= require('async/waterfall')
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')
22 const router
= express
.Router()
23 const uploads
= config
.get('storage.uploads')
24 const Video
= mongoose
.model('Video')
26 // multer configuration
27 const storage
= multer
.diskStorage({
28 destination: function (req
, file
, cb
) {
32 filename: function (req
, file
, cb
) {
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
)
44 const reqFiles
= multer({ storage: storage
}).fields([{ name: 'videofile', maxCount: 1 }])
47 validatorsPagination
.pagination
,
48 validatorsSort
.videosSort
,
50 pagination
.setPagination
,
56 validatorsVideos
.videosAdd
,
60 validatorsVideos
.videosGet
,
65 validatorsVideos
.videosRemove
,
68 router
.get('/search/:value',
69 validatorsVideos
.videosSearch
,
70 validatorsPagination
.pagination
,
71 validatorsSort
.videosSort
,
73 pagination
.setPagination
,
74 search
.setVideosSearch
,
78 // ---------------------------------------------------------------------------
80 module
.exports
= router
82 // ---------------------------------------------------------------------------
84 function addVideo (req
, res
, next
) {
85 const videoFile
= req
.files
.videofile
[0]
86 const videoInfos
= req
.body
90 function insertIntoDB (callback
) {
92 name: videoInfos
.name
,
93 filename: videoFile
.filename
,
94 description: videoInfos
.description
,
95 author: res
.locals
.oauth
.token
.user
.username
,
96 duration: videoFile
.duration
,
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
)
107 function sendToFriends (video
, callback
) {
108 video
.toRemoteJSON(function (err
, remoteVideo
) {
109 if (err
) return callback(err
)
111 // Now we'll add the video's meta data to our friends
112 friends
.addVideoToFriends(remoteVideo
)
114 return callback(null)
118 ], function andFinally (err
) {
120 // TODO unseed the video
121 // TODO remove thumbnail
122 // TODO delete from DB
123 logger
.error('Cannot insert the video.')
127 // TODO : include Location of the new video -> 201
128 return res
.type('json').status(204).end()
132 function getVideo (req
, res
, next
) {
133 Video
.load(req
.params
.id
, function (err
, video
) {
134 if (err
) return next(err
)
137 return res
.type('json').status(204).end()
140 res
.json(video
.toFormatedJSON())
144 function listVideos (req
, res
, next
) {
145 Video
.list(req
.query
.start
, req
.query
.count
, req
.query
.sort
, function (err
, videosList
, videosTotal
) {
146 if (err
) return next(err
)
148 res
.json(getFormatedVideos(videosList
, videosTotal
))
152 function removeVideo (req
, res
, next
) {
153 const videoId
= req
.params
.id
156 function getVideo (callback
) {
157 Video
.load(videoId
, callback
)
160 function removeFromDB (video
, callback
) {
161 video
.remove(function (err
) {
162 if (err
) return callback(err
)
164 return callback(null, video
)
168 function sendInformationToFriends (video
, callback
) {
171 magnetUri: video
.magnetUri
174 friends
.removeVideoToFriends(params
)
176 return callback(null)
178 ], function andFinally (err
) {
180 logger
.error('Errors when removed the video.', { error: err
})
184 return res
.type('json').status(204).end()
188 function searchVideos (req
, res
, next
) {
189 Video
.search(req
.params
.value
, req
.query
.field
, req
.query
.start
, req
.query
.count
, req
.query
.sort
,
190 function (err
, videosList
, videosTotal
) {
191 if (err
) return next(err
)
193 res
.json(getFormatedVideos(videosList
, videosTotal
))
197 // ---------------------------------------------------------------------------
199 function getFormatedVideos (videos
, videosTotal
) {
200 const formatedVideos
= []
202 videos
.forEach(function (video
) {
203 formatedVideos
.push(video
.toFormatedJSON())