]>
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 reqValidatorSort
= reqValidator
.sort
19 const reqValidatorVideos
= reqValidator
.videos
20 const sort
= middlewares
.sort
21 const utils
= require('../../../helpers/utils')
22 const Videos
= require('../../../models/videos') // model
23 const videos
= require('../../../lib/videos')
24 const webtorrent
= require('../../../lib/webtorrent')
26 const router
= express
.Router()
27 const uploads
= config
.get('storage.uploads')
29 // multer configuration
30 const storage
= multer
.diskStorage({
31 destination: function (req
, file
, cb
) {
35 filename: function (req
, file
, cb
) {
37 if (file
.mimetype
=== 'video/webm') extension
= 'webm'
38 else if (file
.mimetype
=== 'video/mp4') extension
= 'mp4'
39 else if (file
.mimetype
=== 'video/ogg') extension
= 'ogv'
40 utils
.generateRandomString(16, function (err
, randomString
) {
41 const fieldname
= err
? undefined : randomString
42 cb(null, fieldname
+ '.' + extension
)
47 const reqFiles
= multer({ storage: storage
}).fields([{ name: 'videofile', maxCount: 1 }])
48 const thumbnailsDir
= path
.join(__dirname
, '..', '..', '..', '..', config
.get('storage.thumbnails'))
51 reqValidatorPagination
.pagination
,
52 reqValidatorSort
.videosSort
,
54 pagination
.setPagination
,
60 reqValidatorVideos
.videosAdd
,
64 reqValidatorVideos
.videosGet
,
69 reqValidatorVideos
.videosRemove
,
72 router
.get('/search/:name',
73 reqValidatorVideos
.videosSearch
,
74 reqValidatorPagination
.pagination
,
75 reqValidatorSort
.videosSort
,
77 pagination
.setPagination
,
81 // ---------------------------------------------------------------------------
83 module
.exports
= router
85 // ---------------------------------------------------------------------------
87 function addVideo (req
, res
, next
) {
88 const videoFile
= req
.files
.videofile
[0]
89 const videoInfos
= req
.body
92 function seedTheVideo (callback
) {
93 videos
.seed(videoFile
.path
, callback
)
96 function createThumbnail (torrent
, callback
) {
97 videos
.createVideoThumbnail(videoFile
.path
, function (err
, thumbnailName
) {
99 // TODO: unseed the video
100 logger
.error('Cannot make a thumbnail of the video file.')
104 callback(null, torrent
, thumbnailName
)
108 function insertIntoDB (torrent
, thumbnailName
, callback
) {
110 name: videoInfos
.name
,
111 namePath: videoFile
.filename
,
112 description: videoInfos
.description
,
113 magnetUri: torrent
.magnetURI
,
114 author: res
.locals
.oauth
.token
.user
.username
,
115 duration: videoFile
.duration
,
116 thumbnail: thumbnailName
119 Videos
.add(videoData
, function (err
, insertedVideo
) {
121 // TODO unseed the video
122 // TODO remove thumbnail
123 logger
.error('Cannot insert this video in the database.')
127 return callback(null, torrent
, thumbnailName
, videoData
, insertedVideo
)
131 function getThumbnailBase64 (torrent
, thumbnailName
, videoData
, insertedVideo
, callback
) {
132 videoData
.createdDate
= insertedVideo
.createdDate
134 fs
.readFile(thumbnailsDir
+ thumbnailName
, function (err
, thumbnailData
) {
136 // TODO unseed the video
137 // TODO remove thumbnail
138 // TODO: remove video
139 logger
.error('Cannot read the thumbnail of the video')
143 return callback(null, videoData
, thumbnailData
)
147 function sendToFriends (videoData
, thumbnailData
, callback
) {
148 // Set the image in base64
149 videoData
.thumbnailBase64
= new Buffer(thumbnailData
).toString('base64')
151 // Now we'll add the video's meta data to our friends
152 friends
.addVideoToFriends(videoData
)
154 return callback(null)
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 getVideos (req
, res
, next
) {
169 Videos
.get(req
.params
.id
, function (err
, videoObj
) {
170 if (err
) return next(err
)
172 const state
= videos
.getVideoState(videoObj
)
173 if (state
.exist
=== false) {
174 return res
.type('json').status(204).end()
177 res
.json(getFormatedVideo(videoObj
))
181 function listVideos (req
, res
, next
) {
182 Videos
.list(req
.query
.start
, req
.query
.count
, req
.query
.sort
, function (err
, videosList
) {
183 if (err
) return next(err
)
185 res
.json(getFormatedVideos(videosList
))
189 function removeVideo (req
, res
, next
) {
190 const videoId
= req
.params
.id
193 function getVideo (callback
) {
194 Videos
.get(videoId
, callback
)
197 function removeVideoTorrent (video
, callback
) {
198 removeTorrent(video
.magnetUri
, function () {
199 return callback(null, video
)
203 function removeFromDB (video
, callback
) {
204 Videos
.removeOwned(req
.params
.id
, function (err
) {
205 if (err
) return callback(err
)
207 return callback(null, video
)
211 function removeVideoData (video
, callback
) {
212 videos
.removeVideosDataFromDisk([ video
], function (err
) {
213 if (err
) logger
.error('Cannot remove video data from disk.', { video: video
})
215 return callback(null, video
)
219 function sendInformationToFriends (video
, callback
) {
222 magnetUri: video
.magnetUri
225 friends
.removeVideoToFriends(params
)
227 return callback(null)
231 logger
.error('Errors when removed the video.', { error: err
})
235 return res
.type('json').status(204).end()
239 function searchVideos (req
, res
, next
) {
240 Videos
.search(req
.params
.name
, req
.query
.start
, req
.query
.count
, req
.query
.sort
, function (err
, videosList
) {
241 if (err
) return next(err
)
243 res
.json(getFormatedVideos(videosList
))
247 // ---------------------------------------------------------------------------
249 function getFormatedVideo (videoObj
) {
250 const formatedVideo
= {
253 description: videoObj
.description
,
254 podUrl: videoObj
.podUrl
.replace(/^https?:\/\//, ''),
255 isLocal: videos
.getVideoState(videoObj
).owned
,
256 magnetUri: videoObj
.magnetUri
,
257 author: videoObj
.author
,
258 duration: videoObj
.duration
,
259 thumbnailPath: constants
.THUMBNAILS_STATIC_PATH
+ '/' + videoObj
.thumbnail
,
260 createdDate: videoObj
.createdDate
266 function getFormatedVideos (videosObj
) {
267 const formatedVideos
= []
269 videosObj
.forEach(function (videoObj
) {
270 formatedVideos
.push(getFormatedVideo(videoObj
))
273 return formatedVideos
276 // Maybe the torrent is not seeded, but we catch the error to don't stop the removing process
277 function removeTorrent (magnetUri
, callback
) {
279 webtorrent
.remove(magnetUri
, callback
)
281 logger
.warn('Cannot remove the torrent from WebTorrent', { err: err
})
282 return callback(null)