aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2016-05-13 21:14:14 +0200
committerChocobozzz <florian.bigard@gmail.com>2016-05-13 21:14:14 +0200
commit807df9e6682d1995d3d46e2461ca1b7b47eaf9d5 (patch)
tree1165a512e796956234b0f16b89e8ba7231964d8e /server/controllers
parent57a56079fe927f99592a3defa2ba17586a6fe14c (diff)
downloadPeerTube-807df9e6682d1995d3d46e2461ca1b7b47eaf9d5.tar.gz
PeerTube-807df9e6682d1995d3d46e2461ca1b7b47eaf9d5.tar.zst
PeerTube-807df9e6682d1995d3d46e2461ca1b7b47eaf9d5.zip
Use async waterfall in videos controller for better readability
Diffstat (limited to 'server/controllers')
-rw-r--r--server/controllers/api/v1/videos.js175
1 files changed, 115 insertions, 60 deletions
diff --git a/server/controllers/api/v1/videos.js b/server/controllers/api/v1/videos.js
index 641fef6ea..231309c5e 100644
--- a/server/controllers/api/v1/videos.js
+++ b/server/controllers/api/v1/videos.js
@@ -1,5 +1,6 @@
1'use strict' 1'use strict'
2 2
3const async = require('async')
3const config = require('config') 4const config = require('config')
4const express = require('express') 5const express = require('express')
5const fs = require('fs') 6const fs = require('fs')
@@ -81,63 +82,92 @@ function addVideo (req, res, next) {
81 const videoFile = req.files.videofile[0] 82 const videoFile = req.files.videofile[0]
82 const videoInfos = req.body 83 const videoInfos = req.body
83 84
84 videos.seed(videoFile.path, function (err, torrent) { 85 async.waterfall([
85 if (err) { 86 function (callback) {
86 logger.error('Cannot seed this video.') 87 videos.seed(videoFile.path, callback)
87 return next(err) 88 },
88 }
89 89
90 videos.getVideoDuration(videoFile.path, function (err, duration) { 90 function seed (torrent, callback) {
91 if (err) { 91 videos.getVideoDuration(videoFile.path, function (err, duration) {
92 // TODO: unseed the video 92 if (err) {
93 logger.error('Cannot retrieve metadata of the file.') 93 // TODO: unseed the video
94 return next(err) 94 logger.error('Cannot retrieve metadata of the file.')
95 } 95 return next(err)
96 }
97
98 callback(null, torrent, duration)
99 })
100 },
96 101
102 function createThumbnail (torrent, duration, callback) {
97 videos.createVideoThumbnail(videoFile.path, function (err, thumbnailName) { 103 videos.createVideoThumbnail(videoFile.path, function (err, thumbnailName) {
98 if (err) { 104 if (err) {
99 // TODO: unseed the video 105 // TODO: unseed the video
100 logger.error('Cannot make a thumbnail of the video file.') 106 logger.error('Cannot make a thumbnail of the video file.')
101 return next(err) 107 return callback(err)
102 } 108 }
103 109
104 const videoData = { 110 callback(null, torrent, duration, thumbnailName)
105 name: videoInfos.name, 111 })
106 namePath: videoFile.filename, 112 },
107 description: videoInfos.description, 113
108 magnetUri: torrent.magnetURI, 114 function insertIntoDB (torrent, duration, thumbnailName, callback) {
109 author: res.locals.oauth.token.user.username, 115 const videoData = {
110 duration: duration, 116 name: videoInfos.name,
111 thumbnail: thumbnailName 117 namePath: videoFile.filename,
118 description: videoInfos.description,
119 magnetUri: torrent.magnetURI,
120 author: res.locals.oauth.token.user.username,
121 duration: duration,
122 thumbnail: thumbnailName
123 }
124
125 Videos.add(videoData, function (err, insertedVideo) {
126 if (err) {
127 // TODO unseed the video
128 // TODO remove thumbnail
129 logger.error('Cannot insert this video in the database.')
130 return callback(err)
112 } 131 }
113 132
114 Videos.add(videoData, function (err, insertedVideo) { 133 return callback(null, torrent, duration, thumbnailName, videoData, insertedVideo)
115 if (err) {
116 // TODO unseed the video
117 logger.error('Cannot insert this video in the database.')
118 return next(err)
119 }
120
121 videoData.createdDate = insertedVideo.createdDate
122
123 fs.readFile(thumbnailsDir + thumbnailName, function (err, data) {
124 if (err) {
125 // TODO: remove video?
126 logger.error('Cannot read the thumbnail of the video')
127 return next(err)
128 }
129
130 // Set the image in base64
131 videoData.thumbnailBase64 = new Buffer(data).toString('base64')
132 // Now we'll add the video's meta data to our friends
133 friends.addVideoToFriends(videoData)
134
135 // TODO : include Location of the new video -> 201
136 res.type('json').status(204).end()
137 })
138 })
139 }) 134 })
140 }) 135 },
136
137 function getThumbnailBase64 (torrent, duration, thumbnailName, videoData, insertedVideo, callback) {
138 videoData.createdDate = insertedVideo.createdDate
139
140 fs.readFile(thumbnailsDir + thumbnailName, function (err, thumbnailData) {
141 if (err) {
142 // TODO unseed the video
143 // TODO remove thumbnail
144 // TODO: remove video
145 logger.error('Cannot read the thumbnail of the video')
146 return callback(err)
147 }
148
149 return callback(null, videoData, thumbnailData)
150 })
151 },
152
153 function sendToFriends (videoData, thumbnailData, callback) {
154 // Set the image in base64
155 videoData.thumbnailBase64 = new Buffer(thumbnailData).toString('base64')
156
157 // Now we'll add the video's meta data to our friends
158 friends.addVideoToFriends(videoData)
159
160 return callback(null)
161 }
162
163 ], function (err) {
164 if (err) {
165 logger.error('Cannot insert the video.')
166 return next(err)
167 }
168
169 // TODO : include Location of the new video -> 201
170 return res.type('json').status(204).end()
141 }) 171 })
142} 172}
143 173
@@ -164,26 +194,51 @@ function listVideos (req, res, next) {
164 194
165function removeVideo (req, res, next) { 195function removeVideo (req, res, next) {
166 const videoId = req.params.id 196 const videoId = req.params.id
167 Videos.get(videoId, function (err, video) {
168 if (err) return next(err)
169 197
170 removeTorrent(video.magnetUri, function () { 198 async.waterfall([
199 function getVideo (callback) {
200 Videos.get(videoId, callback)
201 },
202
203 function removeVideoTorrent (video, callback) {
204 removeTorrent(video.magnetUri, function () {
205 return callback(null, video)
206 })
207 },
208
209 function removeFromDB (video, callback) {
171 Videos.removeOwned(req.params.id, function (err) { 210 Videos.removeOwned(req.params.id, function (err) {
172 if (err) return next(err) 211 if (err) return callback(err)
173 212
174 videos.removeVideosDataFromDisk([ video ], function (err) { 213 return callback(null, video)
175 if (err) logger.error('Cannot remove video data from disk.', { video: video }) 214 })
215 },
176 216
177 const params = { 217 function removeVideoData (video, callback) {
178 name: video.name, 218 videos.removeVideosDataFromDisk([ video ], function (err) {
179 magnetUri: video.magnetUri 219 if (err) logger.error('Cannot remove video data from disk.', { video: video })
180 }
181 220
182 friends.removeVideoToFriends(params) 221 return callback(null, video)
183 res.type('json').status(204).end()
184 })
185 }) 222 })
186 }) 223 },
224
225 function sendInformationToFriends (video, callback) {
226 const params = {
227 name: video.name,
228 magnetUri: video.magnetUri
229 }
230
231 friends.removeVideoToFriends(params)
232
233 return callback(null)
234 }
235 ], function (err) {
236 if (err) {
237 logger.error('Errors when removed the video.', { error: err })
238 return next(err)
239 }
240
241 return res.type('json').status(204).end()
187 }) 242 })
188} 243}
189 244