]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/v1/videos.js
3 const express
= require('express')
4 const mongoose
= require('mongoose')
5 const multer
= require('multer')
6 const waterfall
= require('async/waterfall')
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')
22 const router
= express
.Router()
23 const Video
= mongoose
.model('Video')
25 // multer configuration
26 const storage
= multer
.diskStorage({
27 destination: function (req
, file
, cb
) {
28 cb(null, constants
.CONFIG
.STORAGE
.UPLOAD_DIR
)
31 filename: function (req
, file
, cb
) {
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
)
43 const reqFiles
= multer({ storage: storage
}).fields([{ name: 'videofile', maxCount: 1 }])
46 validatorsPagination
.pagination
,
47 validatorsSort
.videosSort
,
49 pagination
.setPagination
,
55 validatorsVideos
.videosAdd
,
59 validatorsVideos
.videosGet
,
64 validatorsVideos
.videosRemove
,
67 router
.get('/search/:value',
68 validatorsVideos
.videosSearch
,
69 validatorsPagination
.pagination
,
70 validatorsSort
.videosSort
,
72 pagination
.setPagination
,
73 search
.setVideosSearch
,
77 // ---------------------------------------------------------------------------
79 module
.exports
= router
81 // ---------------------------------------------------------------------------
83 function addVideo (req
, res
, next
) {
84 const videoFile
= req
.files
.videofile
[0]
85 const videoInfos
= req
.body
89 function insertIntoDB (callback
) {
91 name: videoInfos
.name
,
92 filename: videoFile
.filename
,
93 description: videoInfos
.description
,
94 author: res
.locals
.oauth
.token
.user
.username
,
95 duration: videoFile
.duration
,
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
)
106 function sendToFriends (video
, callback
) {
107 video
.toRemoteJSON(function (err
, remoteVideo
) {
108 if (err
) return callback(err
)
110 // Now we'll add the video's meta data to our friends
111 friends
.addVideoToFriends(remoteVideo
)
113 return callback(null)
117 ], function andFinally (err
) {
119 logger
.error('Cannot insert the video.')
123 // TODO : include Location of the new video -> 201
124 return res
.type('json').status(204).end()
128 function getVideo (req
, res
, next
) {
129 Video
.load(req
.params
.id
, function (err
, video
) {
130 if (err
) return next(err
)
133 return res
.type('json').status(204).end()
136 res
.json(video
.toFormatedJSON())
140 function listVideos (req
, res
, next
) {
141 Video
.listForApi(req
.query
.start
, req
.query
.count
, req
.query
.sort
, function (err
, videosList
, videosTotal
) {
142 if (err
) return next(err
)
144 res
.json(getFormatedVideos(videosList
, videosTotal
))
148 function removeVideo (req
, res
, next
) {
149 const videoId
= req
.params
.id
152 function getVideo (callback
) {
153 Video
.load(videoId
, callback
)
156 function removeFromDB (video
, callback
) {
157 video
.remove(function (err
) {
158 if (err
) return callback(err
)
160 return callback(null, video
)
164 function sendInformationToFriends (video
, callback
) {
167 magnetUri: video
.magnetUri
170 friends
.removeVideoToFriends(params
)
172 return callback(null)
174 ], function andFinally (err
) {
176 logger
.error('Errors when removed the video.', { error: err
})
180 return res
.type('json').status(204).end()
184 function searchVideos (req
, res
, next
) {
185 Video
.search(req
.params
.value
, req
.query
.field
, req
.query
.start
, req
.query
.count
, req
.query
.sort
,
186 function (err
, videosList
, videosTotal
) {
187 if (err
) return next(err
)
189 res
.json(getFormatedVideos(videosList
, videosTotal
))
193 // ---------------------------------------------------------------------------
195 function getFormatedVideos (videos
, videosTotal
) {
196 const formatedVideos
= []
198 videos
.forEach(function (video
) {
199 formatedVideos
.push(video
.toFormatedJSON())