diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-03-07 11:33:59 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-03-07 11:33:59 +0100 |
commit | b9a3e09ad5a7673f64556d1dba122ed4c4fac980 (patch) | |
tree | 66d4928b82af19a2372a2505822233884f3fd471 /server/models | |
parent | b2ff5e3e686eb552c5ccd64ce67b0455972ceef0 (diff) | |
download | PeerTube-b9a3e09ad5a7673f64556d1dba122ed4c4fac980.tar.gz PeerTube-b9a3e09ad5a7673f64556d1dba122ed4c4fac980.tar.zst PeerTube-b9a3e09ad5a7673f64556d1dba122ed4c4fac980.zip |
Prepare folders structure for angular app
Diffstat (limited to 'server/models')
-rw-r--r-- | server/models/pods.js | 88 | ||||
-rw-r--r-- | server/models/poolRequests.js | 55 | ||||
-rw-r--r-- | server/models/videos.js | 234 |
3 files changed, 377 insertions, 0 deletions
diff --git a/server/models/pods.js b/server/models/pods.js new file mode 100644 index 000000000..57ed20292 --- /dev/null +++ b/server/models/pods.js | |||
@@ -0,0 +1,88 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | var mongoose = require('mongoose') | ||
4 | |||
5 | var constants = require('../initializers/constants') | ||
6 | var logger = require('../helpers/logger') | ||
7 | |||
8 | // --------------------------------------------------------------------------- | ||
9 | |||
10 | var podsSchema = mongoose.Schema({ | ||
11 | url: String, | ||
12 | publicKey: String, | ||
13 | score: { type: Number, max: constants.FRIEND_BASE_SCORE } | ||
14 | }) | ||
15 | var PodsDB = mongoose.model('pods', podsSchema) | ||
16 | |||
17 | // --------------------------------------------------------------------------- | ||
18 | |||
19 | var Pods = { | ||
20 | add: add, | ||
21 | count: count, | ||
22 | findByUrl: findByUrl, | ||
23 | findBadPods: findBadPods, | ||
24 | incrementScores: incrementScores, | ||
25 | list: list, | ||
26 | remove: remove, | ||
27 | removeAll: removeAll, | ||
28 | removeAllByIds: removeAllByIds | ||
29 | } | ||
30 | |||
31 | // TODO: check if the pod is not already a friend | ||
32 | function add (data, callback) { | ||
33 | if (!callback) callback = function () {} | ||
34 | var params = { | ||
35 | url: data.url, | ||
36 | publicKey: data.publicKey, | ||
37 | score: constants.FRIEND_BASE_SCORE | ||
38 | } | ||
39 | |||
40 | PodsDB.create(params, callback) | ||
41 | } | ||
42 | |||
43 | function count (callback) { | ||
44 | return PodsDB.count(callback) | ||
45 | } | ||
46 | |||
47 | function findBadPods (callback) { | ||
48 | PodsDB.find({ score: 0 }, callback) | ||
49 | } | ||
50 | |||
51 | function findByUrl (url, callback) { | ||
52 | PodsDB.findOne({ url: url }, callback) | ||
53 | } | ||
54 | |||
55 | function incrementScores (ids, value, callback) { | ||
56 | if (!callback) callback = function () {} | ||
57 | PodsDB.update({ _id: { $in: ids } }, { $inc: { score: value } }, { multi: true }, callback) | ||
58 | } | ||
59 | |||
60 | function list (callback) { | ||
61 | PodsDB.find(function (err, pods_list) { | ||
62 | if (err) { | ||
63 | logger.error('Cannot get the list of the pods.') | ||
64 | return callback(err) | ||
65 | } | ||
66 | |||
67 | return callback(null, pods_list) | ||
68 | }) | ||
69 | } | ||
70 | |||
71 | function remove (url, callback) { | ||
72 | if (!callback) callback = function () {} | ||
73 | PodsDB.remove({ url: url }, callback) | ||
74 | } | ||
75 | |||
76 | function removeAll (callback) { | ||
77 | if (!callback) callback = function () {} | ||
78 | PodsDB.remove(callback) | ||
79 | } | ||
80 | |||
81 | function removeAllByIds (ids, callback) { | ||
82 | if (!callback) callback = function () {} | ||
83 | PodsDB.remove({ _id: { $in: ids } }, callback) | ||
84 | } | ||
85 | |||
86 | // --------------------------------------------------------------------------- | ||
87 | |||
88 | module.exports = Pods | ||
diff --git a/server/models/poolRequests.js b/server/models/poolRequests.js new file mode 100644 index 000000000..970315597 --- /dev/null +++ b/server/models/poolRequests.js | |||
@@ -0,0 +1,55 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | var mongoose = require('mongoose') | ||
4 | |||
5 | var logger = require('../helpers/logger') | ||
6 | |||
7 | // --------------------------------------------------------------------------- | ||
8 | |||
9 | var poolRequestsSchema = mongoose.Schema({ | ||
10 | type: String, | ||
11 | id: String, // Special id to find duplicates (video created we want to remove...) | ||
12 | request: mongoose.Schema.Types.Mixed | ||
13 | }) | ||
14 | var PoolRequestsDB = mongoose.model('poolRequests', poolRequestsSchema) | ||
15 | |||
16 | // --------------------------------------------------------------------------- | ||
17 | |||
18 | var PoolRequests = { | ||
19 | create: create, | ||
20 | findById: findById, | ||
21 | list: list, | ||
22 | removeRequestById: removeRequestById, | ||
23 | removeRequests: removeRequests | ||
24 | } | ||
25 | |||
26 | function create (id, type, request, callback) { | ||
27 | PoolRequestsDB.create({ id: id, type: type, request: request }, callback) | ||
28 | } | ||
29 | |||
30 | function findById (id, callback) { | ||
31 | PoolRequestsDB.findOne({ id: id }, callback) | ||
32 | } | ||
33 | |||
34 | function list (callback) { | ||
35 | PoolRequestsDB.find({}, { _id: 1, type: 1, request: 1 }, callback) | ||
36 | } | ||
37 | |||
38 | function removeRequestById (id, callback) { | ||
39 | PoolRequestsDB.remove({ id: id }, callback) | ||
40 | } | ||
41 | |||
42 | function removeRequests (ids) { | ||
43 | PoolRequestsDB.remove({ _id: { $in: ids } }, function (err) { | ||
44 | if (err) { | ||
45 | logger.error('Cannot remove requests from the pool requests database.', { error: err }) | ||
46 | return // Abort | ||
47 | } | ||
48 | |||
49 | logger.info('Pool requests flushed.') | ||
50 | }) | ||
51 | } | ||
52 | |||
53 | // --------------------------------------------------------------------------- | ||
54 | |||
55 | module.exports = PoolRequests | ||
diff --git a/server/models/videos.js b/server/models/videos.js new file mode 100644 index 000000000..5e2eeae07 --- /dev/null +++ b/server/models/videos.js | |||
@@ -0,0 +1,234 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | var async = require('async') | ||
4 | var config = require('config') | ||
5 | var dz = require('dezalgo') | ||
6 | var fs = require('fs') | ||
7 | var mongoose = require('mongoose') | ||
8 | var path = require('path') | ||
9 | |||
10 | var logger = require('../helpers/logger') | ||
11 | |||
12 | var http = config.get('webserver.https') === true ? 'https' : 'http' | ||
13 | var host = config.get('webserver.host') | ||
14 | var port = config.get('webserver.port') | ||
15 | var uploadDir = path.join(__dirname, '..', config.get('storage.uploads')) | ||
16 | |||
17 | // --------------------------------------------------------------------------- | ||
18 | |||
19 | var videosSchema = mongoose.Schema({ | ||
20 | name: String, | ||
21 | namePath: String, | ||
22 | description: String, | ||
23 | magnetUri: String, | ||
24 | podUrl: String | ||
25 | }) | ||
26 | var VideosDB = mongoose.model('videos', videosSchema) | ||
27 | |||
28 | // --------------------------------------------------------------------------- | ||
29 | |||
30 | var Videos = { | ||
31 | add: add, | ||
32 | addRemotes: addRemotes, | ||
33 | get: get, | ||
34 | getVideoState: getVideoState, | ||
35 | isOwned: isOwned, | ||
36 | list: list, | ||
37 | listOwned: listOwned, | ||
38 | removeOwned: removeOwned, | ||
39 | removeAllRemotes: removeAllRemotes, | ||
40 | removeAllRemotesOf: removeAllRemotesOf, | ||
41 | removeRemotesOfByMagnetUris: removeRemotesOfByMagnetUris, | ||
42 | search: search | ||
43 | } | ||
44 | |||
45 | function add (video, callback) { | ||
46 | logger.info('Adding %s video to database.', video.name) | ||
47 | |||
48 | var params = video | ||
49 | params.podUrl = http + '://' + host + ':' + port | ||
50 | |||
51 | VideosDB.create(params, function (err, video) { | ||
52 | if (err) { | ||
53 | logger.error('Cannot insert this video into database.') | ||
54 | return callback(err) | ||
55 | } | ||
56 | |||
57 | callback(null) | ||
58 | }) | ||
59 | } | ||
60 | |||
61 | // TODO: avoid doublons | ||
62 | function addRemotes (videos, callback) { | ||
63 | if (!callback) callback = function () {} | ||
64 | |||
65 | var to_add = [] | ||
66 | |||
67 | async.each(videos, function (video, callback_each) { | ||
68 | callback_each = dz(callback_each) | ||
69 | logger.debug('Add remote video from pod: %s', video.podUrl) | ||
70 | |||
71 | var params = { | ||
72 | name: video.name, | ||
73 | namePath: null, | ||
74 | description: video.description, | ||
75 | magnetUri: video.magnetUri, | ||
76 | podUrl: video.podUrl | ||
77 | } | ||
78 | |||
79 | to_add.push(params) | ||
80 | |||
81 | callback_each() | ||
82 | }, function () { | ||
83 | VideosDB.create(to_add, function (err, videos) { | ||
84 | if (err) { | ||
85 | logger.error('Cannot insert this remote video.') | ||
86 | return callback(err) | ||
87 | } | ||
88 | |||
89 | return callback(null, videos) | ||
90 | }) | ||
91 | }) | ||
92 | } | ||
93 | |||
94 | function get (id, callback) { | ||
95 | VideosDB.findById(id, function (err, video) { | ||
96 | if (err) { | ||
97 | logger.error('Cannot get this video.') | ||
98 | return callback(err) | ||
99 | } | ||
100 | |||
101 | return callback(null, video) | ||
102 | }) | ||
103 | } | ||
104 | |||
105 | function getVideoState (id, callback) { | ||
106 | get(id, function (err, video) { | ||
107 | if (err) return callback(err) | ||
108 | |||
109 | var exist = (video !== null) | ||
110 | var owned = false | ||
111 | if (exist === true) { | ||
112 | owned = (video.namePath !== null) | ||
113 | } | ||
114 | |||
115 | return callback(null, { exist: exist, owned: owned }) | ||
116 | }) | ||
117 | } | ||
118 | |||
119 | function isOwned (id, callback) { | ||
120 | VideosDB.findById(id, function (err, video) { | ||
121 | if (err || !video) { | ||
122 | if (!err) err = new Error('Cannot find this video.') | ||
123 | logger.error('Cannot find this video.') | ||
124 | return callback(err) | ||
125 | } | ||
126 | |||
127 | if (video.namePath === null) { | ||
128 | var error_string = 'Cannot remove the video of another pod.' | ||
129 | logger.error(error_string) | ||
130 | return callback(new Error(error_string), false, video) | ||
131 | } | ||
132 | |||
133 | callback(null, true, video) | ||
134 | }) | ||
135 | } | ||
136 | |||
137 | function list (callback) { | ||
138 | VideosDB.find(function (err, videos_list) { | ||
139 | if (err) { | ||
140 | logger.error('Cannot get the list of the videos.') | ||
141 | return callback(err) | ||
142 | } | ||
143 | |||
144 | return callback(null, videos_list) | ||
145 | }) | ||
146 | } | ||
147 | |||
148 | function listOwned (callback) { | ||
149 | // If namePath is not null this is *our* video | ||
150 | VideosDB.find({ namePath: { $ne: null } }, function (err, videos_list) { | ||
151 | if (err) { | ||
152 | logger.error('Cannot get the list of owned videos.') | ||
153 | return callback(err) | ||
154 | } | ||
155 | |||
156 | return callback(null, videos_list) | ||
157 | }) | ||
158 | } | ||
159 | |||
160 | function removeOwned (id, callback) { | ||
161 | VideosDB.findByIdAndRemove(id, function (err, video) { | ||
162 | if (err) { | ||
163 | logger.error('Cannot remove the torrent.') | ||
164 | return callback(err) | ||
165 | } | ||
166 | |||
167 | fs.unlink(uploadDir + video.namePath, function (err) { | ||
168 | if (err) { | ||
169 | logger.error('Cannot remove this video file.') | ||
170 | return callback(err) | ||
171 | } | ||
172 | |||
173 | callback(null) | ||
174 | }) | ||
175 | }) | ||
176 | } | ||
177 | |||
178 | function removeAllRemotes (callback) { | ||
179 | VideosDB.remove({ namePath: null }, callback) | ||
180 | } | ||
181 | |||
182 | function removeAllRemotesOf (fromUrl, callback) { | ||
183 | VideosDB.remove({ podUrl: fromUrl }, callback) | ||
184 | } | ||
185 | |||
186 | // Use the magnet Uri because the _id field is not the same on different servers | ||
187 | function removeRemotesOfByMagnetUris (fromUrl, magnetUris, callback) { | ||
188 | if (callback === undefined) callback = function () {} | ||
189 | |||
190 | VideosDB.find({ magnetUri: { $in: magnetUris } }, function (err, videos) { | ||
191 | if (err || !videos) { | ||
192 | logger.error('Cannot find the torrent URI of these remote videos.') | ||
193 | return callback(err) | ||
194 | } | ||
195 | |||
196 | var to_remove = [] | ||
197 | async.each(videos, function (video, callback_async) { | ||
198 | callback_async = dz(callback_async) | ||
199 | |||
200 | if (video.podUrl !== fromUrl) { | ||
201 | logger.error('The pod %s has not the rights on the video of %s.', fromUrl, video.podUrl) | ||
202 | } else { | ||
203 | to_remove.push(video._id) | ||
204 | } | ||
205 | |||
206 | callback_async() | ||
207 | }, function () { | ||
208 | VideosDB.remove({ _id: { $in: to_remove } }, function (err) { | ||
209 | if (err) { | ||
210 | logger.error('Cannot remove the remote videos.') | ||
211 | return callback(err) | ||
212 | } | ||
213 | |||
214 | logger.info('Removed remote videos from %s.', fromUrl) | ||
215 | callback(null) | ||
216 | }) | ||
217 | }) | ||
218 | }) | ||
219 | } | ||
220 | |||
221 | function search (name, callback) { | ||
222 | VideosDB.find({ name: new RegExp(name) }, function (err, videos) { | ||
223 | if (err) { | ||
224 | logger.error('Cannot search the videos.') | ||
225 | return callback(err) | ||
226 | } | ||
227 | |||
228 | return callback(null, videos) | ||
229 | }) | ||
230 | } | ||
231 | |||
232 | // --------------------------------------------------------------------------- | ||
233 | |||
234 | module.exports = Videos | ||