aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers
diff options
context:
space:
mode:
Diffstat (limited to 'server/controllers')
-rw-r--r--server/controllers/api/clients.js8
-rw-r--r--server/controllers/api/pods.js15
-rw-r--r--server/controllers/api/remote.js95
-rw-r--r--server/controllers/api/requests.js10
-rw-r--r--server/controllers/api/users.js28
-rw-r--r--server/controllers/api/videos.js63
-rw-r--r--server/controllers/client.js11
7 files changed, 153 insertions, 77 deletions
diff --git a/server/controllers/api/clients.js b/server/controllers/api/clients.js
index 7755f6c2b..cf83cb835 100644
--- a/server/controllers/api/clients.js
+++ b/server/controllers/api/clients.js
@@ -1,13 +1,11 @@
1'use strict' 1'use strict'
2 2
3const express = require('express') 3const express = require('express')
4const mongoose = require('mongoose')
5 4
6const constants = require('../../initializers/constants') 5const constants = require('../../initializers/constants')
6const db = require('../../initializers/database')
7const logger = require('../../helpers/logger') 7const logger = require('../../helpers/logger')
8 8
9const Client = mongoose.model('OAuthClient')
10
11const router = express.Router() 9const router = express.Router()
12 10
13router.get('/local', getLocalClient) 11router.get('/local', getLocalClient)
@@ -27,12 +25,12 @@ function getLocalClient (req, res, next) {
27 return res.type('json').status(403).end() 25 return res.type('json').status(403).end()
28 } 26 }
29 27
30 Client.loadFirstClient(function (err, client) { 28 db.OAuthClient.loadFirstClient(function (err, client) {
31 if (err) return next(err) 29 if (err) return next(err)
32 if (!client) return next(new Error('No client available.')) 30 if (!client) return next(new Error('No client available.'))
33 31
34 res.json({ 32 res.json({
35 client_id: client._id, 33 client_id: client.clientId,
36 client_secret: client.clientSecret 34 client_secret: client.clientSecret
37 }) 35 })
38 }) 36 })
diff --git a/server/controllers/api/pods.js b/server/controllers/api/pods.js
index 7857fcee0..79f3f9d8d 100644
--- a/server/controllers/api/pods.js
+++ b/server/controllers/api/pods.js
@@ -1,9 +1,9 @@
1'use strict' 1'use strict'
2 2
3const express = require('express') 3const express = require('express')
4const mongoose = require('mongoose')
5const waterfall = require('async/waterfall') 4const waterfall = require('async/waterfall')
6 5
6const db = require('../../initializers/database')
7const logger = require('../../helpers/logger') 7const logger = require('../../helpers/logger')
8const friends = require('../../lib/friends') 8const friends = require('../../lib/friends')
9const middlewares = require('../../middlewares') 9const middlewares = require('../../middlewares')
@@ -15,7 +15,6 @@ const validators = middlewares.validators.pods
15const signatureValidator = middlewares.validators.remote.signature 15const signatureValidator = middlewares.validators.remote.signature
16 16
17const router = express.Router() 17const router = express.Router()
18const Pod = mongoose.model('Pod')
19 18
20router.get('/', listPods) 19router.get('/', listPods)
21router.post('/', 20router.post('/',
@@ -53,15 +52,15 @@ function addPods (req, res, next) {
53 52
54 waterfall([ 53 waterfall([
55 function addPod (callback) { 54 function addPod (callback) {
56 const pod = new Pod(informations) 55 const pod = db.Pod.build(informations)
57 pod.save(function (err, podCreated) { 56 pod.save().asCallback(function (err, podCreated) {
58 // Be sure about the number of parameters for the callback 57 // Be sure about the number of parameters for the callback
59 return callback(err, podCreated) 58 return callback(err, podCreated)
60 }) 59 })
61 }, 60 },
62 61
63 function sendMyVideos (podCreated, callback) { 62 function sendMyVideos (podCreated, callback) {
64 friends.sendOwnedVideosToPod(podCreated._id) 63 friends.sendOwnedVideosToPod(podCreated.id)
65 64
66 callback(null) 65 callback(null)
67 }, 66 },
@@ -84,7 +83,7 @@ function addPods (req, res, next) {
84} 83}
85 84
86function listPods (req, res, next) { 85function listPods (req, res, next) {
87 Pod.list(function (err, podsList) { 86 db.Pod.list(function (err, podsList) {
88 if (err) return next(err) 87 if (err) return next(err)
89 88
90 res.json(getFormatedPods(podsList)) 89 res.json(getFormatedPods(podsList))
@@ -111,11 +110,11 @@ function removePods (req, res, next) {
111 110
112 waterfall([ 111 waterfall([
113 function loadPod (callback) { 112 function loadPod (callback) {
114 Pod.loadByHost(host, callback) 113 db.Pod.loadByHost(host, callback)
115 }, 114 },
116 115
117 function removePod (pod, callback) { 116 function removePod (pod, callback) {
118 pod.remove(callback) 117 pod.destroy().asCallback(callback)
119 } 118 }
120 ], function (err) { 119 ], function (err) {
121 if (err) return next(err) 120 if (err) return next(err)
diff --git a/server/controllers/api/remote.js b/server/controllers/api/remote.js
index f1046c534..d856576a9 100644
--- a/server/controllers/api/remote.js
+++ b/server/controllers/api/remote.js
@@ -3,15 +3,15 @@
3const each = require('async/each') 3const each = require('async/each')
4const eachSeries = require('async/eachSeries') 4const eachSeries = require('async/eachSeries')
5const express = require('express') 5const express = require('express')
6const mongoose = require('mongoose') 6const waterfall = require('async/waterfall')
7 7
8const db = require('../../initializers/database')
8const middlewares = require('../../middlewares') 9const middlewares = require('../../middlewares')
9const secureMiddleware = middlewares.secure 10const secureMiddleware = middlewares.secure
10const validators = middlewares.validators.remote 11const validators = middlewares.validators.remote
11const logger = require('../../helpers/logger') 12const logger = require('../../helpers/logger')
12 13
13const router = express.Router() 14const router = express.Router()
14const Video = mongoose.model('Video')
15 15
16router.post('/videos', 16router.post('/videos',
17 validators.signature, 17 validators.signature,
@@ -53,34 +53,99 @@ function remoteVideos (req, res, next) {
53function addRemoteVideo (videoToCreateData, fromHost, callback) { 53function addRemoteVideo (videoToCreateData, fromHost, callback) {
54 logger.debug('Adding remote video "%s".', videoToCreateData.name) 54 logger.debug('Adding remote video "%s".', videoToCreateData.name)
55 55
56 const video = new Video(videoToCreateData) 56 waterfall([
57 video.podHost = fromHost 57
58 Video.generateThumbnailFromBase64(video, videoToCreateData.thumbnailBase64, function (err) { 58 function findOrCreatePod (callback) {
59 if (err) { 59 fromHost
60 logger.error('Cannot generate thumbnail from base 64 data.', { error: err }) 60
61 return callback(err) 61 const query = {
62 where: {
63 host: fromHost
64 },
65 defaults: {
66 host: fromHost
67 }
68 }
69
70 db.Pod.findOrCreate(query).asCallback(function (err, result) {
71 // [ instance, wasCreated ]
72 return callback(err, result[0])
73 })
74 },
75
76 function findOrCreateAuthor (pod, callback) {
77 const username = videoToCreateData.author
78
79 const query = {
80 where: {
81 name: username,
82 podId: pod.id
83 },
84 defaults: {
85 name: username,
86 podId: pod.id
87 }
88 }
89
90 db.Author.findOrCreate(query).asCallback(function (err, result) {
91 // [ instance, wasCreated ]
92 return callback(err, result[0])
93 })
94 },
95
96 function createVideoObject (author, callback) {
97 const videoData = {
98 name: videoToCreateData.name,
99 remoteId: videoToCreateData.remoteId,
100 extname: videoToCreateData.extname,
101 infoHash: videoToCreateData.infoHash,
102 description: videoToCreateData.description,
103 authorId: author.id,
104 duration: videoToCreateData.duration,
105 tags: videoToCreateData.tags
106 }
107
108 const video = db.Video.build(videoData)
109
110 return callback(null, video)
111 },
112
113 function generateThumbnail (video, callback) {
114 db.Video.generateThumbnailFromBase64(video, videoToCreateData.thumbnailBase64, function (err) {
115 if (err) {
116 logger.error('Cannot generate thumbnail from base 64 data.', { error: err })
117 return callback(err)
118 }
119
120 video.save().asCallback(callback)
121 })
122 },
123
124 function insertIntoDB (video, callback) {
125 video.save().asCallback(callback)
62 } 126 }
63 127
64 video.save(callback) 128 ], callback)
65 })
66} 129}
67 130
68function removeRemoteVideo (videoToRemoveData, fromHost, callback) { 131function removeRemoteVideo (videoToRemoveData, fromHost, callback) {
132 // TODO: use bulkDestroy?
133
69 // We need the list because we have to remove some other stuffs (thumbnail etc) 134 // We need the list because we have to remove some other stuffs (thumbnail etc)
70 Video.listByHostAndRemoteId(fromHost, videoToRemoveData.remoteId, function (err, videosList) { 135 db.Video.listByHostAndRemoteId(fromHost, videoToRemoveData.remoteId, function (err, videosList) {
71 if (err) { 136 if (err) {
72 logger.error('Cannot list videos from host and magnets.', { error: err }) 137 logger.error('Cannot list videos from host and remote id.', { error: err.message })
73 return callback(err) 138 return callback(err)
74 } 139 }
75 140
76 if (videosList.length === 0) { 141 if (videosList.length === 0) {
77 logger.error('No remote video was found for this pod.', { magnetUri: videoToRemoveData.magnetUri, podHost: fromHost }) 142 logger.error('No remote video was found for this pod.', { remoteId: videoToRemoveData.remoteId, podHost: fromHost })
78 } 143 }
79 144
80 each(videosList, function (video, callbackEach) { 145 each(videosList, function (video, callbackEach) {
81 logger.debug('Removing remote video %s.', video.magnetUri) 146 logger.debug('Removing remote video %s.', video.remoteId)
82 147
83 video.remove(callbackEach) 148 video.destroy().asCallback(callbackEach)
84 }, callback) 149 }, callback)
85 }) 150 })
86} 151}
diff --git a/server/controllers/api/requests.js b/server/controllers/api/requests.js
index 52aad6997..1f9193fc8 100644
--- a/server/controllers/api/requests.js
+++ b/server/controllers/api/requests.js
@@ -1,15 +1,13 @@
1'use strict' 1'use strict'
2 2
3const express = require('express') 3const express = require('express')
4const mongoose = require('mongoose')
5 4
6const constants = require('../../initializers/constants') 5const constants = require('../../initializers/constants')
6const db = require('../../initializers/database')
7const middlewares = require('../../middlewares') 7const middlewares = require('../../middlewares')
8const admin = middlewares.admin 8const admin = middlewares.admin
9const oAuth = middlewares.oauth 9const oAuth = middlewares.oauth
10 10
11const Request = mongoose.model('Request')
12
13const router = express.Router() 11const router = express.Router()
14 12
15router.get('/stats', 13router.get('/stats',
@@ -25,13 +23,13 @@ module.exports = router
25// --------------------------------------------------------------------------- 23// ---------------------------------------------------------------------------
26 24
27function getStatsRequests (req, res, next) { 25function getStatsRequests (req, res, next) {
28 Request.list(function (err, requests) { 26 db.Request.countTotalRequests(function (err, totalRequests) {
29 if (err) return next(err) 27 if (err) return next(err)
30 28
31 return res.json({ 29 return res.json({
32 requests: requests, 30 totalRequests: totalRequests,
33 maxRequestsInParallel: constants.REQUESTS_IN_PARALLEL, 31 maxRequestsInParallel: constants.REQUESTS_IN_PARALLEL,
34 remainingMilliSeconds: Request.remainingMilliSeconds(), 32 remainingMilliSeconds: db.Request.remainingMilliSeconds(),
35 milliSecondsInterval: constants.REQUESTS_INTERVAL 33 milliSecondsInterval: constants.REQUESTS_INTERVAL
36 }) 34 })
37 }) 35 })
diff --git a/server/controllers/api/users.js b/server/controllers/api/users.js
index b4d687312..890028b36 100644
--- a/server/controllers/api/users.js
+++ b/server/controllers/api/users.js
@@ -2,10 +2,10 @@
2 2
3const each = require('async/each') 3const each = require('async/each')
4const express = require('express') 4const express = require('express')
5const mongoose = require('mongoose')
6const waterfall = require('async/waterfall') 5const waterfall = require('async/waterfall')
7 6
8const constants = require('../../initializers/constants') 7const constants = require('../../initializers/constants')
8const db = require('../../initializers/database')
9const friends = require('../../lib/friends') 9const friends = require('../../lib/friends')
10const logger = require('../../helpers/logger') 10const logger = require('../../helpers/logger')
11const middlewares = require('../../middlewares') 11const middlewares = require('../../middlewares')
@@ -17,9 +17,6 @@ const validatorsPagination = middlewares.validators.pagination
17const validatorsSort = middlewares.validators.sort 17const validatorsSort = middlewares.validators.sort
18const validatorsUsers = middlewares.validators.users 18const validatorsUsers = middlewares.validators.users
19 19
20const User = mongoose.model('User')
21const Video = mongoose.model('Video')
22
23const router = express.Router() 20const router = express.Router()
24 21
25router.get('/me', oAuth.authenticate, getUserInformation) 22router.get('/me', oAuth.authenticate, getUserInformation)
@@ -62,13 +59,13 @@ module.exports = router
62// --------------------------------------------------------------------------- 59// ---------------------------------------------------------------------------
63 60
64function createUser (req, res, next) { 61function createUser (req, res, next) {
65 const user = new User({ 62 const user = db.User.build({
66 username: req.body.username, 63 username: req.body.username,
67 password: req.body.password, 64 password: req.body.password,
68 role: constants.USER_ROLES.USER 65 role: constants.USER_ROLES.USER
69 }) 66 })
70 67
71 user.save(function (err, createdUser) { 68 user.save().asCallback(function (err, createdUser) {
72 if (err) return next(err) 69 if (err) return next(err)
73 70
74 return res.type('json').status(204).end() 71 return res.type('json').status(204).end()
@@ -76,7 +73,7 @@ function createUser (req, res, next) {
76} 73}
77 74
78function getUserInformation (req, res, next) { 75function getUserInformation (req, res, next) {
79 User.loadByUsername(res.locals.oauth.token.user.username, function (err, user) { 76 db.User.loadByUsername(res.locals.oauth.token.user.username, function (err, user) {
80 if (err) return next(err) 77 if (err) return next(err)
81 78
82 return res.json(user.toFormatedJSON()) 79 return res.json(user.toFormatedJSON())
@@ -84,7 +81,7 @@ function getUserInformation (req, res, next) {
84} 81}
85 82
86function listUsers (req, res, next) { 83function listUsers (req, res, next) {
87 User.listForApi(req.query.start, req.query.count, req.query.sort, function (err, usersList, usersTotal) { 84 db.User.listForApi(req.query.start, req.query.count, req.query.sort, function (err, usersList, usersTotal) {
88 if (err) return next(err) 85 if (err) return next(err)
89 86
90 res.json(getFormatedUsers(usersList, usersTotal)) 87 res.json(getFormatedUsers(usersList, usersTotal))
@@ -94,18 +91,19 @@ function listUsers (req, res, next) {
94function removeUser (req, res, next) { 91function removeUser (req, res, next) {
95 waterfall([ 92 waterfall([
96 function getUser (callback) { 93 function getUser (callback) {
97 User.loadById(req.params.id, callback) 94 db.User.loadById(req.params.id, callback)
98 }, 95 },
99 96
97 // TODO: use foreignkey?
100 function getVideos (user, callback) { 98 function getVideos (user, callback) {
101 Video.listOwnedByAuthor(user.username, function (err, videos) { 99 db.Video.listOwnedByAuthor(user.username, function (err, videos) {
102 return callback(err, user, videos) 100 return callback(err, user, videos)
103 }) 101 })
104 }, 102 },
105 103
106 function removeVideosFromDB (user, videos, callback) { 104 function removeVideosFromDB (user, videos, callback) {
107 each(videos, function (video, callbackEach) { 105 each(videos, function (video, callbackEach) {
108 video.remove(callbackEach) 106 video.destroy().asCallback(callbackEach)
109 }, function (err) { 107 }, function (err) {
110 return callback(err, user, videos) 108 return callback(err, user, videos)
111 }) 109 })
@@ -115,7 +113,7 @@ function removeUser (req, res, next) {
115 videos.forEach(function (video) { 113 videos.forEach(function (video) {
116 const params = { 114 const params = {
117 name: video.name, 115 name: video.name,
118 magnetUri: video.magnetUri 116 remoteId: video.id
119 } 117 }
120 118
121 friends.removeVideoToFriends(params) 119 friends.removeVideoToFriends(params)
@@ -125,7 +123,7 @@ function removeUser (req, res, next) {
125 }, 123 },
126 124
127 function removeUserFromDB (user, callback) { 125 function removeUserFromDB (user, callback) {
128 user.remove(callback) 126 user.destroy().asCallback(callback)
129 } 127 }
130 ], function andFinally (err) { 128 ], function andFinally (err) {
131 if (err) { 129 if (err) {
@@ -138,11 +136,11 @@ function removeUser (req, res, next) {
138} 136}
139 137
140function updateUser (req, res, next) { 138function updateUser (req, res, next) {
141 User.loadByUsername(res.locals.oauth.token.user.username, function (err, user) { 139 db.User.loadByUsername(res.locals.oauth.token.user.username, function (err, user) {
142 if (err) return next(err) 140 if (err) return next(err)
143 141
144 user.password = req.body.password 142 user.password = req.body.password
145 user.save(function (err) { 143 user.save().asCallback(function (err) {
146 if (err) return next(err) 144 if (err) return next(err)
147 145
148 return res.sendStatus(204) 146 return res.sendStatus(204)
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
diff --git a/server/controllers/client.js b/server/controllers/client.js
index 572db6133..a5fac5626 100644
--- a/server/controllers/client.js
+++ b/server/controllers/client.js
@@ -3,13 +3,12 @@
3const parallel = require('async/parallel') 3const parallel = require('async/parallel')
4const express = require('express') 4const express = require('express')
5const fs = require('fs') 5const fs = require('fs')
6const mongoose = require('mongoose')
7const path = require('path') 6const path = require('path')
8const validator = require('express-validator').validator 7const validator = require('express-validator').validator
9 8
10const constants = require('../initializers/constants') 9const constants = require('../initializers/constants')
10const db = require('../initializers/database')
11 11
12const Video = mongoose.model('Video')
13const router = express.Router() 12const router = express.Router()
14 13
15const opengraphComment = '<!-- opengraph tags -->' 14const opengraphComment = '<!-- opengraph tags -->'
@@ -45,14 +44,14 @@ function addOpenGraphTags (htmlStringPage, video) {
45 if (video.isOwned()) { 44 if (video.isOwned()) {
46 basePreviewUrlHttp = constants.CONFIG.WEBSERVER.URL 45 basePreviewUrlHttp = constants.CONFIG.WEBSERVER.URL
47 } else { 46 } else {
48 basePreviewUrlHttp = constants.REMOTE_SCHEME.HTTP + '://' + video.podHost 47 basePreviewUrlHttp = constants.REMOTE_SCHEME.HTTP + '://' + video.Author.Pod.host
49 } 48 }
50 49
51 // We fetch the remote preview (bigger than the thumbnail) 50 // We fetch the remote preview (bigger than the thumbnail)
52 // This should not overhead the remote server since social websites put in a cache the OpenGraph tags 51 // This should not overhead the remote server since social websites put in a cache the OpenGraph tags
53 // We can't use the thumbnail because these social websites want bigger images (> 200x200 for Facebook for example) 52 // We can't use the thumbnail because these social websites want bigger images (> 200x200 for Facebook for example)
54 const previewUrl = basePreviewUrlHttp + constants.STATIC_PATHS.PREVIEWS + video.getPreviewName() 53 const previewUrl = basePreviewUrlHttp + constants.STATIC_PATHS.PREVIEWS + video.getPreviewName()
55 const videoUrl = constants.CONFIG.WEBSERVER.URL + '/videos/watch/' + video._id 54 const videoUrl = constants.CONFIG.WEBSERVER.URL + '/videos/watch/' + video.id
56 55
57 const metaTags = { 56 const metaTags = {
58 'og:type': 'video', 57 'og:type': 'video',
@@ -86,7 +85,7 @@ function generateWatchHtmlPage (req, res, next) {
86 const videoId = req.params.id 85 const videoId = req.params.id
87 86
88 // Let Angular application handle errors 87 // Let Angular application handle errors
89 if (!validator.isMongoId(videoId)) return res.sendFile(indexPath) 88 if (!validator.isUUID(videoId, 4)) return res.sendFile(indexPath)
90 89
91 parallel({ 90 parallel({
92 file: function (callback) { 91 file: function (callback) {
@@ -94,7 +93,7 @@ function generateWatchHtmlPage (req, res, next) {
94 }, 93 },
95 94
96 video: function (callback) { 95 video: function (callback) {
97 Video.load(videoId, callback) 96 db.Video.loadAndPopulateAuthorAndPod(videoId, callback)
98 } 97 }
99 }, function (err, results) { 98 }, function (err, results) {
100 if (err) return next(err) 99 if (err) return next(err)