]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/videos.js
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({
30 const VideosDB
= mongoose
.model('videos', videosSchema
)
32 // ---------------------------------------------------------------------------
36 addRemotes: addRemotes
,
39 listFromUrl: listFromUrl
,
40 listFromUrls: listFromUrls
,
41 listFromUrlAndMagnets: listFromUrlAndMagnets
,
42 listFromRemotes: listFromRemotes
,
44 removeOwned: removeOwned
,
45 removeByIds: removeByIds
,
49 function add (video
, callback
) {
50 logger
.info('Adding %s video to database.', video
.name
)
53 params
.podUrl
= http
+ '://' + host
+ ':' + port
55 VideosDB
.create(params
, function (err
, insertedVideo
) {
57 logger
.error('Cannot insert this video into database.')
61 callback(null, insertedVideo
)
65 function addRemotes (videos
, callback
) {
66 videos
.forEach(function (video
) {
67 // Ensure they are remote videos
71 VideosDB
.create(videos
, callback
)
74 function get (id
, callback
) {
75 VideosDB
.findById(id
, function (err
, video
) {
77 logger
.error('Cannot get this video.')
81 return callback(null, video
)
85 function list (start
, count
, sort
, callback
) {
87 return findWithCount(query
, start
, count
, sort
, callback
)
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 (value
, field
, start
, count
, sort
, callback
) {
130 // Make an exact search with the magnet
131 if (field
=== 'magnetUri') {
134 query
[field
] = new RegExp(value
)
137 findWithCount(query
, start
, count
, sort
, callback
)
140 // ---------------------------------------------------------------------------
142 module
.exports
= Videos
144 // ---------------------------------------------------------------------------
146 function findWithCount (query
, start
, count
, sort
, callback
) {
148 function (asyncCallback
) {
149 VideosDB
.find(query
).skip(start
).limit(start
+ count
).sort(sort
).exec(asyncCallback
)
151 function (asyncCallback
) {
152 VideosDB
.count(query
, asyncCallback
)
154 ], function (err
, results
) {
155 if (err
) return callback(err
)
157 const videos
= results
[0]
158 const totalVideos
= results
[1]
159 return callback(null, videos
, totalVideos
)