3 const config = require('config')
4 const mongoose = require('mongoose')
6 const logger = require('../helpers/logger')
8 const http = config.get('webserver.https') === true ? 'https' : 'http'
9 const host = config.get('webserver.host')
10 const port = config.get('webserver.port')
12 // ---------------------------------------------------------------------------
14 const videosSchema = mongoose.Schema({
24 const VideosDB = mongoose.model('videos', videosSchema)
26 // ---------------------------------------------------------------------------
30 addRemotes: addRemotes,
33 listFromUrl: listFromUrl,
34 listFromUrls: listFromUrls,
35 listFromUrlAndMagnets: listFromUrlAndMagnets,
36 listFromRemotes: listFromRemotes,
38 removeOwned: removeOwned,
39 removeByIds: removeByIds,
43 function add (video, callback) {
44 logger.info('Adding %s video to database.', video.name)
47 params.podUrl = http + '://' + host + ':' + port
49 VideosDB.create(params, function (err, video) {
51 logger.error('Cannot insert this video into database.')
59 function addRemotes (videos, callback) {
60 videos.forEach(function (video) {
61 // Ensure they are remote videos
65 VideosDB.create(videos, callback)
68 function get (id, callback) {
69 VideosDB.findById(id, function (err, video) {
71 logger.error('Cannot get this video.')
75 return callback(null, video)
79 function list (callback) {
80 VideosDB.find(function (err, videosList) {
82 logger.error('Cannot get the list of the videos.')
86 return callback(null, videosList)
90 function listFromUrl (fromUrl, callback) {
91 VideosDB.find({ podUrl: fromUrl }, callback)
94 function listFromUrls (fromUrls, callback) {
95 VideosDB.find({ podUrl: { $in: fromUrls } }, callback)
98 function listFromUrlAndMagnets (fromUrl, magnets, callback) {
99 VideosDB.find({ podUrl: fromUrl, magnetUri: { $in: magnets } }, callback)
102 function listFromRemotes (callback) {
103 VideosDB.find({ namePath: null }, callback)
106 function listOwned (callback) {
107 // If namePath is not null this is *our* video
108 VideosDB.find({ namePath: { $ne: null } }, function (err, videosList) {
110 logger.error('Cannot get the list of owned videos.')
114 return callback(null, videosList)
118 // Return the video in the callback
119 function removeOwned (id, callback) {
120 VideosDB.findByIdAndRemove(id, callback)
123 // Use the magnet Uri because the _id field is not the same on different servers
124 function removeByIds (ids, callback) {
125 VideosDB.remove({ _id: { $in: ids } }, callback)
128 function search (name, callback) {
129 VideosDB.find({ name: new RegExp(name) }, function (err, videos) {
131 logger.error('Cannot search the videos.')
135 return callback(null, videos)
139 // ---------------------------------------------------------------------------
141 module.exports = Videos