]>
Commit | Line | Data |
---|---|---|
9f10b292 C |
1 | 'use strict' |
2 | ||
f0f5567b | 3 | const config = require('config') |
f0f5567b | 4 | const mongoose = require('mongoose') |
9f10b292 | 5 | |
f0f5567b | 6 | const logger = require('../helpers/logger') |
9f10b292 | 7 | |
f0f5567b C |
8 | const http = config.get('webserver.https') === true ? 'https' : 'http' |
9 | const host = config.get('webserver.host') | |
10 | const port = config.get('webserver.port') | |
9f10b292 C |
11 | |
12 | // --------------------------------------------------------------------------- | |
13 | ||
f0f5567b | 14 | const videosSchema = mongoose.Schema({ |
9f10b292 C |
15 | name: String, |
16 | namePath: String, | |
17 | description: String, | |
18 | magnetUri: String, | |
0c1cbbfe | 19 | podUrl: String, |
3a8a8b51 | 20 | author: String, |
cbe2f7c3 C |
21 | duration: Number, |
22 | thumbnail: String | |
9f10b292 | 23 | }) |
f0f5567b | 24 | const VideosDB = mongoose.model('videos', videosSchema) |
9f10b292 C |
25 | |
26 | // --------------------------------------------------------------------------- | |
27 | ||
f0f5567b | 28 | const Videos = { |
9f10b292 C |
29 | add: add, |
30 | addRemotes: addRemotes, | |
31 | get: get, | |
9f10b292 | 32 | list: list, |
cbe2f7c3 C |
33 | listFromUrl: listFromUrl, |
34 | listFromUrls: listFromUrls, | |
35 | listFromUrlAndMagnets: listFromUrlAndMagnets, | |
36 | listFromRemotes: listFromRemotes, | |
9f10b292 C |
37 | listOwned: listOwned, |
38 | removeOwned: removeOwned, | |
cbe2f7c3 | 39 | removeByIds: removeByIds, |
9f10b292 C |
40 | search: search |
41 | } | |
42 | ||
43 | function add (video, callback) { | |
44 | logger.info('Adding %s video to database.', video.name) | |
45 | ||
f0f5567b | 46 | const params = video |
9f10b292 C |
47 | params.podUrl = http + '://' + host + ':' + port |
48 | ||
49 | VideosDB.create(params, function (err, video) { | |
50 | if (err) { | |
51 | logger.error('Cannot insert this video into database.') | |
52 | return callback(err) | |
53 | } | |
54 | ||
55 | callback(null) | |
c173e565 | 56 | }) |
9f10b292 | 57 | } |
8c308c2b | 58 | |
9f10b292 | 59 | function addRemotes (videos, callback) { |
cbe2f7c3 C |
60 | videos.forEach(function (video) { |
61 | // Ensure they are remote videos | |
62 | video.namePath = null | |
9f10b292 | 63 | }) |
cbe2f7c3 C |
64 | |
65 | VideosDB.create(videos, callback) | |
9f10b292 | 66 | } |
c45f7f84 | 67 | |
9f10b292 C |
68 | function get (id, callback) { |
69 | VideosDB.findById(id, function (err, video) { | |
70 | if (err) { | |
71 | logger.error('Cannot get this video.') | |
72 | return callback(err) | |
73 | } | |
c173e565 | 74 | |
9f10b292 C |
75 | return callback(null, video) |
76 | }) | |
77 | } | |
c173e565 | 78 | |
fbf1134e C |
79 | function list (start, count, callback) { |
80 | VideosDB.find({}).skip(start).limit(start + count).exec(function (err, videosList) { | |
9f10b292 C |
81 | if (err) { |
82 | logger.error('Cannot get the list of the videos.') | |
83 | return callback(err) | |
84 | } | |
c173e565 | 85 | |
bc503c2a | 86 | return callback(null, videosList) |
9f10b292 C |
87 | }) |
88 | } | |
c45f7f84 | 89 | |
cbe2f7c3 C |
90 | function listFromUrl (fromUrl, callback) { |
91 | VideosDB.find({ podUrl: fromUrl }, callback) | |
92 | } | |
93 | ||
94 | function listFromUrls (fromUrls, callback) { | |
95 | VideosDB.find({ podUrl: { $in: fromUrls } }, callback) | |
96 | } | |
97 | ||
98 | function listFromUrlAndMagnets (fromUrl, magnets, callback) { | |
99 | VideosDB.find({ podUrl: fromUrl, magnetUri: { $in: magnets } }, callback) | |
100 | } | |
101 | ||
102 | function listFromRemotes (callback) { | |
103 | VideosDB.find({ namePath: null }, callback) | |
104 | } | |
105 | ||
9f10b292 C |
106 | function listOwned (callback) { |
107 | // If namePath is not null this is *our* video | |
bc503c2a | 108 | VideosDB.find({ namePath: { $ne: null } }, function (err, videosList) { |
9f10b292 C |
109 | if (err) { |
110 | logger.error('Cannot get the list of owned videos.') | |
111 | return callback(err) | |
112 | } | |
113 | ||
bc503c2a | 114 | return callback(null, videosList) |
9f10b292 C |
115 | }) |
116 | } | |
117 | ||
cbe2f7c3 | 118 | // Return the video in the callback |
9f10b292 | 119 | function removeOwned (id, callback) { |
cbe2f7c3 | 120 | VideosDB.findByIdAndRemove(id, callback) |
9f10b292 C |
121 | } |
122 | ||
123 | // Use the magnet Uri because the _id field is not the same on different servers | |
cbe2f7c3 C |
124 | function removeByIds (ids, callback) { |
125 | VideosDB.remove({ _id: { $in: ids } }, callback) | |
9f10b292 | 126 | } |
8c308c2b | 127 | |
fbf1134e C |
128 | function search (name, start, count, callback) { |
129 | VideosDB.find({ name: new RegExp(name) }).skip(start).limit(start + count) | |
130 | .exec(function (err, videos) { | |
9f10b292 C |
131 | if (err) { |
132 | logger.error('Cannot search the videos.') | |
133 | return callback(err) | |
134 | } | |
8c308c2b | 135 | |
9f10b292 C |
136 | return callback(null, videos) |
137 | }) | |
138 | } | |
8c308c2b | 139 | |
9f10b292 | 140 | // --------------------------------------------------------------------------- |
c45f7f84 | 141 | |
9f10b292 | 142 | module.exports = Videos |