]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/videos.js
3 const express
= require('express')
4 const fs
= require('fs')
5 const mongoose
= require('mongoose')
6 const multer
= require('multer')
7 const path
= require('path')
8 const waterfall
= require('async/waterfall')
10 const constants
= require('../../initializers/constants')
11 const logger
= require('../../helpers/logger')
12 const friends
= require('../../lib/friends')
13 const middlewares
= require('../../middlewares')
14 const oAuth
= middlewares
.oauth
15 const pagination
= middlewares
.pagination
16 const validators
= middlewares
.validators
17 const validatorsPagination
= validators
.pagination
18 const validatorsSort
= validators
.sort
19 const validatorsVideos
= validators
.videos
20 const search
= middlewares
.search
21 const sort
= middlewares
.sort
22 const utils
= require('../../helpers/utils')
24 const router
= express
.Router()
25 const Video
= mongoose
.model('Video')
27 // multer configuration
28 const storage
= multer
.diskStorage({
29 destination: function (req
, file
, cb
) {
30 cb(null, constants
.CONFIG
.STORAGE
.VIDEOS_DIR
)
33 filename: function (req
, file
, cb
) {
35 if (file
.mimetype
=== 'video/webm') extension
= 'webm'
36 else if (file
.mimetype
=== 'video/mp4') extension
= 'mp4'
37 else if (file
.mimetype
=== 'video/ogg') extension
= 'ogv'
38 utils
.generateRandomString(16, function (err
, randomString
) {
39 const fieldname
= err
? undefined : randomString
40 cb(null, fieldname
+ '.' + extension
)
45 const reqFiles
= multer({ storage: storage
}).fields([{ name: 'videofile', maxCount: 1 }])
48 validatorsPagination
.pagination
,
49 validatorsSort
.videosSort
,
51 pagination
.setPagination
,
57 validatorsVideos
.videosAdd
,
61 validatorsVideos
.videosGet
,
66 validatorsVideos
.videosRemove
,
69 router
.get('/search/:value',
70 validatorsVideos
.videosSearch
,
71 validatorsPagination
.pagination
,
72 validatorsSort
.videosSort
,
74 pagination
.setPagination
,
75 search
.setVideosSearch
,
79 // ---------------------------------------------------------------------------
81 module
.exports
= router
83 // ---------------------------------------------------------------------------
85 function addVideo (req
, res
, next
) {
86 const videoFile
= req
.files
.videofile
[0]
87 const videoInfos
= req
.body
90 function createVideoObject (callback
) {
91 const id
= mongoose
.Types
.ObjectId()
95 name: videoInfos
.name
,
97 extname: path
.extname(videoFile
.filename
),
98 description: videoInfos
.description
,
99 author: res
.locals
.oauth
.token
.user
.username
,
100 duration: videoFile
.duration
,
101 tags: videoInfos
.tags
104 const video
= new Video(videoData
)
106 return callback(null, video
)
109 // Set the videoname the same as the MongoDB id
110 function renameVideoFile (video
, callback
) {
111 const videoDir
= constants
.CONFIG
.STORAGE
.VIDEOS_DIR
112 const source
= path
.join(videoDir
, videoFile
.filename
)
113 const destination
= path
.join(videoDir
, video
.getFilename())
115 fs
.rename(source
, destination
, function (err
) {
116 return callback(err
, video
)
120 function insertIntoDB (video
, callback
) {
121 video
.save(function (err
, video
, videoFile
) {
122 // Assert there are only one argument sent to the next function (video)
123 return callback(err
, video
)
127 function sendToFriends (video
, callback
) {
128 video
.toRemoteJSON(function (err
, remoteVideo
) {
129 if (err
) return callback(err
)
131 // Now we'll add the video's meta data to our friends
132 friends
.addVideoToFriends(remoteVideo
)
134 return callback(null)
138 ], function andFinally (err
) {
140 logger
.error('Cannot insert the video.')
144 // TODO : include Location of the new video -> 201
145 return res
.type('json').status(204).end()
149 function getVideo (req
, res
, next
) {
150 Video
.load(req
.params
.id
, function (err
, video
) {
151 if (err
) return next(err
)
154 return res
.type('json').status(204).end()
157 res
.json(video
.toFormatedJSON())
161 function listVideos (req
, res
, next
) {
162 Video
.listForApi(req
.query
.start
, req
.query
.count
, req
.query
.sort
, function (err
, videosList
, videosTotal
) {
163 if (err
) return next(err
)
165 res
.json(getFormatedVideos(videosList
, videosTotal
))
169 function removeVideo (req
, res
, next
) {
170 const videoId
= req
.params
.id
173 function getVideo (callback
) {
174 Video
.load(videoId
, callback
)
177 function removeFromDB (video
, callback
) {
178 video
.remove(function (err
) {
179 if (err
) return callback(err
)
181 return callback(null, video
)
185 function sendInformationToFriends (video
, callback
) {
191 friends
.removeVideoToFriends(params
)
193 return callback(null)
195 ], function andFinally (err
) {
197 logger
.error('Errors when removed the video.', { error: err
})
201 return res
.type('json').status(204).end()
205 function searchVideos (req
, res
, next
) {
206 Video
.search(req
.params
.value
, req
.query
.field
, req
.query
.start
, req
.query
.count
, req
.query
.sort
,
207 function (err
, videosList
, videosTotal
) {
208 if (err
) return next(err
)
210 res
.json(getFormatedVideos(videosList
, videosTotal
))
214 // ---------------------------------------------------------------------------
216 function getFormatedVideos (videos
, videosTotal
) {
217 const formatedVideos
= []
219 videos
.forEach(function (video
) {
220 formatedVideos
.push(video
.toFormatedJSON())