]>
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 multer
= require('multer')
6 const path
= require('path')
7 const waterfall
= require('async/waterfall')
9 const constants
= require('../../initializers/constants')
10 const db
= require('../../initializers/database')
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()
26 // multer configuration
27 const storage
= multer
.diskStorage({
28 destination: function (req
, file
, cb
) {
29 cb(null, constants
.CONFIG
.STORAGE
.VIDEOS_DIR
)
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 findOrCreateAuthor (callback
) {
91 const username
= res
.locals
.oauth
.token
.user
.username
100 podId: null // null because it is OUR pod
104 db
.Author
.findOrCreate(query
).asCallback(function (err
, result
) {
105 // [ instance, wasCreated ]
106 return callback(err
, result
[0])
110 function createVideoObject (author
, callback
) {
112 name: videoInfos
.name
,
114 extname: path
.extname(videoFile
.filename
),
115 description: videoInfos
.description
,
116 duration: videoFile
.duration
,
117 tags: videoInfos
.tags
,
121 const video
= db
.Video
.build(videoData
)
123 return callback(null, author
, video
)
126 // Set the videoname the same as the id
127 function renameVideoFile (author
, video
, callback
) {
128 const videoDir
= constants
.CONFIG
.STORAGE
.VIDEOS_DIR
129 const source
= path
.join(videoDir
, videoFile
.filename
)
130 const destination
= path
.join(videoDir
, video
.getVideoFilename())
132 fs
.rename(source
, destination
, function (err
) {
133 return callback(err
, author
, video
)
137 function insertIntoDB (author
, video
, callback
) {
138 video
.save().asCallback(function (err
, videoCreated
) {
139 // Do not forget to add Author informations to the created video
140 videoCreated
.Author
= author
142 return callback(err
, videoCreated
)
146 function sendToFriends (video
, callback
) {
147 video
.toRemoteJSON(function (err
, remoteVideo
) {
148 if (err
) return callback(err
)
150 // Now we'll add the video's meta data to our friends
151 friends
.addVideoToFriends(remoteVideo
)
153 return callback(null)
157 ], function andFinally (err
) {
159 logger
.error('Cannot insert the video.')
163 // TODO : include Location of the new video -> 201
164 return res
.type('json').status(204).end()
168 function getVideo (req
, res
, next
) {
169 db
.Video
.loadAndPopulateAuthorAndPod(req
.params
.id
, function (err
, video
) {
170 if (err
) return next(err
)
173 return res
.type('json').status(204).end()
176 res
.json(video
.toFormatedJSON())
180 function listVideos (req
, res
, next
) {
181 db
.Video
.listForApi(req
.query
.start
, req
.query
.count
, req
.query
.sort
, function (err
, videosList
, videosTotal
) {
182 if (err
) return next(err
)
184 res
.json(getFormatedVideos(videosList
, videosTotal
))
188 function removeVideo (req
, res
, next
) {
189 const videoId
= req
.params
.id
192 function getVideo (callback
) {
193 db
.Video
.load(videoId
, callback
)
196 function removeFromDB (video
, callback
) {
197 video
.destroy().asCallback(function (err
) {
198 if (err
) return callback(err
)
200 return callback(null, video
)
204 function sendInformationToFriends (video
, callback
) {
210 friends
.removeVideoToFriends(params
)
212 return callback(null)
214 ], function andFinally (err
) {
216 logger
.error('Errors when removed the video.', { error: err
})
220 return res
.type('json').status(204).end()
224 function searchVideos (req
, res
, next
) {
225 db
.Video
.searchAndPopulateAuthorAndPod(req
.params
.value
, req
.query
.field
, req
.query
.start
, req
.query
.count
, req
.query
.sort
,
226 function (err
, videosList
, videosTotal
) {
227 if (err
) return next(err
)
229 res
.json(getFormatedVideos(videosList
, videosTotal
))
233 // ---------------------------------------------------------------------------
235 function getFormatedVideos (videos
, videosTotal
) {
236 const formatedVideos
= []
238 videos
.forEach(function (video
) {
239 formatedVideos
.push(video
.toFormatedJSON())