]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/videos.js
9521e63e314ca35a08fd60d41ed8876e932b94d2
3 const async
= require('async')
4 const config
= require('config')
5 const mongoose
= require('mongoose')
7 const logger
= require('../helpers/logger')
9 const http
= config
.get('webserver.https') === true ? 'https' : 'http'
10 const host
= config
.get('webserver.host')
11 const port
= config
.get('webserver.port')
13 // ---------------------------------------------------------------------------
15 const videosSchema
= mongoose
.Schema({
29 const VideosDB
= mongoose
.model('videos', videosSchema
)
31 // ---------------------------------------------------------------------------
35 addRemotes: addRemotes
,
38 listFromUrl: listFromUrl
,
39 listFromUrls: listFromUrls
,
40 listFromUrlAndMagnets: listFromUrlAndMagnets
,
41 listFromRemotes: listFromRemotes
,
43 removeOwned: removeOwned
,
44 removeByIds: removeByIds
,
48 function add (video
, callback
) {
49 logger
.info('Adding %s video to database.', video
.name
)
52 params
.podUrl
= http
+ '://' + host
+ ':' + port
54 VideosDB
.create(params
, function (err
, insertedVideo
) {
56 logger
.error('Cannot insert this video into database.')
60 callback(null, insertedVideo
)
64 function addRemotes (videos
, callback
) {
65 videos
.forEach(function (video
) {
66 // Ensure they are remote videos
70 VideosDB
.create(videos
, callback
)
73 function get (id
, callback
) {
74 VideosDB
.findById(id
, function (err
, video
) {
76 logger
.error('Cannot get this video.')
80 return callback(null, video
)
84 function list (start
, count
, sort
, callback
) {
86 return findWithCount(query
, start
, count
, sort
, callback
)
89 function listFromUrl (fromUrl
, callback
) {
90 VideosDB
.find({ podUrl: fromUrl
}, callback
)
93 function listFromUrls (fromUrls
, callback
) {
94 VideosDB
.find({ podUrl: { $in: fromUrls
} }, callback
)
97 function listFromUrlAndMagnets (fromUrl
, magnets
, callback
) {
98 VideosDB
.find({ podUrl: fromUrl
, magnetUri: { $in: magnets
} }, callback
)
101 function listFromRemotes (callback
) {
102 VideosDB
.find({ namePath: null }, callback
)
105 function listOwned (callback
) {
106 // If namePath is not null this is *our* video
107 VideosDB
.find({ namePath: { $ne: null } }, function (err
, videosList
) {
109 logger
.error('Cannot get the list of owned videos.')
113 return callback(null, videosList
)
117 // Return the video in the callback
118 function removeOwned (id
, callback
) {
119 VideosDB
.findByIdAndRemove(id
, callback
)
122 // Use the magnet Uri because the _id field is not the same on different servers
123 function removeByIds (ids
, callback
) {
124 VideosDB
.remove({ _id: { $in: ids
} }, callback
)
127 function search (name
, start
, count
, sort
, callback
) {
128 const query
= { name: new RegExp(name
) }
129 findWithCount(query
, start
, count
, sort
, callback
)
132 // ---------------------------------------------------------------------------
134 module
.exports
= Videos
136 // ---------------------------------------------------------------------------
138 function findWithCount (query
, start
, count
, sort
, callback
) {
140 function (asyncCallback
) {
141 VideosDB
.find(query
).skip(start
).limit(start
+ count
).sort(sort
).exec(asyncCallback
)
143 function (asyncCallback
) {
144 VideosDB
.count(query
, asyncCallback
)
146 ], function (err
, results
) {
147 if (err
) return callback(err
)
149 const videos
= results
[0]
150 const totalVideos
= results
[1]
151 return callback(null, videos
, totalVideos
)