]>
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 | 23 | thumbnail: String, |
be587647 | 24 | tags: [ String ], |
bb10240e C |
25 | createdDate: { |
26 | type: Date, | |
27 | default: Date.now | |
28 | } | |
9f10b292 | 29 | }) |
f0f5567b | 30 | const VideosDB = mongoose.model('videos', videosSchema) |
9f10b292 C |
31 | |
32 | // --------------------------------------------------------------------------- | |
33 | ||
f0f5567b | 34 | const Videos = { |
9f10b292 C |
35 | add: add, |
36 | addRemotes: addRemotes, | |
37 | get: get, | |
9f10b292 | 38 | list: list, |
cbe2f7c3 C |
39 | listFromUrl: listFromUrl, |
40 | listFromUrls: listFromUrls, | |
41 | listFromUrlAndMagnets: listFromUrlAndMagnets, | |
42 | listFromRemotes: listFromRemotes, | |
9f10b292 C |
43 | listOwned: listOwned, |
44 | removeOwned: removeOwned, | |
cbe2f7c3 | 45 | removeByIds: removeByIds, |
9f10b292 C |
46 | search: search |
47 | } | |
48 | ||
49 | function add (video, callback) { | |
50 | logger.info('Adding %s video to database.', video.name) | |
51 | ||
f0f5567b | 52 | const params = video |
9f10b292 C |
53 | params.podUrl = http + '://' + host + ':' + port |
54 | ||
bb10240e | 55 | VideosDB.create(params, function (err, insertedVideo) { |
9f10b292 C |
56 | if (err) { |
57 | logger.error('Cannot insert this video into database.') | |
58 | return callback(err) | |
59 | } | |
60 | ||
bb10240e | 61 | callback(null, insertedVideo) |
c173e565 | 62 | }) |
9f10b292 | 63 | } |
8c308c2b | 64 | |
9f10b292 | 65 | function addRemotes (videos, callback) { |
cbe2f7c3 C |
66 | videos.forEach(function (video) { |
67 | // Ensure they are remote videos | |
68 | video.namePath = null | |
9f10b292 | 69 | }) |
cbe2f7c3 C |
70 | |
71 | VideosDB.create(videos, callback) | |
9f10b292 | 72 | } |
c45f7f84 | 73 | |
9f10b292 C |
74 | function get (id, callback) { |
75 | VideosDB.findById(id, function (err, video) { | |
76 | if (err) { | |
77 | logger.error('Cannot get this video.') | |
78 | return callback(err) | |
79 | } | |
c173e565 | 80 | |
9f10b292 C |
81 | return callback(null, video) |
82 | }) | |
83 | } | |
c173e565 | 84 | |
a877d5ac | 85 | function list (start, count, sort, callback) { |
68ce3ae0 C |
86 | const query = {} |
87 | return findWithCount(query, start, count, sort, callback) | |
9f10b292 | 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 | |
46246b5f C |
128 | function search (value, field, start, count, sort, callback) { |
129 | const query = {} | |
130 | // Make an exact search with the magnet | |
8d199cb8 | 131 | if (field === 'magnetUri' || field === 'tags') { |
46246b5f C |
132 | query[field] = value |
133 | } else { | |
134 | query[field] = new RegExp(value) | |
135 | } | |
136 | ||
68ce3ae0 | 137 | findWithCount(query, start, count, sort, callback) |
9f10b292 | 138 | } |
8c308c2b | 139 | |
9f10b292 | 140 | // --------------------------------------------------------------------------- |
c45f7f84 | 141 | |
9f10b292 | 142 | module.exports = Videos |
68ce3ae0 C |
143 | |
144 | // --------------------------------------------------------------------------- | |
145 | ||
146 | function findWithCount (query, start, count, sort, callback) { | |
147 | async.parallel([ | |
148 | function (asyncCallback) { | |
149 | VideosDB.find(query).skip(start).limit(start + count).sort(sort).exec(asyncCallback) | |
150 | }, | |
151 | function (asyncCallback) { | |
152 | VideosDB.count(query, asyncCallback) | |
153 | } | |
154 | ], function (err, results) { | |
155 | if (err) return callback(err) | |
156 | ||
157 | const videos = results[0] | |
158 | const totalVideos = results[1] | |
159 | return callback(null, videos, totalVideos) | |
160 | }) | |
161 | } |