]>
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 middleware
= require('../../../middlewares')
13 const oAuth2
= require('../../../middlewares/oauth2')
14 const cacheMiddleware
= middleware
.cache
15 const reqValidator
= middleware
.reqValidators
.videos
16 const utils
= require('../../../helpers/utils')
17 const Videos
= require('../../../models/videos') // model
18 const videos
= require('../../../lib/videos')
19 const webtorrent
= require('../../../lib/webtorrent')
21 const router
= express
.Router()
22 const uploads
= config
.get('storage.uploads')
24 // multer configuration
25 const storage
= multer
.diskStorage({
26 destination: function (req
, file
, cb
) {
30 filename: function (req
, file
, cb
) {
32 if (file
.mimetype
=== 'video/webm') extension
= 'webm'
33 else if (file
.mimetype
=== 'video/mp4') extension
= 'mp4'
34 else if (file
.mimetype
=== 'video/ogg') extension
= 'ogv'
35 utils
.generateRandomString(16, function (err
, randomString
) {
36 const fieldname
= err
? undefined : randomString
37 cb(null, fieldname
+ '.' + extension
)
42 const reqFiles
= multer({ storage: storage
}).fields([{ name: 'videofile', maxCount: 1 }])
43 const thumbnailsDir
= path
.join(__dirname
, '..', '..', '..', '..', config
.get('storage.thumbnails'))
45 router
.get('/', cacheMiddleware
.cache(false), listVideos
)
46 router
.post('/', oAuth2
.authenticate
, reqFiles
, reqValidator
.videosAdd
, cacheMiddleware
.cache(false), addVideo
)
47 router
.get('/:id', reqValidator
.videosGet
, cacheMiddleware
.cache(false), getVideos
)
48 router
.delete('/:id', oAuth2
.authenticate
, reqValidator
.videosRemove
, cacheMiddleware
.cache(false), removeVideo
)
49 router
.get('/search/:name', reqValidator
.videosSearch
, cacheMiddleware
.cache(false), searchVideos
)
51 // ---------------------------------------------------------------------------
53 module
.exports
= router
55 // ---------------------------------------------------------------------------
57 function addVideo (req
, res
, next
) {
58 const videoFile
= req
.files
.videofile
[0]
59 const videoInfos
= req
.body
61 videos
.seed(videoFile
.path
, function (err
, torrent
) {
63 logger
.error('Cannot seed this video.')
67 videos
.getVideoDuration(videoFile
.path
, function (err
, duration
) {
69 // TODO: unseed the video
70 logger
.error('Cannot retrieve metadata of the file.')
74 videos
.getVideoThumbnail(videoFile
.path
, function (err
, thumbnailName
) {
76 // TODO: unseed the video
77 logger
.error('Cannot make a thumbnail of the video file.')
82 name: videoInfos
.name
,
83 namePath: videoFile
.filename
,
84 description: videoInfos
.description
,
85 magnetUri: torrent
.magnetURI
,
86 author: res
.locals
.oauth
.token
.user
.username
,
88 thumbnail: thumbnailName
91 Videos
.add(videoData
, function (err
) {
93 // TODO unseed the video
94 logger
.error('Cannot insert this video in the database.')
98 fs
.readFile(thumbnailsDir
+ thumbnailName
, function (err
, data
) {
100 // TODO: remove video?
101 logger
.error('Cannot read the thumbnail of the video')
105 // Set the image in base64
106 videoData
.thumbnailBase64
= new Buffer(data
).toString('base64')
107 // Now we'll add the video's meta data to our friends
108 friends
.addVideoToFriends(videoData
)
110 // TODO : include Location of the new video -> 201
111 res
.type('json').status(204).end()
119 function getVideos (req
, res
, next
) {
120 Videos
.get(req
.params
.id
, function (err
, videoObj
) {
121 if (err
) return next(err
)
123 const state
= videos
.getVideoState(videoObj
)
124 if (state
.exist
=== false) {
125 return res
.type('json').status(204).end()
128 res
.json(getFormatedVideo(videoObj
))
132 function listVideos (req
, res
, next
) {
133 Videos
.list(function (err
, videosList
) {
134 if (err
) return next(err
)
136 res
.json(getFormatedVideos(videosList
))
140 function removeVideo (req
, res
, next
) {
141 const videoId
= req
.params
.id
142 Videos
.get(videoId
, function (err
, video
) {
143 if (err
) return next(err
)
145 removeTorrent(video
.magnetUri
, function () {
146 Videos
.removeOwned(req
.params
.id
, function (err
) {
147 if (err
) return next(err
)
149 videos
.removeVideosDataFromDisk([ video
], function (err
) {
150 if (err
) logger
.error('Cannot remove video data from disk.', { video: video
})
154 magnetUri: video
.magnetUri
157 friends
.removeVideoToFriends(params
)
158 res
.type('json').status(204).end()
165 function searchVideos (req
, res
, next
) {
166 Videos
.search(req
.params
.name
, function (err
, videosList
) {
167 if (err
) return next(err
)
169 res
.json(getFormatedVideos(videosList
))
173 // ---------------------------------------------------------------------------
175 function getFormatedVideo (videoObj
) {
176 const formatedVideo
= {
179 description: videoObj
.description
,
180 podUrl: videoObj
.podUrl
,
181 isLocal: videos
.getVideoState(videoObj
).owned
,
182 magnetUri: videoObj
.magnetUri
,
183 author: videoObj
.author
,
184 duration: videoObj
.duration
,
185 thumbnailPath: constants
.THUMBNAILS_STATIC_PATH
+ '/' + videoObj
.thumbnail
191 function getFormatedVideos (videosObj
) {
192 const formatedVideos
= []
194 videosObj
.forEach(function (videoObj
) {
195 formatedVideos
.push(getFormatedVideo(videoObj
))
198 return formatedVideos
201 // Maybe the torrent is not seeded, but we catch the error to don't stop the removing process
202 function removeTorrent (magnetUri
, callback
) {
204 webtorrent
.remove(magnetUri
, callback
)
206 logger
.warn('Cannot remove the torrent from WebTorrent', { err: err
})
207 return callback(null)