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