]>
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 | const videosSchema = mongoose.Schema({ | |
16 | name: String, | |
17 | namePath: String, | |
18 | description: String, | |
19 | magnetUri: String, | |
20 | podUrl: String, | |
21 | author: String, | |
22 | duration: Number, | |
23 | thumbnail: String, | |
24 | tags: [ String ], | |
25 | createdDate: { | |
26 | type: Date, | |
27 | default: Date.now | |
28 | } | |
29 | }) | |
30 | const VideosDB = mongoose.model('videos', videosSchema) | |
31 | ||
32 | // --------------------------------------------------------------------------- | |
33 | ||
34 | const Videos = { | |
35 | add: add, | |
36 | addRemotes: addRemotes, | |
37 | get: get, | |
38 | list: list, | |
39 | listFromUrl: listFromUrl, | |
40 | listFromUrls: listFromUrls, | |
41 | listFromUrlAndMagnets: listFromUrlAndMagnets, | |
42 | listFromRemotes: listFromRemotes, | |
43 | listOwned: listOwned, | |
44 | removeOwned: removeOwned, | |
45 | removeByIds: removeByIds, | |
46 | search: search | |
47 | } | |
48 | ||
49 | function add (video, callback) { | |
50 | logger.info('Adding %s video to database.', video.name) | |
51 | ||
52 | const params = video | |
53 | params.podUrl = http + '://' + host + ':' + port | |
54 | ||
55 | VideosDB.create(params, function (err, insertedVideo) { | |
56 | if (err) { | |
57 | logger.error('Cannot insert this video into database.') | |
58 | return callback(err) | |
59 | } | |
60 | ||
61 | callback(null, insertedVideo) | |
62 | }) | |
63 | } | |
64 | ||
65 | function addRemotes (videos, callback) { | |
66 | videos.forEach(function (video) { | |
67 | // Ensure they are remote videos | |
68 | video.namePath = null | |
69 | }) | |
70 | ||
71 | VideosDB.create(videos, callback) | |
72 | } | |
73 | ||
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 | } | |
80 | ||
81 | return callback(null, video) | |
82 | }) | |
83 | } | |
84 | ||
85 | function list (start, count, sort, callback) { | |
86 | const query = {} | |
87 | return findWithCount(query, start, count, sort, callback) | |
88 | } | |
89 | ||
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 | ||
106 | function listOwned (callback) { | |
107 | // If namePath is not null this is *our* video | |
108 | VideosDB.find({ namePath: { $ne: null } }, function (err, videosList) { | |
109 | if (err) { | |
110 | logger.error('Cannot get the list of owned videos.') | |
111 | return callback(err) | |
112 | } | |
113 | ||
114 | return callback(null, videosList) | |
115 | }) | |
116 | } | |
117 | ||
118 | // Return the video in the callback | |
119 | function removeOwned (id, callback) { | |
120 | VideosDB.findByIdAndRemove(id, callback) | |
121 | } | |
122 | ||
123 | // Use the magnet Uri because the _id field is not the same on different servers | |
124 | function removeByIds (ids, callback) { | |
125 | VideosDB.remove({ _id: { $in: ids } }, callback) | |
126 | } | |
127 | ||
128 | function search (value, field, start, count, sort, callback) { | |
129 | const query = {} | |
130 | // Make an exact search with the magnet | |
131 | if (field === 'magnetUri' || field === 'tags') { | |
132 | query[field] = value | |
133 | } else { | |
134 | query[field] = new RegExp(value) | |
135 | } | |
136 | ||
137 | findWithCount(query, start, count, sort, callback) | |
138 | } | |
139 | ||
140 | // --------------------------------------------------------------------------- | |
141 | ||
142 | module.exports = Videos | |
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 | } |