]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/videos.js
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({
28 const VideosDB
= mongoose
.model('videos', videosSchema
)
30 // ---------------------------------------------------------------------------
34 addRemotes: addRemotes
,
37 listFromUrl: listFromUrl
,
38 listFromUrls: listFromUrls
,
39 listFromUrlAndMagnets: listFromUrlAndMagnets
,
40 listFromRemotes: listFromRemotes
,
42 removeOwned: removeOwned
,
43 removeByIds: removeByIds
,
47 function add (video
, callback
) {
48 logger
.info('Adding %s video to database.', video
.name
)
51 params
.podUrl
= http
+ '://' + host
+ ':' + port
53 VideosDB
.create(params
, function (err
, insertedVideo
) {
55 logger
.error('Cannot insert this video into database.')
59 callback(null, insertedVideo
)
63 function addRemotes (videos
, callback
) {
64 videos
.forEach(function (video
) {
65 // Ensure they are remote videos
69 VideosDB
.create(videos
, callback
)
72 function get (id
, callback
) {
73 VideosDB
.findById(id
, function (err
, video
) {
75 logger
.error('Cannot get this video.')
79 return callback(null, video
)
83 function list (start
, count
, sort
, callback
) {
84 VideosDB
.find({}).skip(start
).limit(start
+ count
).sort(sort
)
85 .exec(function (err
, videosList
) {
87 logger
.error('Cannot get the list of the videos.')
91 return callback(null, videosList
)
95 function listFromUrl (fromUrl
, callback
) {
96 VideosDB
.find({ podUrl: fromUrl
}, callback
)
99 function listFromUrls (fromUrls
, callback
) {
100 VideosDB
.find({ podUrl: { $in: fromUrls
} }, callback
)
103 function listFromUrlAndMagnets (fromUrl
, magnets
, callback
) {
104 VideosDB
.find({ podUrl: fromUrl
, magnetUri: { $in: magnets
} }, callback
)
107 function listFromRemotes (callback
) {
108 VideosDB
.find({ namePath: null }, callback
)
111 function listOwned (callback
) {
112 // If namePath is not null this is *our* video
113 VideosDB
.find({ namePath: { $ne: null } }, function (err
, videosList
) {
115 logger
.error('Cannot get the list of owned videos.')
119 return callback(null, videosList
)
123 // Return the video in the callback
124 function removeOwned (id
, callback
) {
125 VideosDB
.findByIdAndRemove(id
, callback
)
128 // Use the magnet Uri because the _id field is not the same on different servers
129 function removeByIds (ids
, callback
) {
130 VideosDB
.remove({ _id: { $in: ids
} }, callback
)
133 function search (name
, start
, count
, sort
, callback
) {
134 VideosDB
.find({ name: new RegExp(name
) }).skip(start
).limit(start
+ count
).sort(sort
)
135 .exec(function (err
, videos
) {
137 logger
.error('Cannot search the videos.')
141 return callback(null, videos
)
145 // ---------------------------------------------------------------------------
147 module
.exports
= Videos