2016-02-07 05:23:23 -05:00
|
|
|
'use strict'
|
|
|
|
|
2016-05-21 13:30:22 -04:00
|
|
|
const async = require('async')
|
2016-03-16 17:29:27 -04:00
|
|
|
const config = require('config')
|
|
|
|
const mongoose = require('mongoose')
|
2016-02-07 05:23:23 -05:00
|
|
|
|
2016-03-16 17:29:27 -04:00
|
|
|
const logger = require('../helpers/logger')
|
2016-02-07 05:23:23 -05:00
|
|
|
|
2016-03-16 17:29:27 -04:00
|
|
|
const http = config.get('webserver.https') === true ? 'https' : 'http'
|
|
|
|
const host = config.get('webserver.host')
|
|
|
|
const port = config.get('webserver.port')
|
2016-02-07 05:23:23 -05:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2016-03-16 17:29:27 -04:00
|
|
|
const videosSchema = mongoose.Schema({
|
2016-02-07 05:23:23 -05:00
|
|
|
name: String,
|
|
|
|
namePath: String,
|
|
|
|
description: String,
|
|
|
|
magnetUri: String,
|
2016-04-14 16:06:11 -04:00
|
|
|
podUrl: String,
|
2016-05-03 16:41:46 -04:00
|
|
|
author: String,
|
2016-05-10 15:19:24 -04:00
|
|
|
duration: Number,
|
2016-05-13 14:42:11 -04:00
|
|
|
thumbnail: String,
|
|
|
|
createdDate: {
|
|
|
|
type: Date,
|
|
|
|
default: Date.now
|
|
|
|
}
|
2016-02-07 05:23:23 -05:00
|
|
|
})
|
2016-03-16 17:29:27 -04:00
|
|
|
const VideosDB = mongoose.model('videos', videosSchema)
|
2016-02-07 05:23:23 -05:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2016-03-16 17:29:27 -04:00
|
|
|
const Videos = {
|
2016-02-07 05:23:23 -05:00
|
|
|
add: add,
|
|
|
|
addRemotes: addRemotes,
|
|
|
|
get: get,
|
|
|
|
list: list,
|
2016-05-10 15:19:24 -04:00
|
|
|
listFromUrl: listFromUrl,
|
|
|
|
listFromUrls: listFromUrls,
|
|
|
|
listFromUrlAndMagnets: listFromUrlAndMagnets,
|
|
|
|
listFromRemotes: listFromRemotes,
|
2016-02-07 05:23:23 -05:00
|
|
|
listOwned: listOwned,
|
|
|
|
removeOwned: removeOwned,
|
2016-05-10 15:19:24 -04:00
|
|
|
removeByIds: removeByIds,
|
2016-02-07 05:23:23 -05:00
|
|
|
search: search
|
|
|
|
}
|
|
|
|
|
|
|
|
function add (video, callback) {
|
|
|
|
logger.info('Adding %s video to database.', video.name)
|
|
|
|
|
2016-03-16 17:29:27 -04:00
|
|
|
const params = video
|
2016-02-07 05:23:23 -05:00
|
|
|
params.podUrl = http + '://' + host + ':' + port
|
|
|
|
|
2016-05-13 14:42:11 -04:00
|
|
|
VideosDB.create(params, function (err, insertedVideo) {
|
2016-02-07 05:23:23 -05:00
|
|
|
if (err) {
|
|
|
|
logger.error('Cannot insert this video into database.')
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
|
2016-05-13 14:42:11 -04:00
|
|
|
callback(null, insertedVideo)
|
2016-02-04 15:10:33 -05:00
|
|
|
})
|
2016-02-07 05:23:23 -05:00
|
|
|
}
|
2015-06-09 11:41:40 -04:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
function addRemotes (videos, callback) {
|
2016-05-10 15:19:24 -04:00
|
|
|
videos.forEach(function (video) {
|
|
|
|
// Ensure they are remote videos
|
|
|
|
video.namePath = null
|
2016-02-07 05:23:23 -05:00
|
|
|
})
|
2016-05-10 15:19:24 -04:00
|
|
|
|
|
|
|
VideosDB.create(videos, callback)
|
2016-02-07 05:23:23 -05:00
|
|
|
}
|
2016-01-31 05:23:52 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
function get (id, callback) {
|
|
|
|
VideosDB.findById(id, function (err, video) {
|
|
|
|
if (err) {
|
|
|
|
logger.error('Cannot get this video.')
|
|
|
|
return callback(err)
|
|
|
|
}
|
2016-02-04 15:10:33 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
return callback(null, video)
|
|
|
|
})
|
|
|
|
}
|
2016-02-04 15:10:33 -05:00
|
|
|
|
2016-05-17 15:03:00 -04:00
|
|
|
function list (start, count, sort, callback) {
|
2016-05-21 13:30:22 -04:00
|
|
|
const query = {}
|
|
|
|
return findWithCount(query, start, count, sort, callback)
|
2016-02-07 05:23:23 -05:00
|
|
|
}
|
2016-01-31 05:23:52 -05:00
|
|
|
|
2016-05-10 15:19:24 -04:00
|
|
|
function listFromUrl (fromUrl, callback) {
|
|
|
|
VideosDB.find({ podUrl: fromUrl }, callback)
|
|
|
|
}
|
|
|
|
|
|
|
|
function listFromUrls (fromUrls, callback) {
|
|
|
|
VideosDB.find({ podUrl: { $in: fromUrls } }, callback)
|
|
|
|
}
|
|
|
|
|
|
|
|
function listFromUrlAndMagnets (fromUrl, magnets, callback) {
|
|
|
|
VideosDB.find({ podUrl: fromUrl, magnetUri: { $in: magnets } }, callback)
|
|
|
|
}
|
|
|
|
|
|
|
|
function listFromRemotes (callback) {
|
|
|
|
VideosDB.find({ namePath: null }, callback)
|
|
|
|
}
|
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
function listOwned (callback) {
|
|
|
|
// If namePath is not null this is *our* video
|
2016-05-11 15:19:34 -04:00
|
|
|
VideosDB.find({ namePath: { $ne: null } }, function (err, videosList) {
|
2016-02-07 05:23:23 -05:00
|
|
|
if (err) {
|
|
|
|
logger.error('Cannot get the list of owned videos.')
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
|
2016-05-11 15:19:34 -04:00
|
|
|
return callback(null, videosList)
|
2016-02-07 05:23:23 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-05-10 15:19:24 -04:00
|
|
|
// Return the video in the callback
|
2016-02-07 05:23:23 -05:00
|
|
|
function removeOwned (id, callback) {
|
2016-05-10 15:19:24 -04:00
|
|
|
VideosDB.findByIdAndRemove(id, callback)
|
2016-02-07 05:23:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Use the magnet Uri because the _id field is not the same on different servers
|
2016-05-10 15:19:24 -04:00
|
|
|
function removeByIds (ids, callback) {
|
|
|
|
VideosDB.remove({ _id: { $in: ids } }, callback)
|
2016-02-07 05:23:23 -05:00
|
|
|
}
|
2015-06-09 11:41:40 -04:00
|
|
|
|
2016-05-22 03:15:00 -04:00
|
|
|
function search (value, field, start, count, sort, callback) {
|
|
|
|
const query = {}
|
|
|
|
// Make an exact search with the magnet
|
|
|
|
if (field === 'magnetUri') {
|
|
|
|
query[field] = value
|
|
|
|
} else {
|
|
|
|
query[field] = new RegExp(value)
|
|
|
|
}
|
|
|
|
|
2016-05-21 13:30:22 -04:00
|
|
|
findWithCount(query, start, count, sort, callback)
|
2016-02-07 05:23:23 -05:00
|
|
|
}
|
2015-06-09 11:41:40 -04:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
2016-01-31 05:23:52 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
module.exports = Videos
|
2016-05-21 13:30:22 -04:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
function findWithCount (query, start, count, sort, callback) {
|
|
|
|
async.parallel([
|
|
|
|
function (asyncCallback) {
|
|
|
|
VideosDB.find(query).skip(start).limit(start + count).sort(sort).exec(asyncCallback)
|
|
|
|
},
|
|
|
|
function (asyncCallback) {
|
|
|
|
VideosDB.count(query, asyncCallback)
|
|
|
|
}
|
|
|
|
], function (err, results) {
|
|
|
|
if (err) return callback(err)
|
|
|
|
|
|
|
|
const videos = results[0]
|
|
|
|
const totalVideos = results[1]
|
|
|
|
return callback(null, videos, totalVideos)
|
|
|
|
})
|
|
|
|
}
|