]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/v1/videos.js
3 const config
= require('config')
4 const crypto
= require('crypto')
5 const express
= require('express')
6 const multer
= require('multer')
8 const logger
= require('../../../helpers/logger')
9 const friends
= require('../../../lib/friends')
10 const middleware
= require('../../../middlewares')
11 const oAuth2
= require('../../../middlewares/oauth2')
12 const cacheMiddleware
= middleware
.cache
13 const reqValidator
= middleware
.reqValidators
.videos
14 const Videos
= require('../../../models/videos') // model
15 const videos
= require('../../../lib/videos')
16 const webtorrent
= require('../../../lib/webtorrent')
18 const router
= express
.Router()
19 const uploads
= config
.get('storage.uploads')
21 // multer configuration
22 const storage
= multer
.diskStorage({
23 destination: function (req
, file
, cb
) {
27 filename: function (req
, file
, cb
) {
29 if (file
.mimetype
=== 'video/webm') extension
= 'webm'
30 else if (file
.mimetype
=== 'video/mp4') extension
= 'mp4'
31 else if (file
.mimetype
=== 'video/ogg') extension
= 'ogv'
32 crypto
.pseudoRandomBytes(16, function (err
, raw
) {
33 const fieldname
= err
? undefined : raw
.toString('hex')
34 cb(null, fieldname
+ '.' + extension
)
39 const reqFiles
= multer({ storage: storage
}).fields([{ name: 'videofile', maxCount: 1 }])
41 router
.get('/', cacheMiddleware
.cache(false), listVideos
)
42 router
.post('/', oAuth2
.authenticate
, reqFiles
, reqValidator
.videosAdd
, cacheMiddleware
.cache(false), addVideo
)
43 router
.get('/:id', reqValidator
.videosGet
, cacheMiddleware
.cache(false), getVideos
)
44 router
.delete('/:id', oAuth2
.authenticate
, reqValidator
.videosRemove
, cacheMiddleware
.cache(false), removeVideo
)
45 router
.get('/search/:name', reqValidator
.videosSearch
, cacheMiddleware
.cache(false), searchVideos
)
47 // ---------------------------------------------------------------------------
49 module
.exports
= router
51 // ---------------------------------------------------------------------------
53 function addVideo (req
, res
, next
) {
54 const video_file
= req
.files
.videofile
[0]
55 const video_infos
= req
.body
57 videos
.seed(video_file
.path
, function (err
, torrent
) {
59 logger
.error('Cannot seed this video.')
63 videos
.getVideoDuration(video_file
.path
, function (err
, duration
) {
65 // TODO: unseed the video
66 logger
.error('Cannot retrieve metadata of the file')
71 name: video_infos
.name
,
72 namePath: video_file
.filename
,
73 description: video_infos
.description
,
74 magnetUri: torrent
.magnetURI
,
75 author: res
.locals
.oauth
.token
.user
.username
,
79 Videos
.add(video_data
, function (err
) {
81 // TODO unseed the video
82 logger
.error('Cannot insert this video in the database.')
86 // Now we'll add the video's meta data to our friends
87 friends
.addVideoToFriends(video_data
)
89 // TODO : include Location of the new video -> 201
90 res
.type('json').status(204).end()
96 function getVideos (req
, res
, next
) {
97 Videos
.get(req
.params
.id
, function (err
, video_obj
) {
98 if (err
) return next(err
)
100 const state
= videos
.getVideoState(video_obj
)
101 if (state
.exist
=== false) {
102 return res
.type('json').status(204).end()
105 res
.json(getFormatedVideo(video_obj
))
109 function listVideos (req
, res
, next
) {
110 Videos
.list(function (err
, videos_list
) {
111 if (err
) return next(err
)
113 res
.json(getFormatedVideos(videos_list
))
117 function removeVideo (req
, res
, next
) {
118 const video_id
= req
.params
.id
119 Videos
.get(video_id
, function (err
, video
) {
120 if (err
) return next(err
)
122 removeTorrent(video
.magnetUri
, function () {
123 Videos
.removeOwned(req
.params
.id
, function (err
) {
124 if (err
) return next(err
)
128 magnetUri: video
.magnetUri
131 friends
.removeVideoToFriends(params
)
132 res
.type('json').status(204).end()
138 function searchVideos (req
, res
, next
) {
139 Videos
.search(req
.params
.name
, function (err
, videos_list
) {
140 if (err
) return next(err
)
142 res
.json(getFormatedVideos(videos_list
))
146 // ---------------------------------------------------------------------------
148 function getFormatedVideo (video_obj
) {
149 const formated_video
= {
151 name: video_obj
.name
,
152 description: video_obj
.description
,
153 podUrl: video_obj
.podUrl
,
154 isLocal: videos
.getVideoState(video_obj
).owned
,
155 magnetUri: video_obj
.magnetUri
,
156 author: video_obj
.author
,
157 duration: video_obj
.duration
160 return formated_video
163 function getFormatedVideos (videos_obj
) {
164 const formated_videos
= []
166 videos_obj
.forEach(function (video_obj
) {
167 formated_videos
.push(getFormatedVideo(video_obj
))
170 return formated_videos
173 // Maybe the torrent is not seeded, but we catch the error to don't stop the removing process
174 function removeTorrent (magnetUri
, callback
) {
176 webtorrent
.remove(magnetUri
, callback
)
178 logger
.warn('Cannot remove the torrent from WebTorrent', { err: err
})
179 return callback(null)