]>
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 reqValidator
= middleware
.reqValidators
.videos
15 const utils
= require('../../../helpers/utils')
16 const Videos
= require('../../../models/videos') // model
17 const videos
= require('../../../lib/videos')
18 const webtorrent
= require('../../../lib/webtorrent')
20 const router
= express
.Router()
21 const uploads
= config
.get('storage.uploads')
23 // multer configuration
24 const storage
= multer
.diskStorage({
25 destination: function (req
, file
, cb
) {
29 filename: function (req
, file
, cb
) {
31 if (file
.mimetype
=== 'video/webm') extension
= 'webm'
32 else if (file
.mimetype
=== 'video/mp4') extension
= 'mp4'
33 else if (file
.mimetype
=== 'video/ogg') extension
= 'ogv'
34 utils
.generateRandomString(16, function (err
, randomString
) {
35 const fieldname
= err
? undefined : randomString
36 cb(null, fieldname
+ '.' + extension
)
41 const reqFiles
= multer({ storage: storage
}).fields([{ name: 'videofile', maxCount: 1 }])
42 const thumbnailsDir
= path
.join(__dirname
, '..', '..', '..', '..', config
.get('storage.thumbnails'))
44 router
.get('/', listVideos
)
45 router
.post('/', oAuth2
.authenticate
, reqFiles
, reqValidator
.videosAdd
, addVideo
)
46 router
.get('/:id', reqValidator
.videosGet
, getVideos
)
47 router
.delete('/:id', oAuth2
.authenticate
, reqValidator
.videosRemove
, removeVideo
)
48 router
.get('/search/:name', reqValidator
.videosSearch
, searchVideos
)
50 // ---------------------------------------------------------------------------
52 module
.exports
= router
54 // ---------------------------------------------------------------------------
56 function addVideo (req
, res
, next
) {
57 const videoFile
= req
.files
.videofile
[0]
58 const videoInfos
= req
.body
60 videos
.seed(videoFile
.path
, function (err
, torrent
) {
62 logger
.error('Cannot seed this video.')
66 videos
.getVideoDuration(videoFile
.path
, function (err
, duration
) {
68 // TODO: unseed the video
69 logger
.error('Cannot retrieve metadata of the file.')
73 videos
.getVideoThumbnail(videoFile
.path
, function (err
, thumbnailName
) {
75 // TODO: unseed the video
76 logger
.error('Cannot make a thumbnail of the video file.')
81 name: videoInfos
.name
,
82 namePath: videoFile
.filename
,
83 description: videoInfos
.description
,
84 magnetUri: torrent
.magnetURI
,
85 author: res
.locals
.oauth
.token
.user
.username
,
87 thumbnail: thumbnailName
90 Videos
.add(videoData
, function (err
) {
92 // TODO unseed the video
93 logger
.error('Cannot insert this video in the database.')
97 fs
.readFile(thumbnailsDir
+ thumbnailName
, function (err
, data
) {
99 // TODO: remove video?
100 logger
.error('Cannot read the thumbnail of the video')
104 // Set the image in base64
105 videoData
.thumbnailBase64
= new Buffer(data
).toString('base64')
106 // Now we'll add the video's meta data to our friends
107 friends
.addVideoToFriends(videoData
)
109 // TODO : include Location of the new video -> 201
110 res
.type('json').status(204).end()
118 function getVideos (req
, res
, next
) {
119 Videos
.get(req
.params
.id
, function (err
, videoObj
) {
120 if (err
) return next(err
)
122 const state
= videos
.getVideoState(videoObj
)
123 if (state
.exist
=== false) {
124 return res
.type('json').status(204).end()
127 res
.json(getFormatedVideo(videoObj
))
131 function listVideos (req
, res
, next
) {
132 Videos
.list(function (err
, videosList
) {
133 if (err
) return next(err
)
135 res
.json(getFormatedVideos(videosList
))
139 function removeVideo (req
, res
, next
) {
140 const videoId
= req
.params
.id
141 Videos
.get(videoId
, function (err
, video
) {
142 if (err
) return next(err
)
144 removeTorrent(video
.magnetUri
, function () {
145 Videos
.removeOwned(req
.params
.id
, function (err
) {
146 if (err
) return next(err
)
148 videos
.removeVideosDataFromDisk([ video
], function (err
) {
149 if (err
) logger
.error('Cannot remove video data from disk.', { video: video
})
153 magnetUri: video
.magnetUri
156 friends
.removeVideoToFriends(params
)
157 res
.type('json').status(204).end()
164 function searchVideos (req
, res
, next
) {
165 Videos
.search(req
.params
.name
, function (err
, videosList
) {
166 if (err
) return next(err
)
168 res
.json(getFormatedVideos(videosList
))
172 // ---------------------------------------------------------------------------
174 function getFormatedVideo (videoObj
) {
175 const formatedVideo
= {
178 description: videoObj
.description
,
179 podUrl: videoObj
.podUrl
,
180 isLocal: videos
.getVideoState(videoObj
).owned
,
181 magnetUri: videoObj
.magnetUri
,
182 author: videoObj
.author
,
183 duration: videoObj
.duration
,
184 thumbnailPath: constants
.THUMBNAILS_STATIC_PATH
+ '/' + videoObj
.thumbnail
190 function getFormatedVideos (videosObj
) {
191 const formatedVideos
= []
193 videosObj
.forEach(function (videoObj
) {
194 formatedVideos
.push(getFormatedVideo(videoObj
))
197 return formatedVideos
200 // Maybe the torrent is not seeded, but we catch the error to don't stop the removing process
201 function removeTorrent (magnetUri
, callback
) {
203 webtorrent
.remove(magnetUri
, callback
)
205 logger
.warn('Cannot remove the torrent from WebTorrent', { err: err
})
206 return callback(null)