diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-08-16 21:51:04 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-08-16 21:51:04 +0200 |
commit | 0ff21c1c086f907612e34880f76f08bc74eeb78c (patch) | |
tree | 30da1520f5c9ba451158d0db1d22416ea49e0560 | |
parent | 7eef95353f9202e1f3285606282fc8fd904c90ef (diff) | |
download | PeerTube-0ff21c1c086f907612e34880f76f08bc74eeb78c.tar.gz PeerTube-0ff21c1c086f907612e34880f76f08bc74eeb78c.tar.zst PeerTube-0ff21c1c086f907612e34880f76f08bc74eeb78c.zip |
Server: video.list -> video.listForApi (with pagination, sort...)
-rw-r--r-- | server/controllers/api/v1/videos.js | 2 | ||||
-rw-r--r-- | server/models/video.js | 28 |
2 files changed, 6 insertions, 24 deletions
diff --git a/server/controllers/api/v1/videos.js b/server/controllers/api/v1/videos.js index 1f939b077..0a441f146 100644 --- a/server/controllers/api/v1/videos.js +++ b/server/controllers/api/v1/videos.js | |||
@@ -142,7 +142,7 @@ function getVideo (req, res, next) { | |||
142 | } | 142 | } |
143 | 143 | ||
144 | function listVideos (req, res, next) { | 144 | function listVideos (req, res, next) { |
145 | Video.list(req.query.start, req.query.count, req.query.sort, function (err, videosList, videosTotal) { | 145 | Video.listForApi(req.query.start, req.query.count, req.query.sort, function (err, videosList, videosTotal) { |
146 | if (err) return next(err) | 146 | if (err) return next(err) |
147 | 147 | ||
148 | res.json(getFormatedVideos(videosList, videosTotal)) | 148 | res.json(getFormatedVideos(videosList, videosTotal)) |
diff --git a/server/models/video.js b/server/models/video.js index 14bc91b16..a5540d127 100644 --- a/server/models/video.js +++ b/server/models/video.js | |||
@@ -11,6 +11,7 @@ const mongoose = require('mongoose') | |||
11 | const constants = require('../initializers/constants') | 11 | const constants = require('../initializers/constants') |
12 | const customVideosValidators = require('../helpers/custom-validators').videos | 12 | const customVideosValidators = require('../helpers/custom-validators').videos |
13 | const logger = require('../helpers/logger') | 13 | const logger = require('../helpers/logger') |
14 | const modelUtils = require('./utils') | ||
14 | const utils = require('../helpers/utils') | 15 | const utils = require('../helpers/utils') |
15 | const webtorrent = require('../lib/webtorrent') | 16 | const webtorrent = require('../lib/webtorrent') |
16 | 17 | ||
@@ -60,7 +61,7 @@ VideoSchema.methods = { | |||
60 | 61 | ||
61 | VideoSchema.statics = { | 62 | VideoSchema.statics = { |
62 | getDurationFromFile: getDurationFromFile, | 63 | getDurationFromFile: getDurationFromFile, |
63 | list: list, | 64 | listForApi: listForApi, |
64 | listByUrlAndMagnet: listByUrlAndMagnet, | 65 | listByUrlAndMagnet: listByUrlAndMagnet, |
65 | listByUrls: listByUrls, | 66 | listByUrls: listByUrls, |
66 | listOwned: listOwned, | 67 | listOwned: listOwned, |
@@ -194,9 +195,9 @@ function getDurationFromFile (videoPath, callback) { | |||
194 | }) | 195 | }) |
195 | } | 196 | } |
196 | 197 | ||
197 | function list (start, count, sort, callback) { | 198 | function listForApi (start, count, sort, callback) { |
198 | const query = {} | 199 | const query = {} |
199 | return findWithCount.call(this, query, start, count, sort, callback) | 200 | return modelUtils.findWithCount.call(this, query, start, count, sort, callback) |
200 | } | 201 | } |
201 | 202 | ||
202 | function listByUrlAndMagnet (fromUrl, magnetUri, callback) { | 203 | function listByUrlAndMagnet (fromUrl, magnetUri, callback) { |
@@ -233,7 +234,7 @@ function search (value, field, start, count, sort, callback) { | |||
233 | query[field] = new RegExp(value) | 234 | query[field] = new RegExp(value) |
234 | } | 235 | } |
235 | 236 | ||
236 | findWithCount.call(this, query, start, count, sort, callback) | 237 | modelUtils.findWithCount.call(this, query, start, count, sort, callback) |
237 | } | 238 | } |
238 | 239 | ||
239 | function seedAllExisting (callback) { | 240 | function seedAllExisting (callback) { |
@@ -249,25 +250,6 @@ function seedAllExisting (callback) { | |||
249 | 250 | ||
250 | // --------------------------------------------------------------------------- | 251 | // --------------------------------------------------------------------------- |
251 | 252 | ||
252 | function findWithCount (query, start, count, sort, callback) { | ||
253 | const self = this | ||
254 | |||
255 | parallel([ | ||
256 | function (asyncCallback) { | ||
257 | self.find(query).skip(start).limit(count).sort(sort).exec(asyncCallback) | ||
258 | }, | ||
259 | function (asyncCallback) { | ||
260 | self.count(query, asyncCallback) | ||
261 | } | ||
262 | ], function (err, results) { | ||
263 | if (err) return callback(err) | ||
264 | |||
265 | const videos = results[0] | ||
266 | const totalVideos = results[1] | ||
267 | return callback(null, videos, totalVideos) | ||
268 | }) | ||
269 | } | ||
270 | |||
271 | function removeThumbnail (video, callback) { | 253 | function removeThumbnail (video, callback) { |
272 | fs.unlink(thumbnailsDir + video.thumbnail, callback) | 254 | fs.unlink(thumbnailsDir + video.thumbnail, callback) |
273 | } | 255 | } |