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