]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/v1/videos.js
3 const async
= require('async')
4 const config
= require('config')
5 const express
= require('express')
6 const fs
= require('fs')
7 const path
= require('path')
8 const multer
= require('multer')
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 oAuth2
= middlewares
.oauth2
15 const pagination
= middlewares
.pagination
16 const reqValidator
= middlewares
.reqValidators
17 const reqValidatorPagination
= reqValidator
.pagination
18 const reqValidatorVideos
= reqValidator
.videos
19 const utils
= require('../../../helpers/utils')
20 const Videos
= require('../../../models/videos') // model
21 const videos
= require('../../../lib/videos')
22 const webtorrent
= require('../../../lib/webtorrent')
24 const router
= express
.Router()
25 const uploads
= config
.get('storage.uploads')
27 // multer configuration
28 const storage
= multer
.diskStorage({
29 destination: function (req
, file
, cb
) {
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 }])
46 const thumbnailsDir
= path
.join(__dirname
, '..', '..', '..', '..', config
.get('storage.thumbnails'))
49 reqValidatorPagination
.pagination
,
50 pagination
.setPagination
,
56 reqValidatorVideos
.videosAdd
,
60 reqValidatorVideos
.videosGet
,
65 reqValidatorVideos
.videosRemove
,
68 router
.get('/search/:name',
69 reqValidatorVideos
.videosSearch
,
70 reqValidatorPagination
.pagination
,
71 pagination
.setPagination
,
75 // ---------------------------------------------------------------------------
77 module
.exports
= router
79 // ---------------------------------------------------------------------------
81 function addVideo (req
, res
, next
) {
82 const videoFile
= req
.files
.videofile
[0]
83 const videoInfos
= req
.body
86 function seedTheVideo (callback
) {
87 videos
.seed(videoFile
.path
, callback
)
90 function createThumbnail (torrent
, callback
) {
91 videos
.createVideoThumbnail(videoFile
.path
, function (err
, thumbnailName
) {
93 // TODO: unseed the video
94 logger
.error('Cannot make a thumbnail of the video file.')
98 callback(null, torrent
, thumbnailName
)
102 function insertIntoDB (torrent
, thumbnailName
, callback
) {
104 name: videoInfos
.name
,
105 namePath: videoFile
.filename
,
106 description: videoInfos
.description
,
107 magnetUri: torrent
.magnetURI
,
108 author: res
.locals
.oauth
.token
.user
.username
,
109 duration: videoFile
.duration
,
110 thumbnail: thumbnailName
113 Videos
.add(videoData
, function (err
, insertedVideo
) {
115 // TODO unseed the video
116 // TODO remove thumbnail
117 logger
.error('Cannot insert this video in the database.')
121 return callback(null, torrent
, thumbnailName
, videoData
, insertedVideo
)
125 function getThumbnailBase64 (torrent
, thumbnailName
, videoData
, insertedVideo
, callback
) {
126 videoData
.createdDate
= insertedVideo
.createdDate
128 fs
.readFile(thumbnailsDir
+ thumbnailName
, function (err
, thumbnailData
) {
130 // TODO unseed the video
131 // TODO remove thumbnail
132 // TODO: remove video
133 logger
.error('Cannot read the thumbnail of the video')
137 return callback(null, videoData
, thumbnailData
)
141 function sendToFriends (videoData
, thumbnailData
, callback
) {
142 // Set the image in base64
143 videoData
.thumbnailBase64
= new Buffer(thumbnailData
).toString('base64')
145 // Now we'll add the video's meta data to our friends
146 friends
.addVideoToFriends(videoData
)
148 return callback(null)
153 logger
.error('Cannot insert the video.')
157 // TODO : include Location of the new video -> 201
158 return res
.type('json').status(204).end()
162 function getVideos (req
, res
, next
) {
163 Videos
.get(req
.params
.id
, function (err
, videoObj
) {
164 if (err
) return next(err
)
166 const state
= videos
.getVideoState(videoObj
)
167 if (state
.exist
=== false) {
168 return res
.type('json').status(204).end()
171 res
.json(getFormatedVideo(videoObj
))
175 function listVideos (req
, res
, next
) {
176 Videos
.list(req
.query
.start
, req
.query
.count
, function (err
, videosList
) {
177 if (err
) return next(err
)
179 res
.json(getFormatedVideos(videosList
))
183 function removeVideo (req
, res
, next
) {
184 const videoId
= req
.params
.id
187 function getVideo (callback
) {
188 Videos
.get(videoId
, callback
)
191 function removeVideoTorrent (video
, callback
) {
192 removeTorrent(video
.magnetUri
, function () {
193 return callback(null, video
)
197 function removeFromDB (video
, callback
) {
198 Videos
.removeOwned(req
.params
.id
, function (err
) {
199 if (err
) return callback(err
)
201 return callback(null, video
)
205 function removeVideoData (video
, callback
) {
206 videos
.removeVideosDataFromDisk([ video
], function (err
) {
207 if (err
) logger
.error('Cannot remove video data from disk.', { video: video
})
209 return callback(null, video
)
213 function sendInformationToFriends (video
, callback
) {
216 magnetUri: video
.magnetUri
219 friends
.removeVideoToFriends(params
)
221 return callback(null)
225 logger
.error('Errors when removed the video.', { error: err
})
229 return res
.type('json').status(204).end()
233 function searchVideos (req
, res
, next
) {
234 Videos
.search(req
.params
.name
, req
.query
.start
, req
.query
.count
, function (err
, videosList
) {
235 if (err
) return next(err
)
237 res
.json(getFormatedVideos(videosList
))
241 // ---------------------------------------------------------------------------
243 function getFormatedVideo (videoObj
) {
244 const formatedVideo
= {
247 description: videoObj
.description
,
248 podUrl: videoObj
.podUrl
,
249 isLocal: videos
.getVideoState(videoObj
).owned
,
250 magnetUri: videoObj
.magnetUri
,
251 author: videoObj
.author
,
252 duration: videoObj
.duration
,
253 thumbnailPath: constants
.THUMBNAILS_STATIC_PATH
+ '/' + videoObj
.thumbnail
,
254 createdDate: videoObj
.createdDate
260 function getFormatedVideos (videosObj
) {
261 const formatedVideos
= []
263 videosObj
.forEach(function (videoObj
) {
264 formatedVideos
.push(getFormatedVideo(videoObj
))
267 return formatedVideos
270 // Maybe the torrent is not seeded, but we catch the error to don't stop the removing process
271 function removeTorrent (magnetUri
, callback
) {
273 webtorrent
.remove(magnetUri
, callback
)
275 logger
.warn('Cannot remove the torrent from WebTorrent', { err: err
})
276 return callback(null)