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