]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/v1/videos.js
Remove the cache middleware
[github/Chocobozzz/PeerTube.git] / server / controllers / api / v1 / videos.js
1 'use strict'
2
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')
8
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')
19
20 const router = express.Router()
21 const uploads = config.get('storage.uploads')
22
23 // multer configuration
24 const storage = multer.diskStorage({
25 destination: function (req, file, cb) {
26 cb(null, uploads)
27 },
28
29 filename: function (req, file, cb) {
30 let extension = ''
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)
37 })
38 }
39 })
40
41 const reqFiles = multer({ storage: storage }).fields([{ name: 'videofile', maxCount: 1 }])
42 const thumbnailsDir = path.join(__dirname, '..', '..', '..', '..', config.get('storage.thumbnails'))
43
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)
49
50 // ---------------------------------------------------------------------------
51
52 module.exports = router
53
54 // ---------------------------------------------------------------------------
55
56 function addVideo (req, res, next) {
57 const videoFile = req.files.videofile[0]
58 const videoInfos = req.body
59
60 videos.seed(videoFile.path, function (err, torrent) {
61 if (err) {
62 logger.error('Cannot seed this video.')
63 return next(err)
64 }
65
66 videos.getVideoDuration(videoFile.path, function (err, duration) {
67 if (err) {
68 // TODO: unseed the video
69 logger.error('Cannot retrieve metadata of the file.')
70 return next(err)
71 }
72
73 videos.getVideoThumbnail(videoFile.path, function (err, thumbnailName) {
74 if (err) {
75 // TODO: unseed the video
76 logger.error('Cannot make a thumbnail of the video file.')
77 return next(err)
78 }
79
80 const videoData = {
81 name: videoInfos.name,
82 namePath: videoFile.filename,
83 description: videoInfos.description,
84 magnetUri: torrent.magnetURI,
85 author: res.locals.oauth.token.user.username,
86 duration: duration,
87 thumbnail: thumbnailName
88 }
89
90 Videos.add(videoData, function (err) {
91 if (err) {
92 // TODO unseed the video
93 logger.error('Cannot insert this video in the database.')
94 return next(err)
95 }
96
97 fs.readFile(thumbnailsDir + thumbnailName, function (err, data) {
98 if (err) {
99 // TODO: remove video?
100 logger.error('Cannot read the thumbnail of the video')
101 return next(err)
102 }
103
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)
108
109 // TODO : include Location of the new video -> 201
110 res.type('json').status(204).end()
111 })
112 })
113 })
114 })
115 })
116 }
117
118 function getVideos (req, res, next) {
119 Videos.get(req.params.id, function (err, videoObj) {
120 if (err) return next(err)
121
122 const state = videos.getVideoState(videoObj)
123 if (state.exist === false) {
124 return res.type('json').status(204).end()
125 }
126
127 res.json(getFormatedVideo(videoObj))
128 })
129 }
130
131 function listVideos (req, res, next) {
132 Videos.list(function (err, videosList) {
133 if (err) return next(err)
134
135 res.json(getFormatedVideos(videosList))
136 })
137 }
138
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)
143
144 removeTorrent(video.magnetUri, function () {
145 Videos.removeOwned(req.params.id, function (err) {
146 if (err) return next(err)
147
148 videos.removeVideosDataFromDisk([ video ], function (err) {
149 if (err) logger.error('Cannot remove video data from disk.', { video: video })
150
151 const params = {
152 name: video.name,
153 magnetUri: video.magnetUri
154 }
155
156 friends.removeVideoToFriends(params)
157 res.type('json').status(204).end()
158 })
159 })
160 })
161 })
162 }
163
164 function searchVideos (req, res, next) {
165 Videos.search(req.params.name, function (err, videosList) {
166 if (err) return next(err)
167
168 res.json(getFormatedVideos(videosList))
169 })
170 }
171
172 // ---------------------------------------------------------------------------
173
174 function getFormatedVideo (videoObj) {
175 const formatedVideo = {
176 id: videoObj._id,
177 name: videoObj.name,
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
185 }
186
187 return formatedVideo
188 }
189
190 function getFormatedVideos (videosObj) {
191 const formatedVideos = []
192
193 videosObj.forEach(function (videoObj) {
194 formatedVideos.push(getFormatedVideo(videoObj))
195 })
196
197 return formatedVideos
198 }
199
200 // Maybe the torrent is not seeded, but we catch the error to don't stop the removing process
201 function removeTorrent (magnetUri, callback) {
202 try {
203 webtorrent.remove(magnetUri, callback)
204 } catch (err) {
205 logger.warn('Cannot remove the torrent from WebTorrent', { err: err })
206 return callback(null)
207 }
208 }