]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/videos.js
250ad3952b27e297712c75c0cf9da3bedc96edc2
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
, callback
) {
84 VideosDB
.find({}).skip(start
).limit(start
+ count
).exec(function (err
, videosList
) {
86 logger
.error('Cannot get the list of the videos.')
90 return callback(null, videosList
)
94 function listFromUrl (fromUrl
, callback
) {
95 VideosDB
.find({ podUrl: fromUrl
}, callback
)
98 function listFromUrls (fromUrls
, callback
) {
99 VideosDB
.find({ podUrl: { $in: fromUrls
} }, callback
)
102 function listFromUrlAndMagnets (fromUrl
, magnets
, callback
) {
103 VideosDB
.find({ podUrl: fromUrl
, magnetUri: { $in: magnets
} }, callback
)
106 function listFromRemotes (callback
) {
107 VideosDB
.find({ namePath: null }, callback
)
110 function listOwned (callback
) {
111 // If namePath is not null this is *our* video
112 VideosDB
.find({ namePath: { $ne: null } }, function (err
, videosList
) {
114 logger
.error('Cannot get the list of owned videos.')
118 return callback(null, videosList
)
122 // Return the video in the callback
123 function removeOwned (id
, callback
) {
124 VideosDB
.findByIdAndRemove(id
, callback
)
127 // Use the magnet Uri because the _id field is not the same on different servers
128 function removeByIds (ids
, callback
) {
129 VideosDB
.remove({ _id: { $in: ids
} }, callback
)
132 function search (name
, start
, count
, callback
) {
133 VideosDB
.find({ name: new RegExp(name
) }).skip(start
).limit(start
+ count
)
134 .exec(function (err
, videos
) {
136 logger
.error('Cannot search the videos.')
140 return callback(null, videos
)
144 // ---------------------------------------------------------------------------
146 module
.exports
= Videos