]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/videos.js
992f03db0c3b864d15b58346314368fec68c2b15
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 username
= res
.locals
.oauth
.token
.user
.username
107 podId: null // null because it is OUR pod
112 db
.Author
.findOrCreate(query
).asCallback(function (err
, result
) {
113 // [ instance, wasCreated ]
114 return callback(err
, t
, result
[0])
118 function findOrCreateTags (t
, author
, callback
) {
119 const tags
= videoInfos
.tags
120 const tagInstances
= []
122 each(tags
, function (tag
, callbackEach
) {
133 db
.Tag
.findOrCreate(query
).asCallback(function (err
, res
) {
134 if (err
) return callbackEach(err
)
136 // res = [ tag, isCreated ]
138 tagInstances
.push(tag
)
139 return callbackEach()
142 return callback(err
, t
, author
, tagInstances
)
146 function createVideoObject (t
, author
, tagInstances
, callback
) {
148 name: videoInfos
.name
,
150 extname: path
.extname(videoFile
.filename
),
151 description: videoInfos
.description
,
152 duration: videoFile
.duration
,
156 const video
= db
.Video
.build(videoData
)
158 return callback(null, t
, author
, tagInstances
, video
)
161 // Set the videoname the same as the id
162 function renameVideoFile (t
, author
, tagInstances
, video
, callback
) {
163 const videoDir
= constants
.CONFIG
.STORAGE
.VIDEOS_DIR
164 const source
= path
.join(videoDir
, videoFile
.filename
)
165 const destination
= path
.join(videoDir
, video
.getVideoFilename())
167 fs
.rename(source
, destination
, function (err
) {
168 return callback(err
, t
, author
, tagInstances
, video
)
172 function insertVideoIntoDB (t
, author
, tagInstances
, video
, callback
) {
173 const options
= { transaction: t
}
175 // Add tags association
176 video
.save(options
).asCallback(function (err
, videoCreated
) {
177 if (err
) return callback(err
)
179 // Do not forget to add Author informations to the created video
180 videoCreated
.Author
= author
182 return callback(err
, t
, tagInstances
, videoCreated
)
186 function associateTagsToVideo (t
, tagInstances
, video
, callback
) {
187 const options
= { transaction: t
}
189 video
.setTags(tagInstances
, options
).asCallback(function (err
) {
190 video
.Tags
= tagInstances
192 return callback(err
, t
, video
)
196 function sendToFriends (t
, video
, callback
) {
197 video
.toRemoteJSON(function (err
, remoteVideo
) {
198 if (err
) return callback(err
)
200 // Now we'll add the video's meta data to our friends
201 friends
.addVideoToFriends(remoteVideo
)
203 return callback(null, t
)
207 ], function andFinally (err
, t
) {
209 logger
.error('Cannot insert the video.')
211 // Abort transaction?
217 // Commit transaction
220 // TODO : include Location of the new video -> 201
221 return res
.type('json').status(204).end()
225 function getVideo (req
, res
, next
) {
226 db
.Video
.loadAndPopulateAuthorAndPodAndTags(req
.params
.id
, function (err
, video
) {
227 if (err
) return next(err
)
230 return res
.type('json').status(204).end()
233 res
.json(video
.toFormatedJSON())
237 function listVideos (req
, res
, next
) {
238 db
.Video
.listForApi(req
.query
.start
, req
.query
.count
, req
.query
.sort
, function (err
, videosList
, videosTotal
) {
239 if (err
) return next(err
)
241 res
.json(getFormatedVideos(videosList
, videosTotal
))
245 function removeVideo (req
, res
, next
) {
246 const videoId
= req
.params
.id
249 function getVideo (callback
) {
250 db
.Video
.load(videoId
, callback
)
253 function removeFromDB (video
, callback
) {
254 video
.destroy().asCallback(function (err
) {
255 if (err
) return callback(err
)
257 return callback(null, video
)
261 function sendInformationToFriends (video
, callback
) {
267 friends
.removeVideoToFriends(params
)
269 return callback(null)
271 ], function andFinally (err
) {
273 logger
.error('Errors when removed the video.', { error: err
})
277 return res
.type('json').status(204).end()
281 function searchVideos (req
, res
, next
) {
282 db
.Video
.searchAndPopulateAuthorAndPodAndTags(
283 req
.params
.value
, req
.query
.field
, req
.query
.start
, req
.query
.count
, req
.query
.sort
,
284 function (err
, videosList
, videosTotal
) {
285 if (err
) return next(err
)
287 res
.json(getFormatedVideos(videosList
, videosTotal
))
292 // ---------------------------------------------------------------------------
294 function getFormatedVideos (videos
, videosTotal
) {
295 const formatedVideos
= []
297 videos
.forEach(function (video
) {
298 formatedVideos
.push(video
.toFormatedJSON())