aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/v1/videos.js
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2016-06-24 17:42:51 +0200
committerChocobozzz <florian.bigard@gmail.com>2016-06-24 17:42:51 +0200
commitaaf61f3810e6d57c5130af959bd2860df32775e7 (patch)
tree3871b20359aa56995a4b8974ae5cb6b911dd73f5 /server/controllers/api/v1/videos.js
parent07d932038745e773359fa87bac6be70523f593ee (diff)
downloadPeerTube-aaf61f3810e6d57c5130af959bd2860df32775e7.tar.gz
PeerTube-aaf61f3810e6d57c5130af959bd2860df32775e7.tar.zst
PeerTube-aaf61f3810e6d57c5130af959bd2860df32775e7.zip
Video model refractoring -> use mongoose api
Diffstat (limited to 'server/controllers/api/v1/videos.js')
-rw-r--r--server/controllers/api/v1/videos.js124
1 files changed, 27 insertions, 97 deletions
diff --git a/server/controllers/api/v1/videos.js b/server/controllers/api/v1/videos.js
index 2edb31122..83734b35e 100644
--- a/server/controllers/api/v1/videos.js
+++ b/server/controllers/api/v1/videos.js
@@ -3,9 +3,9 @@
3const async = require('async') 3const async = require('async')
4const config = require('config') 4const config = require('config')
5const express = require('express') 5const express = require('express')
6const mongoose = require('mongoose')
6const multer = require('multer') 7const multer = require('multer')
7 8
8const constants = require('../../../initializers/constants')
9const logger = require('../../../helpers/logger') 9const logger = require('../../../helpers/logger')
10const friends = require('../../../lib/friends') 10const friends = require('../../../lib/friends')
11const middlewares = require('../../../middlewares') 11const middlewares = require('../../../middlewares')
@@ -18,12 +18,10 @@ const reqValidatorVideos = reqValidator.videos
18const search = middlewares.search 18const search = middlewares.search
19const sort = middlewares.sort 19const sort = middlewares.sort
20const utils = require('../../../helpers/utils') 20const utils = require('../../../helpers/utils')
21const Videos = require('../../../models/videos') // model
22const videos = require('../../../lib/videos')
23const webtorrent = require('../../../lib/webtorrent')
24 21
25const router = express.Router() 22const router = express.Router()
26const uploads = config.get('storage.uploads') 23const uploads = config.get('storage.uploads')
24const Video = mongoose.model('Video')
27 25
28// multer configuration 26// multer configuration
29const storage = multer.diskStorage({ 27const storage = multer.diskStorage({
@@ -88,55 +86,27 @@ function addVideo (req, res, next) {
88 const videoInfos = req.body 86 const videoInfos = req.body
89 87
90 async.waterfall([ 88 async.waterfall([
91 function seedTheVideo (callback) {
92 videos.seed(videoFile.path, callback)
93 },
94
95 function createThumbnail (torrent, callback) {
96 videos.createVideoThumbnail(videoFile.path, function (err, thumbnailName) {
97 if (err) {
98 // TODO: unseed the video
99 logger.error('Cannot make a thumbnail of the video file.')
100 return callback(err)
101 }
102
103 callback(null, torrent, thumbnailName)
104 })
105 },
106 89
107 function insertIntoDB (torrent, thumbnailName, callback) { 90 function insertIntoDB (callback) {
108 const videoData = { 91 const videoData = {
109 name: videoInfos.name, 92 name: videoInfos.name,
110 namePath: videoFile.filename, 93 namePath: videoFile.filename,
111 description: videoInfos.description, 94 description: videoInfos.description,
112 magnetUri: torrent.magnetURI,
113 author: res.locals.oauth.token.user.username, 95 author: res.locals.oauth.token.user.username,
114 duration: videoFile.duration, 96 duration: videoFile.duration,
115 thumbnail: thumbnailName,
116 tags: videoInfos.tags 97 tags: videoInfos.tags
117 } 98 }
118 99
119 Videos.add(videoData, function (err, insertedVideo) { 100 const video = new Video(videoData)
120 if (err) { 101 video.save(function (err, video) {
121 // TODO unseed the video 102 // Assert there are only one argument sent to the next function (video)
122 // TODO remove thumbnail 103 return callback(err, video)
123 logger.error('Cannot insert this video in the database.')
124 return callback(err)
125 }
126
127 return callback(null, insertedVideo)
128 }) 104 })
129 }, 105 },
130 106
131 function sendToFriends (insertedVideo, callback) { 107 function sendToFriends (video, callback) {
132 videos.convertVideoToRemote(insertedVideo, function (err, remoteVideo) { 108 video.toRemoteJSON(function (err, remoteVideo) {
133 if (err) { 109 if (err) return callback(err)
134 // TODO unseed the video
135 // TODO remove thumbnail
136 // TODO delete from DB
137 logger.error('Cannot convert video to remote.')
138 return callback(err)
139 }
140 110
141 // Now we'll add the video's meta data to our friends 111 // Now we'll add the video's meta data to our friends
142 friends.addVideoToFriends(remoteVideo) 112 friends.addVideoToFriends(remoteVideo)
@@ -147,6 +117,9 @@ function addVideo (req, res, next) {
147 117
148 ], function andFinally (err) { 118 ], function andFinally (err) {
149 if (err) { 119 if (err) {
120 // TODO unseed the video
121 // TODO remove thumbnail
122 // TODO delete from DB
150 logger.error('Cannot insert the video.') 123 logger.error('Cannot insert the video.')
151 return next(err) 124 return next(err)
152 } 125 }
@@ -157,23 +130,22 @@ function addVideo (req, res, next) {
157} 130}
158 131
159function getVideo (req, res, next) { 132function getVideo (req, res, next) {
160 Videos.get(req.params.id, function (err, videoObj) { 133 Video.load(req.params.id, function (err, video) {
161 if (err) return next(err) 134 if (err) return next(err)
162 135
163 const state = videos.getVideoState(videoObj) 136 if (!video) {
164 if (state.exist === false) {
165 return res.type('json').status(204).end() 137 return res.type('json').status(204).end()
166 } 138 }
167 139
168 res.json(getFormatedVideo(videoObj)) 140 res.json(video.toFormatedJSON())
169 }) 141 })
170} 142}
171 143
172function listVideos (req, res, next) { 144function listVideos (req, res, next) {
173 Videos.list(req.query.start, req.query.count, req.query.sort, function (err, videosList, totalVideos) { 145 Video.list(req.query.start, req.query.count, req.query.sort, function (err, videosList, videosTotal) {
174 if (err) return next(err) 146 if (err) return next(err)
175 147
176 res.json(getFormatedVideos(videosList, totalVideos)) 148 res.json(getFormatedVideos(videosList, videosTotal))
177 }) 149 })
178} 150}
179 151
@@ -182,31 +154,17 @@ function removeVideo (req, res, next) {
182 154
183 async.waterfall([ 155 async.waterfall([
184 function getVideo (callback) { 156 function getVideo (callback) {
185 Videos.get(videoId, callback) 157 Video.load(videoId, callback)
186 },
187
188 function removeVideoTorrent (video, callback) {
189 removeTorrent(video.magnetUri, function () {
190 return callback(null, video)
191 })
192 }, 158 },
193 159
194 function removeFromDB (video, callback) { 160 function removeFromDB (video, callback) {
195 Videos.removeOwned(req.params.id, function (err) { 161 video.remove(function (err) {
196 if (err) return callback(err) 162 if (err) return callback(err)
197 163
198 return callback(null, video) 164 return callback(null, video)
199 }) 165 })
200 }, 166 },
201 167
202 function removeVideoData (video, callback) {
203 videos.removeVideosDataFromDisk([ video ], function (err) {
204 if (err) logger.error('Cannot remove video data from disk.', { video: video })
205
206 return callback(null, video)
207 })
208 },
209
210 function sendInformationToFriends (video, callback) { 168 function sendInformationToFriends (video, callback) {
211 const params = { 169 const params = {
212 name: video.name, 170 name: video.name,
@@ -228,53 +186,25 @@ function removeVideo (req, res, next) {
228} 186}
229 187
230function searchVideos (req, res, next) { 188function searchVideos (req, res, next) {
231 Videos.search(req.params.value, req.query.field, req.query.start, req.query.count, req.query.sort, 189 Video.search(req.params.value, req.query.field, req.query.start, req.query.count, req.query.sort,
232 function (err, videosList, totalVideos) { 190 function (err, videosList, videosTotal) {
233 if (err) return next(err) 191 if (err) return next(err)
234 192
235 res.json(getFormatedVideos(videosList, totalVideos)) 193 res.json(getFormatedVideos(videosList, videosTotal))
236 }) 194 })
237} 195}
238 196
239// --------------------------------------------------------------------------- 197// ---------------------------------------------------------------------------
240 198
241function getFormatedVideo (videoObj) { 199function getFormatedVideos (videos, videosTotal) {
242 const formatedVideo = {
243 id: videoObj._id,
244 name: videoObj.name,
245 description: videoObj.description,
246 podUrl: videoObj.podUrl.replace(/^https?:\/\//, ''),
247 isLocal: videos.getVideoState(videoObj).owned,
248 magnetUri: videoObj.magnetUri,
249 author: videoObj.author,
250 duration: videoObj.duration,
251 tags: videoObj.tags,
252 thumbnailPath: constants.THUMBNAILS_STATIC_PATH + '/' + videoObj.thumbnail,
253 createdDate: videoObj.createdDate
254 }
255
256 return formatedVideo
257}
258
259function getFormatedVideos (videosObj, totalVideos) {
260 const formatedVideos = [] 200 const formatedVideos = []
261 201
262 videosObj.forEach(function (videoObj) { 202 videos.forEach(function (video) {
263 formatedVideos.push(getFormatedVideo(videoObj)) 203 formatedVideos.push(video.toFormatedJSON())
264 }) 204 })
265 205
266 return { 206 return {
267 total: totalVideos, 207 total: videosTotal,
268 data: formatedVideos 208 data: formatedVideos
269 } 209 }
270} 210}
271
272// Maybe the torrent is not seeded, but we catch the error to don't stop the removing process
273function removeTorrent (magnetUri, callback) {
274 try {
275 webtorrent.remove(magnetUri, callback)
276 } catch (err) {
277 logger.warn('Cannot remove the torrent from WebTorrent', { err: err })
278 return callback(null)
279 }
280}