diff options
Diffstat (limited to 'server/controllers')
-rw-r--r-- | server/controllers/api/clients.js | 8 | ||||
-rw-r--r-- | server/controllers/api/pods.js | 15 | ||||
-rw-r--r-- | server/controllers/api/remote.js | 95 | ||||
-rw-r--r-- | server/controllers/api/requests.js | 10 | ||||
-rw-r--r-- | server/controllers/api/users.js | 28 | ||||
-rw-r--r-- | server/controllers/api/videos.js | 63 | ||||
-rw-r--r-- | server/controllers/client.js | 11 |
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 | ||
3 | const express = require('express') | 3 | const express = require('express') |
4 | const mongoose = require('mongoose') | ||
5 | 4 | ||
6 | const constants = require('../../initializers/constants') | 5 | const constants = require('../../initializers/constants') |
6 | const db = require('../../initializers/database') | ||
7 | const logger = require('../../helpers/logger') | 7 | const logger = require('../../helpers/logger') |
8 | 8 | ||
9 | const Client = mongoose.model('OAuthClient') | ||
10 | |||
11 | const router = express.Router() | 9 | const router = express.Router() |
12 | 10 | ||
13 | router.get('/local', getLocalClient) | 11 | router.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 | ||
3 | const express = require('express') | 3 | const express = require('express') |
4 | const mongoose = require('mongoose') | ||
5 | const waterfall = require('async/waterfall') | 4 | const waterfall = require('async/waterfall') |
6 | 5 | ||
6 | const db = require('../../initializers/database') | ||
7 | const logger = require('../../helpers/logger') | 7 | const logger = require('../../helpers/logger') |
8 | const friends = require('../../lib/friends') | 8 | const friends = require('../../lib/friends') |
9 | const middlewares = require('../../middlewares') | 9 | const middlewares = require('../../middlewares') |
@@ -15,7 +15,6 @@ const validators = middlewares.validators.pods | |||
15 | const signatureValidator = middlewares.validators.remote.signature | 15 | const signatureValidator = middlewares.validators.remote.signature |
16 | 16 | ||
17 | const router = express.Router() | 17 | const router = express.Router() |
18 | const Pod = mongoose.model('Pod') | ||
19 | 18 | ||
20 | router.get('/', listPods) | 19 | router.get('/', listPods) |
21 | router.post('/', | 20 | router.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 | ||
86 | function listPods (req, res, next) { | 85 | function 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 @@ | |||
3 | const each = require('async/each') | 3 | const each = require('async/each') |
4 | const eachSeries = require('async/eachSeries') | 4 | const eachSeries = require('async/eachSeries') |
5 | const express = require('express') | 5 | const express = require('express') |
6 | const mongoose = require('mongoose') | 6 | const waterfall = require('async/waterfall') |
7 | 7 | ||
8 | const db = require('../../initializers/database') | ||
8 | const middlewares = require('../../middlewares') | 9 | const middlewares = require('../../middlewares') |
9 | const secureMiddleware = middlewares.secure | 10 | const secureMiddleware = middlewares.secure |
10 | const validators = middlewares.validators.remote | 11 | const validators = middlewares.validators.remote |
11 | const logger = require('../../helpers/logger') | 12 | const logger = require('../../helpers/logger') |
12 | 13 | ||
13 | const router = express.Router() | 14 | const router = express.Router() |
14 | const Video = mongoose.model('Video') | ||
15 | 15 | ||
16 | router.post('/videos', | 16 | router.post('/videos', |
17 | validators.signature, | 17 | validators.signature, |
@@ -53,34 +53,99 @@ function remoteVideos (req, res, next) { | |||
53 | function addRemoteVideo (videoToCreateData, fromHost, callback) { | 53 | function 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 | ||
68 | function removeRemoteVideo (videoToRemoveData, fromHost, callback) { | 131 | function 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 | ||
3 | const express = require('express') | 3 | const express = require('express') |
4 | const mongoose = require('mongoose') | ||
5 | 4 | ||
6 | const constants = require('../../initializers/constants') | 5 | const constants = require('../../initializers/constants') |
6 | const db = require('../../initializers/database') | ||
7 | const middlewares = require('../../middlewares') | 7 | const middlewares = require('../../middlewares') |
8 | const admin = middlewares.admin | 8 | const admin = middlewares.admin |
9 | const oAuth = middlewares.oauth | 9 | const oAuth = middlewares.oauth |
10 | 10 | ||
11 | const Request = mongoose.model('Request') | ||
12 | |||
13 | const router = express.Router() | 11 | const router = express.Router() |
14 | 12 | ||
15 | router.get('/stats', | 13 | router.get('/stats', |
@@ -25,13 +23,13 @@ module.exports = router | |||
25 | // --------------------------------------------------------------------------- | 23 | // --------------------------------------------------------------------------- |
26 | 24 | ||
27 | function getStatsRequests (req, res, next) { | 25 | function 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 | ||
3 | const each = require('async/each') | 3 | const each = require('async/each') |
4 | const express = require('express') | 4 | const express = require('express') |
5 | const mongoose = require('mongoose') | ||
6 | const waterfall = require('async/waterfall') | 5 | const waterfall = require('async/waterfall') |
7 | 6 | ||
8 | const constants = require('../../initializers/constants') | 7 | const constants = require('../../initializers/constants') |
8 | const db = require('../../initializers/database') | ||
9 | const friends = require('../../lib/friends') | 9 | const friends = require('../../lib/friends') |
10 | const logger = require('../../helpers/logger') | 10 | const logger = require('../../helpers/logger') |
11 | const middlewares = require('../../middlewares') | 11 | const middlewares = require('../../middlewares') |
@@ -17,9 +17,6 @@ const validatorsPagination = middlewares.validators.pagination | |||
17 | const validatorsSort = middlewares.validators.sort | 17 | const validatorsSort = middlewares.validators.sort |
18 | const validatorsUsers = middlewares.validators.users | 18 | const validatorsUsers = middlewares.validators.users |
19 | 19 | ||
20 | const User = mongoose.model('User') | ||
21 | const Video = mongoose.model('Video') | ||
22 | |||
23 | const router = express.Router() | 20 | const router = express.Router() |
24 | 21 | ||
25 | router.get('/me', oAuth.authenticate, getUserInformation) | 22 | router.get('/me', oAuth.authenticate, getUserInformation) |
@@ -62,13 +59,13 @@ module.exports = router | |||
62 | // --------------------------------------------------------------------------- | 59 | // --------------------------------------------------------------------------- |
63 | 60 | ||
64 | function createUser (req, res, next) { | 61 | function 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 | ||
78 | function getUserInformation (req, res, next) { | 75 | function 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 | ||
86 | function listUsers (req, res, next) { | 83 | function 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) { | |||
94 | function removeUser (req, res, next) { | 91 | function 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 | ||
140 | function updateUser (req, res, next) { | 138 | function 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 | ||
3 | const express = require('express') | 3 | const express = require('express') |
4 | const fs = require('fs') | 4 | const fs = require('fs') |
5 | const mongoose = require('mongoose') | ||
6 | const multer = require('multer') | 5 | const multer = require('multer') |
7 | const path = require('path') | 6 | const path = require('path') |
8 | const waterfall = require('async/waterfall') | 7 | const waterfall = require('async/waterfall') |
9 | 8 | ||
10 | const constants = require('../../initializers/constants') | 9 | const constants = require('../../initializers/constants') |
10 | const db = require('../../initializers/database') | ||
11 | const logger = require('../../helpers/logger') | 11 | const logger = require('../../helpers/logger') |
12 | const friends = require('../../lib/friends') | 12 | const friends = require('../../lib/friends') |
13 | const middlewares = require('../../middlewares') | 13 | const middlewares = require('../../middlewares') |
@@ -22,7 +22,6 @@ const sort = middlewares.sort | |||
22 | const utils = require('../../helpers/utils') | 22 | const utils = require('../../helpers/utils') |
23 | 23 | ||
24 | const router = express.Router() | 24 | const router = express.Router() |
25 | const Video = mongoose.model('Video') | ||
26 | 25 | ||
27 | // multer configuration | 26 | // multer configuration |
28 | const storage = multer.diskStorage({ | 27 | const 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 | ||
149 | function getVideo (req, res, next) { | 168 | function 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 | ||
161 | function listVideos (req, res, next) { | 180 | function 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 | ||
205 | function searchVideos (req, res, next) { | 224 | function 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 @@ | |||
3 | const parallel = require('async/parallel') | 3 | const parallel = require('async/parallel') |
4 | const express = require('express') | 4 | const express = require('express') |
5 | const fs = require('fs') | 5 | const fs = require('fs') |
6 | const mongoose = require('mongoose') | ||
7 | const path = require('path') | 6 | const path = require('path') |
8 | const validator = require('express-validator').validator | 7 | const validator = require('express-validator').validator |
9 | 8 | ||
10 | const constants = require('../initializers/constants') | 9 | const constants = require('../initializers/constants') |
10 | const db = require('../initializers/database') | ||
11 | 11 | ||
12 | const Video = mongoose.model('Video') | ||
13 | const router = express.Router() | 12 | const router = express.Router() |
14 | 13 | ||
15 | const opengraphComment = '<!-- opengraph tags -->' | 14 | const 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) |