const pagination = middlewares.pagination
const reqValidator = middlewares.reqValidators
const reqValidatorPagination = reqValidator.pagination
+const reqValidatorSort = reqValidator.sort
const reqValidatorVideos = reqValidator.videos
+const sort = middlewares.sort
const utils = require('../../../helpers/utils')
const Videos = require('../../../models/videos') // model
const videos = require('../../../lib/videos')
router.get('/',
reqValidatorPagination.pagination,
+ reqValidatorSort.videosSort,
+ sort.setVideosSort,
pagination.setPagination,
listVideos
)
router.get('/search/:name',
reqValidatorVideos.videosSearch,
reqValidatorPagination.pagination,
+ reqValidatorSort.videosSort,
+ sort.setVideosSort,
pagination.setPagination,
searchVideos
)
}
function listVideos (req, res, next) {
- Videos.list(req.query.start, req.query.count, function (err, videosList) {
+ Videos.list(req.query.start, req.query.count, req.query.sort, function (err, videosList) {
if (err) return next(err)
res.json(getFormatedVideos(videosList))
}
function searchVideos (req, res, next) {
- Videos.search(req.params.name, req.query.start, req.query.count, function (err, videosList) {
+ Videos.search(req.params.name, req.query.start, req.query.count, req.query.sort, function (err, videosList) {
if (err) return next(err)
res.json(getFormatedVideos(videosList))
// Number of retries we make for the make retry requests (to friends...)
let REQUEST_RETRIES = 10
+// Sortable columns per schema
+const SORTABLE_COLUMNS = {
+ VIDEOS: [ 'name', '-name', 'duration', '-duration', 'createdDate', '-createdDate' ]
+}
+
// Videos thumbnail size
const THUMBNAILS_SIZE = '200x110'
PAGINATION_COUNT_DEFAULT: PAGINATION_COUNT_DEFAULT,
PODS_SCORE: PODS_SCORE,
REQUEST_RETRIES: REQUEST_RETRIES,
+ SORTABLE_COLUMNS: SORTABLE_COLUMNS,
THUMBNAILS_SIZE: THUMBNAILS_SIZE,
THUMBNAILS_STATIC_PATH: THUMBNAILS_STATIC_PATH
}
const oauth2 = require('./oauth2')
const pagination = require('./pagination')
const reqValidatorsMiddleware = require('./reqValidators')
+const sort = require('./sort')
const secureMiddleware = require('./secure')
const middlewares = {
oauth2: oauth2,
pagination: pagination,
reqValidators: reqValidatorsMiddleware,
+ sort: sort,
secure: secureMiddleware
}
const paginationReqValidators = require('./pagination')
const podsReqValidators = require('./pods')
const remoteReqValidators = require('./remote')
+const sortReqValidators = require('./sort')
const videosReqValidators = require('./videos')
const reqValidators = {
pagination: paginationReqValidators,
pods: podsReqValidators,
remote: remoteReqValidators,
+ sort: sortReqValidators,
videos: videosReqValidators
}
}
function pagination (req, res, next) {
- req.checkParams('start', 'Should have a number start').optional().isInt()
- req.checkParams('count', 'Should have a number count').optional().isInt()
+ req.checkQuery('start', 'Should have a number start').optional().isInt()
+ req.checkQuery('count', 'Should have a number count').optional().isInt()
- logger.debug('Checking pagination parameters', { parameters: req.params })
+ logger.debug('Checking pagination parameters', { parameters: req.query })
checkErrors(req, res, next)
}
--- /dev/null
+'use strict'
+
+const checkErrors = require('./utils').checkErrors
+const constants = require('../../initializers/constants')
+const logger = require('../../helpers/logger')
+
+const reqValidatorsSort = {
+ videosSort: videosSort
+}
+
+function videosSort (req, res, next) {
+ const sortableColumns = constants.SORTABLE_COLUMNS.VIDEOS
+
+ req.checkQuery('sort', 'Should have correct sortable column').optional().isIn(sortableColumns)
+
+ logger.debug('Checking sort parameters', { parameters: req.query })
+
+ checkErrors(req, res, next)
+}
+
+// ---------------------------------------------------------------------------
+
+module.exports = reqValidatorsSort
--- /dev/null
+'use strict'
+
+const sortMiddleware = {
+ setVideosSort: setVideosSort
+}
+
+function setVideosSort (req, res, next) {
+ if (!req.query.sort) req.query.sort = '-createdDate'
+
+ return next()
+}
+
+// ---------------------------------------------------------------------------
+
+module.exports = sortMiddleware
})
}
-function list (start, count, callback) {
- VideosDB.find({}).skip(start).limit(start + count).exec(function (err, videosList) {
+function list (start, count, sort, callback) {
+ VideosDB.find({}).skip(start).limit(start + count).sort(sort)
+ .exec(function (err, videosList) {
if (err) {
logger.error('Cannot get the list of the videos.')
return callback(err)
VideosDB.remove({ _id: { $in: ids } }, callback)
}
-function search (name, start, count, callback) {
- VideosDB.find({ name: new RegExp(name) }).skip(start).limit(start + count)
+function search (name, start, count, sort, callback) {
+ VideosDB.find({ name: new RegExp(name) }).skip(start).limit(start + count).sort(sort)
.exec(function (err, videos) {
if (err) {
logger.error('Cannot search the videos.')
describe('Of the videos API', function () {
const path = '/api/v1/videos/'
+ describe('When listing a video', function () {
+ it('Should fail with a bad start pagination', function (done) {
+ request(server.url)
+ .get(path)
+ .query({ start: 'hello' })
+ .set('Accept', 'application/json')
+ .expect(400, done)
+ })
+
+ it('Should fail with a bad count pagination', function (done) {
+ request(server.url)
+ .get(path)
+ .query({ count: 'hello' })
+ .set('Accept', 'application/json')
+ .expect(400, done)
+ })
+
+ it('Should fail with an incorrect sort', function (done) {
+ request(server.url)
+ .get(path)
+ .query({ sort: 'hello' })
+ .set('Accept', 'application/json')
+ .expect(400, done)
+ })
+ })
+
describe('When searching a video', function () {
it('Should fail with nothing', function (done) {
request(server.url)
.set('Accept', 'application/json')
.expect(400, done)
})
+
+ it('Should fail with a bad start pagination', function (done) {
+ request(server.url)
+ .get(pathUtils.join(path, 'search', 'test'))
+ .query({ start: 'hello' })
+ .set('Accept', 'application/json')
+ .expect(400, done)
+ })
+
+ it('Should fail with a bad count pagination', function (done) {
+ request(server.url)
+ .get(pathUtils.join(path, 'search', 'test'))
+ .query({ count: 'hello' })
+ .set('Accept', 'application/json')
+ .expect(400, done)
+ })
+
+ it('Should fail with an incorrect sort', function (done) {
+ request(server.url)
+ .get(pathUtils.join(path, 'search', 'test'))
+ .query({ sort: 'hello' })
+ .set('Accept', 'application/json')
+ .expect(400, done)
+ })
})
describe('When adding a video', function () {
})
})
+ it('Should list and sort by name in descending order', function (done) {
+ utils.getVideosListSort(server.url, '-name', function (err, res) {
+ if (err) throw err
+
+ const videos = res.body
+ expect(videos.length).to.equal(6)
+ expect(videos[5].name === 'video_short.mp4 name')
+ expect(videos[4].name === 'video_short.ogv name')
+ expect(videos[3].name === 'video_short.webm name')
+ expect(videos[2].name === 'video_short1.webm name')
+ expect(videos[1].name === 'video_short2.webm name')
+ expect(videos[0].name === 'video_short3.webm name')
+
+ done()
+ })
+ })
+
+ it('Should search and sort by name in ascending order', function (done) {
+ utils.searchVideoWithSort(server.url, 'webm', 'name', function (err, res) {
+ if (err) throw err
+
+ const videos = res.body
+ expect(videos.length).to.equal(4)
+
+ expect(videos[0].name === 'video_short.webm name')
+ expect(videos[1].name === 'video_short1.webm name')
+ expect(videos[2].name === 'video_short2.webm name')
+ expect(videos[3].name === 'video_short3.webm name')
+
+ done()
+ })
+ })
+
after(function (done) {
process.kill(-server.app.pid)
process.kill(-webtorrent.app.pid)
getVideo: getVideo,
getVideosList: getVideosList,
getVideosListPagination: getVideosListPagination,
+ getVideosListSort: getVideosListSort,
login: login,
loginAndGetAccessToken: loginAndGetAccessToken,
makeFriends: makeFriends,
runServer: runServer,
searchVideo: searchVideo,
searchVideoWithPagination: searchVideoWithPagination,
+ searchVideoWithSort: searchVideoWithSort,
testImage: testImage,
uploadVideo: uploadVideo
}
.end(end)
}
+function getVideosListSort (url, sort, end) {
+ const path = '/api/v1/videos'
+
+ request(url)
+ .get(path)
+ .query({ sort: sort })
+ .set('Accept', 'application/json')
+ .expect(200)
+ .expect('Content-Type', /json/)
+ .end(end)
+}
+
function login (url, client, user, expectedStatus, end) {
if (!end) {
end = expectedStatus
.end(end)
}
+function searchVideoWithSort (url, search, sort, end) {
+ const path = '/api/v1/videos'
+
+ request(url)
+ .get(path + '/search/' + search)
+ .query({ sort: sort })
+ .set('Accept', 'application/json')
+ .expect(200)
+ .expect('Content-Type', /json/)
+ .end(end)
+}
+
function testImage (url, videoName, imagePath, callback) {
request(url)
.get(imagePath)