aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/videos.js
diff options
context:
space:
mode:
Diffstat (limited to 'server/controllers/api/videos.js')
-rw-r--r--server/controllers/api/videos.js63
1 files changed, 41 insertions, 22 deletions
diff --git a/server/controllers/api/videos.js b/server/controllers/api/videos.js
index daf452573..a61f2b2c9 100644
--- a/server/controllers/api/videos.js
+++ b/server/controllers/api/videos.js
@@ -2,12 +2,12 @@
2 2
3const express = require('express') 3const express = require('express')
4const fs = require('fs') 4const fs = require('fs')
5const mongoose = require('mongoose')
6const multer = require('multer') 5const multer = require('multer')
7const path = require('path') 6const path = require('path')
8const waterfall = require('async/waterfall') 7const waterfall = require('async/waterfall')
9 8
10const constants = require('../../initializers/constants') 9const constants = require('../../initializers/constants')
10const db = require('../../initializers/database')
11const logger = require('../../helpers/logger') 11const logger = require('../../helpers/logger')
12const friends = require('../../lib/friends') 12const friends = require('../../lib/friends')
13const middlewares = require('../../middlewares') 13const middlewares = require('../../middlewares')
@@ -22,7 +22,6 @@ const sort = middlewares.sort
22const utils = require('../../helpers/utils') 22const utils = require('../../helpers/utils')
23 23
24const router = express.Router() 24const router = express.Router()
25const Video = mongoose.model('Video')
26 25
27// multer configuration 26// multer configuration
28const storage = multer.diskStorage({ 27const storage = multer.diskStorage({
@@ -87,40 +86,60 @@ function addVideo (req, res, next) {
87 const videoInfos = req.body 86 const videoInfos = req.body
88 87
89 waterfall([ 88 waterfall([
90 function createVideoObject (callback) {
91 const id = mongoose.Types.ObjectId()
92 89
90 function findOrCreateAuthor (callback) {
91 const username = res.locals.oauth.token.user.username
92
93 const query = {
94 where: {
95 name: username,
96 podId: null
97 },
98 defaults: {
99 name: username,
100 podId: null // null because it is OUR pod
101 }
102 }
103
104 db.Author.findOrCreate(query).asCallback(function (err, result) {
105 // [ instance, wasCreated ]
106 return callback(err, result[0])
107 })
108 },
109
110 function createVideoObject (author, callback) {
93 const videoData = { 111 const videoData = {
94 _id: id,
95 name: videoInfos.name, 112 name: videoInfos.name,
96 remoteId: null, 113 remoteId: null,
97 extname: path.extname(videoFile.filename), 114 extname: path.extname(videoFile.filename),
98 description: videoInfos.description, 115 description: videoInfos.description,
99 author: res.locals.oauth.token.user.username,
100 duration: videoFile.duration, 116 duration: videoFile.duration,
101 tags: videoInfos.tags 117 tags: videoInfos.tags,
118 authorId: author.id
102 } 119 }
103 120
104 const video = new Video(videoData) 121 const video = db.Video.build(videoData)
105 122
106 return callback(null, video) 123 return callback(null, author, video)
107 }, 124 },
108 125
109 // Set the videoname the same as the MongoDB id 126 // Set the videoname the same as the id
110 function renameVideoFile (video, callback) { 127 function renameVideoFile (author, video, callback) {
111 const videoDir = constants.CONFIG.STORAGE.VIDEOS_DIR 128 const videoDir = constants.CONFIG.STORAGE.VIDEOS_DIR
112 const source = path.join(videoDir, videoFile.filename) 129 const source = path.join(videoDir, videoFile.filename)
113 const destination = path.join(videoDir, video.getVideoFilename()) 130 const destination = path.join(videoDir, video.getVideoFilename())
114 131
115 fs.rename(source, destination, function (err) { 132 fs.rename(source, destination, function (err) {
116 return callback(err, video) 133 return callback(err, author, video)
117 }) 134 })
118 }, 135 },
119 136
120 function insertIntoDB (video, callback) { 137 function insertIntoDB (author, video, callback) {
121 video.save(function (err, video) { 138 video.save().asCallback(function (err, videoCreated) {
122 // Assert there are only one argument sent to the next function (video) 139 // Do not forget to add Author informations to the created video
123 return callback(err, video) 140 videoCreated.Author = author
141
142 return callback(err, videoCreated)
124 }) 143 })
125 }, 144 },
126 145
@@ -147,7 +166,7 @@ function addVideo (req, res, next) {
147} 166}
148 167
149function getVideo (req, res, next) { 168function getVideo (req, res, next) {
150 Video.load(req.params.id, function (err, video) { 169 db.Video.loadAndPopulateAuthorAndPod(req.params.id, function (err, video) {
151 if (err) return next(err) 170 if (err) return next(err)
152 171
153 if (!video) { 172 if (!video) {
@@ -159,7 +178,7 @@ function getVideo (req, res, next) {
159} 178}
160 179
161function listVideos (req, res, next) { 180function listVideos (req, res, next) {
162 Video.listForApi(req.query.start, req.query.count, req.query.sort, function (err, videosList, videosTotal) { 181 db.Video.listForApi(req.query.start, req.query.count, req.query.sort, function (err, videosList, videosTotal) {
163 if (err) return next(err) 182 if (err) return next(err)
164 183
165 res.json(getFormatedVideos(videosList, videosTotal)) 184 res.json(getFormatedVideos(videosList, videosTotal))
@@ -171,11 +190,11 @@ function removeVideo (req, res, next) {
171 190
172 waterfall([ 191 waterfall([
173 function getVideo (callback) { 192 function getVideo (callback) {
174 Video.load(videoId, callback) 193 db.Video.load(videoId, callback)
175 }, 194 },
176 195
177 function removeFromDB (video, callback) { 196 function removeFromDB (video, callback) {
178 video.remove(function (err) { 197 video.destroy().asCallback(function (err) {
179 if (err) return callback(err) 198 if (err) return callback(err)
180 199
181 return callback(null, video) 200 return callback(null, video)
@@ -185,7 +204,7 @@ function removeVideo (req, res, next) {
185 function sendInformationToFriends (video, callback) { 204 function sendInformationToFriends (video, callback) {
186 const params = { 205 const params = {
187 name: video.name, 206 name: video.name,
188 remoteId: video._id 207 remoteId: video.id
189 } 208 }
190 209
191 friends.removeVideoToFriends(params) 210 friends.removeVideoToFriends(params)
@@ -203,7 +222,7 @@ function removeVideo (req, res, next) {
203} 222}
204 223
205function searchVideos (req, res, next) { 224function searchVideos (req, res, next) {
206 Video.search(req.params.value, req.query.field, req.query.start, req.query.count, req.query.sort, 225 db.Video.searchAndPopulateAuthorAndPod(req.params.value, req.query.field, req.query.start, req.query.count, req.query.sort,
207 function (err, videosList, videosTotal) { 226 function (err, videosList, videosTotal) {
208 if (err) return next(err) 227 if (err) return next(err)
209 228