Add createdDate to videos

This commit is contained in:
Chocobozzz 2016-05-13 20:42:11 +02:00
parent 3fe81fa75e
commit bb10240ee1
6 changed files with 32 additions and 6 deletions

View File

@ -111,13 +111,15 @@ function addVideo (req, res, next) {
thumbnail: thumbnailName thumbnail: thumbnailName
} }
Videos.add(videoData, function (err) { Videos.add(videoData, function (err, insertedVideo) {
if (err) { if (err) {
// TODO unseed the video // TODO unseed the video
logger.error('Cannot insert this video in the database.') logger.error('Cannot insert this video in the database.')
return next(err) return next(err)
} }
videoData.createdDate = insertedVideo.createdDate
fs.readFile(thumbnailsDir + thumbnailName, function (err, data) { fs.readFile(thumbnailsDir + thumbnailName, function (err, data) {
if (err) { if (err) {
// TODO: remove video? // TODO: remove video?
@ -205,7 +207,8 @@ function getFormatedVideo (videoObj) {
magnetUri: videoObj.magnetUri, magnetUri: videoObj.magnetUri,
author: videoObj.author, author: videoObj.author,
duration: videoObj.duration, duration: videoObj.duration,
thumbnailPath: constants.THUMBNAILS_STATIC_PATH + '/' + videoObj.thumbnail thumbnailPath: constants.THUMBNAILS_STATIC_PATH + '/' + videoObj.thumbnail,
createdDate: videoObj.createdDate
} }
return formatedVideo return formatedVideo

View File

@ -14,7 +14,8 @@ function eachIsRemoteVideosAddValid (values) {
validator.isLength(val.description, 1, 50) && validator.isLength(val.description, 1, 50) &&
validator.isLength(val.magnetUri, 10) && validator.isLength(val.magnetUri, 10) &&
validator.isURL(val.podUrl) && validator.isURL(val.podUrl) &&
!isNaN(val.duration) !isNaN(val.duration) &&
validator.isDate(val.createdDate)
}) })
} }

View File

@ -19,7 +19,11 @@ const videosSchema = mongoose.Schema({
podUrl: String, podUrl: String,
author: String, author: String,
duration: Number, duration: Number,
thumbnail: String thumbnail: String,
createdDate: {
type: Date,
default: Date.now
}
}) })
const VideosDB = mongoose.model('videos', videosSchema) const VideosDB = mongoose.model('videos', videosSchema)
@ -46,13 +50,13 @@ function add (video, callback) {
const params = video const params = video
params.podUrl = http + '://' + host + ':' + port params.podUrl = http + '://' + host + ':' + port
VideosDB.create(params, function (err, video) { VideosDB.create(params, function (err, insertedVideo) {
if (err) { if (err) {
logger.error('Cannot insert this video into database.') logger.error('Cannot insert this video into database.')
return callback(err) return callback(err)
} }
callback(null) callback(null, insertedVideo)
}) })
} }

View File

@ -98,6 +98,7 @@ describe('Test multiple pods', function () {
expect(video.podUrl).to.equal('http://localhost:9001') expect(video.podUrl).to.equal('http://localhost:9001')
expect(video.magnetUri).to.exist expect(video.magnetUri).to.exist
expect(video.duration).to.equal(10) expect(video.duration).to.equal(10)
expect(utils.dateIsValid(video.createdDate)).to.be.true
if (server.url !== 'http://localhost:9001') { if (server.url !== 'http://localhost:9001') {
expect(video.isLocal).to.be.false expect(video.isLocal).to.be.false
@ -153,6 +154,7 @@ describe('Test multiple pods', function () {
expect(video.podUrl).to.equal('http://localhost:9002') expect(video.podUrl).to.equal('http://localhost:9002')
expect(video.magnetUri).to.exist expect(video.magnetUri).to.exist
expect(video.duration).to.equal(5) expect(video.duration).to.equal(5)
expect(utils.dateIsValid(video.createdDate)).to.be.true
if (server.url !== 'http://localhost:9002') { if (server.url !== 'http://localhost:9002') {
expect(video.isLocal).to.be.false expect(video.isLocal).to.be.false
@ -221,12 +223,14 @@ describe('Test multiple pods', function () {
expect(video1.podUrl).to.equal('http://localhost:9003') expect(video1.podUrl).to.equal('http://localhost:9003')
expect(video1.magnetUri).to.exist expect(video1.magnetUri).to.exist
expect(video1.duration).to.equal(5) expect(video1.duration).to.equal(5)
expect(utils.dateIsValid(video1.createdDate)).to.be.true
expect(video2.name).to.equal('my super name for pod 3-2') expect(video2.name).to.equal('my super name for pod 3-2')
expect(video2.description).to.equal('my super description for pod 3-2') expect(video2.description).to.equal('my super description for pod 3-2')
expect(video2.podUrl).to.equal('http://localhost:9003') expect(video2.podUrl).to.equal('http://localhost:9003')
expect(video2.magnetUri).to.exist expect(video2.magnetUri).to.exist
expect(video2.duration).to.equal(5) expect(video2.duration).to.equal(5)
expect(utils.dateIsValid(video2.createdDate)).to.be.true
if (server.url !== 'http://localhost:9003') { if (server.url !== 'http://localhost:9003') {
expect(video1.isLocal).to.be.false expect(video1.isLocal).to.be.false

View File

@ -76,6 +76,7 @@ describe('Test a single pod', function () {
expect(video.magnetUri).to.exist expect(video.magnetUri).to.exist
expect(video.author).to.equal('root') expect(video.author).to.equal('root')
expect(video.isLocal).to.be.true expect(video.isLocal).to.be.true
expect(utils.dateIsValid(video.createdDate)).to.be.true
utils.testImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) { utils.testImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
if (err) throw err if (err) throw err
@ -109,6 +110,7 @@ describe('Test a single pod', function () {
expect(video.magnetUri).to.exist expect(video.magnetUri).to.exist
expect(video.author).to.equal('root') expect(video.author).to.equal('root')
expect(video.isLocal).to.be.true expect(video.isLocal).to.be.true
expect(utils.dateIsValid(video.createdDate)).to.be.true
utils.testImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) { utils.testImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
if (err) throw err if (err) throw err
@ -138,6 +140,7 @@ describe('Test a single pod', function () {
expect(video.podUrl).to.equal('http://localhost:9001') expect(video.podUrl).to.equal('http://localhost:9001')
expect(video.author).to.equal('root') expect(video.author).to.equal('root')
expect(video.isLocal).to.be.true expect(video.isLocal).to.be.true
expect(utils.dateIsValid(video.createdDate)).to.be.true
utils.testImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) { utils.testImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
if (err) throw err if (err) throw err

View File

@ -8,6 +8,7 @@ const pathUtils = require('path')
const request = require('supertest') const request = require('supertest')
const testUtils = { const testUtils = {
dateIsValid: dateIsValid,
flushTests: flushTests, flushTests: flushTests,
getFriendsList: getFriendsList, getFriendsList: getFriendsList,
getVideo: getVideo, getVideo: getVideo,
@ -28,6 +29,16 @@ const testUtils = {
// ---------------------- Export functions -------------------- // ---------------------- Export functions --------------------
function dateIsValid (dateString) {
const dateToCheck = new Date(dateString)
const now = new Date()
// Check if the interval is more than 2 minutes
if (now - dateToCheck > 120000) return false
return true
}
function flushTests (callback) { function flushTests (callback) {
exec('npm run clean:server:test', callback) exec('npm run clean:server:test', callback)
} }