]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/v1/videos.js
5449cbcfa8da892c8a5378f50f34273ab5797dc9
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 search
= middlewares
.search
21 const sort
= middlewares
.sort
22 const utils
= require('../../../helpers/utils')
23 const Videos
= require('../../../models/videos') // model
24 const videos
= require('../../../lib/videos')
25 const webtorrent
= require('../../../lib/webtorrent')
27 const router
= express
.Router()
28 const uploads
= config
.get('storage.uploads')
30 // multer configuration
31 const storage
= multer
.diskStorage({
32 destination: function (req
, file
, cb
) {
36 filename: function (req
, file
, cb
) {
38 if (file
.mimetype
=== 'video/webm') extension
= 'webm'
39 else if (file
.mimetype
=== 'video/mp4') extension
= 'mp4'
40 else if (file
.mimetype
=== 'video/ogg') extension
= 'ogv'
41 utils
.generateRandomString(16, function (err
, randomString
) {
42 const fieldname
= err
? undefined : randomString
43 cb(null, fieldname
+ '.' + extension
)
48 const reqFiles
= multer({ storage: storage
}).fields([{ name: 'videofile', maxCount: 1 }])
49 const thumbnailsDir
= path
.join(__dirname
, '..', '..', '..', '..', config
.get('storage.thumbnails'))
52 reqValidatorPagination
.pagination
,
53 reqValidatorSort
.videosSort
,
55 pagination
.setPagination
,
61 reqValidatorVideos
.videosAdd
,
65 reqValidatorVideos
.videosGet
,
70 reqValidatorVideos
.videosRemove
,
73 router
.get('/search/:value',
74 reqValidatorVideos
.videosSearch
,
75 reqValidatorPagination
.pagination
,
76 reqValidatorSort
.videosSort
,
78 pagination
.setPagination
,
79 search
.setVideosSearch
,
83 // ---------------------------------------------------------------------------
85 module
.exports
= router
87 // ---------------------------------------------------------------------------
89 function addVideo (req
, res
, next
) {
90 const videoFile
= req
.files
.videofile
[0]
91 const videoInfos
= req
.body
94 function seedTheVideo (callback
) {
95 videos
.seed(videoFile
.path
, callback
)
98 function createThumbnail (torrent
, callback
) {
99 videos
.createVideoThumbnail(videoFile
.path
, function (err
, thumbnailName
) {
101 // TODO: unseed the video
102 logger
.error('Cannot make a thumbnail of the video file.')
106 callback(null, torrent
, thumbnailName
)
110 function insertIntoDB (torrent
, thumbnailName
, callback
) {
112 name: videoInfos
.name
,
113 namePath: videoFile
.filename
,
114 description: videoInfos
.description
,
115 magnetUri: torrent
.magnetURI
,
116 author: res
.locals
.oauth
.token
.user
.username
,
117 duration: videoFile
.duration
,
118 thumbnail: thumbnailName
,
119 tags: videoInfos
.tags
122 Videos
.add(videoData
, function (err
, insertedVideo
) {
124 // TODO unseed the video
125 // TODO remove thumbnail
126 logger
.error('Cannot insert this video in the database.')
130 return callback(null, torrent
, thumbnailName
, videoData
, insertedVideo
)
134 function getThumbnailBase64 (torrent
, thumbnailName
, videoData
, insertedVideo
, callback
) {
135 videoData
.createdDate
= insertedVideo
.createdDate
137 fs
.readFile(thumbnailsDir
+ thumbnailName
, function (err
, thumbnailData
) {
139 // TODO unseed the video
140 // TODO remove thumbnail
141 // TODO: remove video
142 logger
.error('Cannot read the thumbnail of the video')
146 return callback(null, videoData
, thumbnailData
)
150 function sendToFriends (videoData
, thumbnailData
, callback
) {
151 // Set the image in base64
152 videoData
.thumbnailBase64
= new Buffer(thumbnailData
).toString('base64')
154 // Now we'll add the video's meta data to our friends
155 friends
.addVideoToFriends(videoData
)
157 return callback(null)
160 ], function andFinally (err
) {
162 logger
.error('Cannot insert the video.')
166 // TODO : include Location of the new video -> 201
167 return res
.type('json').status(204).end()
171 function getVideo (req
, res
, next
) {
172 Videos
.get(req
.params
.id
, function (err
, videoObj
) {
173 if (err
) return next(err
)
175 const state
= videos
.getVideoState(videoObj
)
176 if (state
.exist
=== false) {
177 return res
.type('json').status(204).end()
180 res
.json(getFormatedVideo(videoObj
))
184 function listVideos (req
, res
, next
) {
185 Videos
.list(req
.query
.start
, req
.query
.count
, req
.query
.sort
, function (err
, videosList
, totalVideos
) {
186 if (err
) return next(err
)
188 res
.json(getFormatedVideos(videosList
, totalVideos
))
192 function removeVideo (req
, res
, next
) {
193 const videoId
= req
.params
.id
196 function getVideo (callback
) {
197 Videos
.get(videoId
, callback
)
200 function removeVideoTorrent (video
, callback
) {
201 removeTorrent(video
.magnetUri
, function () {
202 return callback(null, video
)
206 function removeFromDB (video
, callback
) {
207 Videos
.removeOwned(req
.params
.id
, function (err
) {
208 if (err
) return callback(err
)
210 return callback(null, video
)
214 function removeVideoData (video
, callback
) {
215 videos
.removeVideosDataFromDisk([ video
], function (err
) {
216 if (err
) logger
.error('Cannot remove video data from disk.', { video: video
})
218 return callback(null, video
)
222 function sendInformationToFriends (video
, callback
) {
225 magnetUri: video
.magnetUri
228 friends
.removeVideoToFriends(params
)
230 return callback(null)
232 ], function andFinally (err
) {
234 logger
.error('Errors when removed the video.', { error: err
})
238 return res
.type('json').status(204).end()
242 function searchVideos (req
, res
, next
) {
243 Videos
.search(req
.params
.value
, req
.query
.field
, req
.query
.start
, req
.query
.count
, req
.query
.sort
,
244 function (err
, videosList
, totalVideos
) {
245 if (err
) return next(err
)
247 res
.json(getFormatedVideos(videosList
, totalVideos
))
251 // ---------------------------------------------------------------------------
253 function getFormatedVideo (videoObj
) {
254 const formatedVideo
= {
257 description: videoObj
.description
,
258 podUrl: videoObj
.podUrl
.replace(/^https?:\/\//, ''),
259 isLocal: videos
.getVideoState(videoObj
).owned
,
260 magnetUri: videoObj
.magnetUri
,
261 author: videoObj
.author
,
262 duration: videoObj
.duration
,
264 thumbnailPath: constants
.THUMBNAILS_STATIC_PATH
+ '/' + videoObj
.thumbnail
,
265 createdDate: videoObj
.createdDate
271 function getFormatedVideos (videosObj
, totalVideos
) {
272 const formatedVideos
= []
274 videosObj
.forEach(function (videoObj
) {
275 formatedVideos
.push(getFormatedVideo(videoObj
))
284 // Maybe the torrent is not seeded, but we catch the error to don't stop the removing process
285 function removeTorrent (magnetUri
, callback
) {
287 webtorrent
.remove(magnetUri
, callback
)
289 logger
.warn('Cannot remove the torrent from WebTorrent', { err: err
})
290 return callback(null)