]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/v1/videos.js
3 const config
= require('config')
4 const express
= require('express')
5 const fs
= require('fs')
6 const path
= require('path')
7 const multer
= require('multer')
9 const constants
= require('../../../initializers/constants')
10 const logger
= require('../../../helpers/logger')
11 const friends
= require('../../../lib/friends')
12 const middlewares
= require('../../../middlewares')
13 const oAuth2
= middlewares
.oauth2
14 const pagination
= middlewares
.pagination
15 const reqValidator
= middlewares
.reqValidators
16 const reqValidatorPagination
= reqValidator
.pagination
17 const reqValidatorVideos
= reqValidator
.videos
18 const utils
= require('../../../helpers/utils')
19 const Videos
= require('../../../models/videos') // model
20 const videos
= require('../../../lib/videos')
21 const webtorrent
= require('../../../lib/webtorrent')
23 const router
= express
.Router()
24 const uploads
= config
.get('storage.uploads')
26 // multer configuration
27 const storage
= multer
.diskStorage({
28 destination: function (req
, file
, cb
) {
32 filename: function (req
, file
, cb
) {
34 if (file
.mimetype
=== 'video/webm') extension
= 'webm'
35 else if (file
.mimetype
=== 'video/mp4') extension
= 'mp4'
36 else if (file
.mimetype
=== 'video/ogg') extension
= 'ogv'
37 utils
.generateRandomString(16, function (err
, randomString
) {
38 const fieldname
= err
? undefined : randomString
39 cb(null, fieldname
+ '.' + extension
)
44 const reqFiles
= multer({ storage: storage
}).fields([{ name: 'videofile', maxCount: 1 }])
45 const thumbnailsDir
= path
.join(__dirname
, '..', '..', '..', '..', config
.get('storage.thumbnails'))
48 reqValidatorPagination
.pagination
,
49 pagination
.setPagination
,
55 reqValidatorVideos
.videosAdd
,
59 reqValidatorVideos
.videosGet
,
64 reqValidatorVideos
.videosRemove
,
67 router
.get('/search/:name',
68 reqValidatorVideos
.videosSearch
,
69 reqValidatorPagination
.pagination
,
70 pagination
.setPagination
,
74 // ---------------------------------------------------------------------------
76 module
.exports
= router
78 // ---------------------------------------------------------------------------
80 function addVideo (req
, res
, next
) {
81 const videoFile
= req
.files
.videofile
[0]
82 const videoInfos
= req
.body
84 videos
.seed(videoFile
.path
, function (err
, torrent
) {
86 logger
.error('Cannot seed this video.')
90 videos
.getVideoDuration(videoFile
.path
, function (err
, duration
) {
92 // TODO: unseed the video
93 logger
.error('Cannot retrieve metadata of the file.')
97 videos
.getVideoThumbnail(videoFile
.path
, function (err
, thumbnailName
) {
99 // TODO: unseed the video
100 logger
.error('Cannot make a thumbnail of the video file.')
105 name: videoInfos
.name
,
106 namePath: videoFile
.filename
,
107 description: videoInfos
.description
,
108 magnetUri: torrent
.magnetURI
,
109 author: res
.locals
.oauth
.token
.user
.username
,
111 thumbnail: thumbnailName
114 Videos
.add(videoData
, function (err
) {
116 // TODO unseed the video
117 logger
.error('Cannot insert this video in the database.')
121 fs
.readFile(thumbnailsDir
+ thumbnailName
, function (err
, data
) {
123 // TODO: remove video?
124 logger
.error('Cannot read the thumbnail of the video')
128 // Set the image in base64
129 videoData
.thumbnailBase64
= new Buffer(data
).toString('base64')
130 // Now we'll add the video's meta data to our friends
131 friends
.addVideoToFriends(videoData
)
133 // TODO : include Location of the new video -> 201
134 res
.type('json').status(204).end()
142 function getVideos (req
, res
, next
) {
143 Videos
.get(req
.params
.id
, function (err
, videoObj
) {
144 if (err
) return next(err
)
146 const state
= videos
.getVideoState(videoObj
)
147 if (state
.exist
=== false) {
148 return res
.type('json').status(204).end()
151 res
.json(getFormatedVideo(videoObj
))
155 function listVideos (req
, res
, next
) {
156 Videos
.list(req
.query
.start
, req
.query
.count
, function (err
, videosList
) {
157 if (err
) return next(err
)
159 res
.json(getFormatedVideos(videosList
))
163 function removeVideo (req
, res
, next
) {
164 const videoId
= req
.params
.id
165 Videos
.get(videoId
, function (err
, video
) {
166 if (err
) return next(err
)
168 removeTorrent(video
.magnetUri
, function () {
169 Videos
.removeOwned(req
.params
.id
, function (err
) {
170 if (err
) return next(err
)
172 videos
.removeVideosDataFromDisk([ video
], function (err
) {
173 if (err
) logger
.error('Cannot remove video data from disk.', { video: video
})
177 magnetUri: video
.magnetUri
180 friends
.removeVideoToFriends(params
)
181 res
.type('json').status(204).end()
188 function searchVideos (req
, res
, next
) {
189 Videos
.search(req
.params
.name
, req
.query
.start
, req
.query
.count
, function (err
, videosList
) {
190 if (err
) return next(err
)
192 res
.json(getFormatedVideos(videosList
))
196 // ---------------------------------------------------------------------------
198 function getFormatedVideo (videoObj
) {
199 const formatedVideo
= {
202 description: videoObj
.description
,
203 podUrl: videoObj
.podUrl
,
204 isLocal: videos
.getVideoState(videoObj
).owned
,
205 magnetUri: videoObj
.magnetUri
,
206 author: videoObj
.author
,
207 duration: videoObj
.duration
,
208 thumbnailPath: constants
.THUMBNAILS_STATIC_PATH
+ '/' + videoObj
.thumbnail
214 function getFormatedVideos (videosObj
) {
215 const formatedVideos
= []
217 videosObj
.forEach(function (videoObj
) {
218 formatedVideos
.push(getFormatedVideo(videoObj
))
221 return formatedVideos
224 // Maybe the torrent is not seeded, but we catch the error to don't stop the removing process
225 function removeTorrent (magnetUri
, callback
) {
227 webtorrent
.remove(magnetUri
, callback
)
229 logger
.warn('Cannot remove the torrent from WebTorrent', { err: err
})
230 return callback(null)