]>
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
87 videos
.seed(videoFile
.path
, callback
)
90 function seed (torrent
, callback
) {
91 videos
.getVideoDuration(videoFile
.path
, function (err
, duration
) {
93 // TODO: unseed the video
94 logger
.error('Cannot retrieve metadata of the file.')
98 callback(null, torrent
, duration
)
102 function createThumbnail (torrent
, duration
, callback
) {
103 videos
.createVideoThumbnail(videoFile
.path
, function (err
, thumbnailName
) {
105 // TODO: unseed the video
106 logger
.error('Cannot make a thumbnail of the video file.')
110 callback(null, torrent
, duration
, thumbnailName
)
114 function insertIntoDB (torrent
, duration
, thumbnailName
, callback
) {
116 name: videoInfos
.name
,
117 namePath: videoFile
.filename
,
118 description: videoInfos
.description
,
119 magnetUri: torrent
.magnetURI
,
120 author: res
.locals
.oauth
.token
.user
.username
,
122 thumbnail: thumbnailName
125 Videos
.add(videoData
, function (err
, insertedVideo
) {
127 // TODO unseed the video
128 // TODO remove thumbnail
129 logger
.error('Cannot insert this video in the database.')
133 return callback(null, torrent
, duration
, thumbnailName
, videoData
, insertedVideo
)
137 function getThumbnailBase64 (torrent
, duration
, thumbnailName
, videoData
, insertedVideo
, callback
) {
138 videoData
.createdDate
= insertedVideo
.createdDate
140 fs
.readFile(thumbnailsDir
+ thumbnailName
, function (err
, thumbnailData
) {
142 // TODO unseed the video
143 // TODO remove thumbnail
144 // TODO: remove video
145 logger
.error('Cannot read the thumbnail of the video')
149 return callback(null, videoData
, thumbnailData
)
153 function sendToFriends (videoData
, thumbnailData
, callback
) {
154 // Set the image in base64
155 videoData
.thumbnailBase64
= new Buffer(thumbnailData
).toString('base64')
157 // Now we'll add the video's meta data to our friends
158 friends
.addVideoToFriends(videoData
)
160 return callback(null)
165 logger
.error('Cannot insert the video.')
169 // TODO : include Location of the new video -> 201
170 return res
.type('json').status(204).end()
174 function getVideos (req
, res
, next
) {
175 Videos
.get(req
.params
.id
, function (err
, videoObj
) {
176 if (err
) return next(err
)
178 const state
= videos
.getVideoState(videoObj
)
179 if (state
.exist
=== false) {
180 return res
.type('json').status(204).end()
183 res
.json(getFormatedVideo(videoObj
))
187 function listVideos (req
, res
, next
) {
188 Videos
.list(req
.query
.start
, req
.query
.count
, function (err
, videosList
) {
189 if (err
) return next(err
)
191 res
.json(getFormatedVideos(videosList
))
195 function removeVideo (req
, res
, next
) {
196 const videoId
= req
.params
.id
199 function getVideo (callback
) {
200 Videos
.get(videoId
, callback
)
203 function removeVideoTorrent (video
, callback
) {
204 removeTorrent(video
.magnetUri
, function () {
205 return callback(null, video
)
209 function removeFromDB (video
, callback
) {
210 Videos
.removeOwned(req
.params
.id
, function (err
) {
211 if (err
) return callback(err
)
213 return callback(null, video
)
217 function removeVideoData (video
, callback
) {
218 videos
.removeVideosDataFromDisk([ video
], function (err
) {
219 if (err
) logger
.error('Cannot remove video data from disk.', { video: video
})
221 return callback(null, video
)
225 function sendInformationToFriends (video
, callback
) {
228 magnetUri: video
.magnetUri
231 friends
.removeVideoToFriends(params
)
233 return callback(null)
237 logger
.error('Errors when removed the video.', { error: err
})
241 return res
.type('json').status(204).end()
245 function searchVideos (req
, res
, next
) {
246 Videos
.search(req
.params
.name
, req
.query
.start
, req
.query
.count
, function (err
, videosList
) {
247 if (err
) return next(err
)
249 res
.json(getFormatedVideos(videosList
))
253 // ---------------------------------------------------------------------------
255 function getFormatedVideo (videoObj
) {
256 const formatedVideo
= {
259 description: videoObj
.description
,
260 podUrl: videoObj
.podUrl
,
261 isLocal: videos
.getVideoState(videoObj
).owned
,
262 magnetUri: videoObj
.magnetUri
,
263 author: videoObj
.author
,
264 duration: videoObj
.duration
,
265 thumbnailPath: constants
.THUMBNAILS_STATIC_PATH
+ '/' + videoObj
.thumbnail
,
266 createdDate: videoObj
.createdDate
272 function getFormatedVideos (videosObj
) {
273 const formatedVideos
= []
275 videosObj
.forEach(function (videoObj
) {
276 formatedVideos
.push(getFormatedVideo(videoObj
))
279 return formatedVideos
282 // Maybe the torrent is not seeded, but we catch the error to don't stop the removing process
283 function removeTorrent (magnetUri
, callback
) {
285 webtorrent
.remove(magnetUri
, callback
)
287 logger
.warn('Cannot remove the torrent from WebTorrent', { err: err
})
288 return callback(null)