]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/videos.js
3 const each
= require('async/each')
4 const express
= require('express')
5 const fs
= require('fs')
6 const multer
= require('multer')
7 const path
= require('path')
8 const waterfall
= require('async/waterfall')
10 const constants
= require('../../initializers/constants')
11 const db
= require('../../initializers/database')
12 const logger
= require('../../helpers/logger')
13 const friends
= require('../../lib/friends')
14 const middlewares
= require('../../middlewares')
15 const oAuth
= middlewares
.oauth
16 const pagination
= middlewares
.pagination
17 const validators
= middlewares
.validators
18 const validatorsPagination
= validators
.pagination
19 const validatorsSort
= validators
.sort
20 const validatorsVideos
= validators
.videos
21 const search
= middlewares
.search
22 const sort
= middlewares
.sort
23 const utils
= require('../../helpers/utils')
25 const router
= express
.Router()
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
91 function startTransaction (callback
) {
92 db
.sequelize
.transaction().asCallback(function (err
, t
) {
93 return callback(err
, t
)
97 function findOrCreateAuthor (t
, callback
) {
98 const user
= res
.locals
.oauth
.token
.User
108 podId: null, // null because it is OUR pod
114 db
.Author
.findOrCreate(query
).asCallback(function (err
, result
) {
115 const authorInstance
= result
[0]
117 return callback(err
, t
, authorInstance
)
121 function findOrCreateTags (t
, author
, callback
) {
122 const tags
= videoInfos
.tags
123 const tagInstances
= []
125 each(tags
, function (tag
, callbackEach
) {
136 db
.Tag
.findOrCreate(query
).asCallback(function (err
, res
) {
137 if (err
) return callbackEach(err
)
139 // res = [ tag, isCreated ]
141 tagInstances
.push(tag
)
142 return callbackEach()
145 return callback(err
, t
, author
, tagInstances
)
149 function createVideoObject (t
, author
, tagInstances
, callback
) {
151 name: videoInfos
.name
,
153 extname: path
.extname(videoFile
.filename
),
154 description: videoInfos
.description
,
155 duration: videoFile
.duration
,
159 const video
= db
.Video
.build(videoData
)
161 return callback(null, t
, author
, tagInstances
, video
)
164 // Set the videoname the same as the id
165 function renameVideoFile (t
, author
, tagInstances
, video
, callback
) {
166 const videoDir
= constants
.CONFIG
.STORAGE
.VIDEOS_DIR
167 const source
= path
.join(videoDir
, videoFile
.filename
)
168 const destination
= path
.join(videoDir
, video
.getVideoFilename())
170 fs
.rename(source
, destination
, function (err
) {
171 return callback(err
, t
, author
, tagInstances
, video
)
175 function insertVideoIntoDB (t
, author
, tagInstances
, video
, callback
) {
176 const options
= { transaction: t
}
178 // Add tags association
179 video
.save(options
).asCallback(function (err
, videoCreated
) {
180 if (err
) return callback(err
)
182 // Do not forget to add Author informations to the created video
183 videoCreated
.Author
= author
185 return callback(err
, t
, tagInstances
, videoCreated
)
189 function associateTagsToVideo (t
, tagInstances
, video
, callback
) {
190 const options
= { transaction: t
}
192 video
.setTags(tagInstances
, options
).asCallback(function (err
) {
193 video
.Tags
= tagInstances
195 return callback(err
, t
, video
)
199 function sendToFriends (t
, video
, callback
) {
200 video
.toRemoteJSON(function (err
, remoteVideo
) {
201 if (err
) return callback(err
)
203 // Now we'll add the video's meta data to our friends
204 friends
.addVideoToFriends(remoteVideo
)
206 return callback(null, t
)
210 ], function andFinally (err
, t
) {
212 logger
.error('Cannot insert the video.')
214 // Abort transaction?
220 // Commit transaction
223 // TODO : include Location of the new video -> 201
224 return res
.type('json').status(204).end()
228 function getVideo (req
, res
, next
) {
229 db
.Video
.loadAndPopulateAuthorAndPodAndTags(req
.params
.id
, function (err
, video
) {
230 if (err
) return next(err
)
233 return res
.type('json').status(204).end()
236 res
.json(video
.toFormatedJSON())
240 function listVideos (req
, res
, next
) {
241 db
.Video
.listForApi(req
.query
.start
, req
.query
.count
, req
.query
.sort
, function (err
, videosList
, videosTotal
) {
242 if (err
) return next(err
)
244 res
.json(getFormatedVideos(videosList
, videosTotal
))
248 function removeVideo (req
, res
, next
) {
249 const videoId
= req
.params
.id
252 function getVideo (callback
) {
253 db
.Video
.load(videoId
, callback
)
256 function removeFromDB (video
, callback
) {
257 video
.destroy().asCallback(function (err
) {
258 if (err
) return callback(err
)
260 return callback(null, video
)
264 function sendInformationToFriends (video
, callback
) {
270 friends
.removeVideoToFriends(params
)
272 return callback(null)
274 ], function andFinally (err
) {
276 logger
.error('Errors when removed the video.', { error: err
})
280 return res
.type('json').status(204).end()
284 function searchVideos (req
, res
, next
) {
285 db
.Video
.searchAndPopulateAuthorAndPodAndTags(
286 req
.params
.value
, req
.query
.field
, req
.query
.start
, req
.query
.count
, req
.query
.sort
,
287 function (err
, videosList
, videosTotal
) {
288 if (err
) return next(err
)
290 res
.json(getFormatedVideos(videosList
, videosTotal
))
295 // ---------------------------------------------------------------------------
297 function getFormatedVideos (videos
, videosTotal
) {
298 const formatedVideos
= []
300 videos
.forEach(function (video
) {
301 formatedVideos
.push(video
.toFormatedJSON())