diff options
Diffstat (limited to 'server/models/videos.js')
-rw-r--r-- | server/models/videos.js | 126 |
1 files changed, 32 insertions, 94 deletions
diff --git a/server/models/videos.js b/server/models/videos.js index e02158be7..eedb6eb58 100644 --- a/server/models/videos.js +++ b/server/models/videos.js | |||
@@ -1,18 +1,13 @@ | |||
1 | 'use strict' | 1 | 'use strict' |
2 | 2 | ||
3 | const async = require('async') | ||
4 | const config = require('config') | 3 | const config = require('config') |
5 | const dz = require('dezalgo') | ||
6 | const fs = require('fs') | ||
7 | const mongoose = require('mongoose') | 4 | const mongoose = require('mongoose') |
8 | const path = require('path') | ||
9 | 5 | ||
10 | const logger = require('../helpers/logger') | 6 | const logger = require('../helpers/logger') |
11 | 7 | ||
12 | const http = config.get('webserver.https') === true ? 'https' : 'http' | 8 | const http = config.get('webserver.https') === true ? 'https' : 'http' |
13 | const host = config.get('webserver.host') | 9 | const host = config.get('webserver.host') |
14 | const port = config.get('webserver.port') | 10 | const port = config.get('webserver.port') |
15 | const uploadDir = path.join(__dirname, '..', '..', config.get('storage.uploads')) | ||
16 | 11 | ||
17 | // --------------------------------------------------------------------------- | 12 | // --------------------------------------------------------------------------- |
18 | 13 | ||
@@ -23,7 +18,8 @@ const videosSchema = mongoose.Schema({ | |||
23 | magnetUri: String, | 18 | magnetUri: String, |
24 | podUrl: String, | 19 | podUrl: String, |
25 | author: String, | 20 | author: String, |
26 | duration: Number | 21 | duration: Number, |
22 | thumbnail: String | ||
27 | }) | 23 | }) |
28 | const VideosDB = mongoose.model('videos', videosSchema) | 24 | const VideosDB = mongoose.model('videos', videosSchema) |
29 | 25 | ||
@@ -34,11 +30,13 @@ const Videos = { | |||
34 | addRemotes: addRemotes, | 30 | addRemotes: addRemotes, |
35 | get: get, | 31 | get: get, |
36 | list: list, | 32 | list: list, |
33 | listFromUrl: listFromUrl, | ||
34 | listFromUrls: listFromUrls, | ||
35 | listFromUrlAndMagnets: listFromUrlAndMagnets, | ||
36 | listFromRemotes: listFromRemotes, | ||
37 | listOwned: listOwned, | 37 | listOwned: listOwned, |
38 | removeOwned: removeOwned, | 38 | removeOwned: removeOwned, |
39 | removeAllRemotes: removeAllRemotes, | 39 | removeByIds: removeByIds, |
40 | removeAllRemotesOf: removeAllRemotesOf, | ||
41 | removeRemotesOfByMagnetUris: removeRemotesOfByMagnetUris, | ||
42 | search: search | 40 | search: search |
43 | } | 41 | } |
44 | 42 | ||
@@ -58,38 +56,13 @@ function add (video, callback) { | |||
58 | }) | 56 | }) |
59 | } | 57 | } |
60 | 58 | ||
61 | // TODO: avoid doublons | ||
62 | function addRemotes (videos, callback) { | 59 | function addRemotes (videos, callback) { |
63 | if (!callback) callback = function () {} | 60 | videos.forEach(function (video) { |
64 | 61 | // Ensure they are remote videos | |
65 | const to_add = [] | 62 | video.namePath = null |
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 | const params = { | ||
72 | name: video.name, | ||
73 | namePath: null, | ||
74 | description: video.description, | ||
75 | magnetUri: video.magnetUri, | ||
76 | podUrl: video.podUrl, | ||
77 | duration: video.duration | ||
78 | } | ||
79 | |||
80 | to_add.push(params) | ||
81 | |||
82 | callback_each() | ||
83 | }, function () { | ||
84 | VideosDB.create(to_add, function (err, videos) { | ||
85 | if (err) { | ||
86 | logger.error('Cannot insert this remote video.') | ||
87 | return callback(err) | ||
88 | } | ||
89 | |||
90 | return callback(null, videos) | ||
91 | }) | ||
92 | }) | 63 | }) |
64 | |||
65 | VideosDB.create(videos, callback) | ||
93 | } | 66 | } |
94 | 67 | ||
95 | function get (id, callback) { | 68 | function get (id, callback) { |
@@ -114,6 +87,22 @@ function list (callback) { | |||
114 | }) | 87 | }) |
115 | } | 88 | } |
116 | 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 | |||
117 | function listOwned (callback) { | 106 | function listOwned (callback) { |
118 | // If namePath is not null this is *our* video | 107 | // If namePath is not null this is *our* video |
119 | VideosDB.find({ namePath: { $ne: null } }, function (err, videos_list) { | 108 | VideosDB.find({ namePath: { $ne: null } }, function (err, videos_list) { |
@@ -126,65 +115,14 @@ function listOwned (callback) { | |||
126 | }) | 115 | }) |
127 | } | 116 | } |
128 | 117 | ||
118 | // Return the video in the callback | ||
129 | function removeOwned (id, callback) { | 119 | function removeOwned (id, callback) { |
130 | VideosDB.findByIdAndRemove(id, function (err, video) { | 120 | VideosDB.findByIdAndRemove(id, callback) |
131 | if (err) { | ||
132 | logger.error('Cannot remove the torrent.') | ||
133 | return callback(err) | ||
134 | } | ||
135 | |||
136 | fs.unlink(uploadDir + video.namePath, function (err) { | ||
137 | if (err) { | ||
138 | logger.error('Cannot remove this video file.') | ||
139 | return callback(err) | ||
140 | } | ||
141 | |||
142 | callback(null) | ||
143 | }) | ||
144 | }) | ||
145 | } | ||
146 | |||
147 | function removeAllRemotes (callback) { | ||
148 | VideosDB.remove({ namePath: null }, callback) | ||
149 | } | ||
150 | |||
151 | function removeAllRemotesOf (fromUrl, callback) { | ||
152 | VideosDB.remove({ podUrl: fromUrl }, callback) | ||
153 | } | 121 | } |
154 | 122 | ||
155 | // Use the magnet Uri because the _id field is not the same on different servers | 123 | // Use the magnet Uri because the _id field is not the same on different servers |
156 | function removeRemotesOfByMagnetUris (fromUrl, magnetUris, callback) { | 124 | function removeByIds (ids, callback) { |
157 | if (callback === undefined) callback = function () {} | 125 | VideosDB.remove({ _id: { $in: ids } }, callback) |
158 | |||
159 | VideosDB.find({ magnetUri: { $in: magnetUris } }, function (err, videos) { | ||
160 | if (err || !videos) { | ||
161 | logger.error('Cannot find the torrent URI of these remote videos.') | ||
162 | return callback(err) | ||
163 | } | ||
164 | |||
165 | const to_remove = [] | ||
166 | async.each(videos, function (video, callback_async) { | ||
167 | callback_async = dz(callback_async) | ||
168 | |||
169 | if (video.podUrl !== fromUrl) { | ||
170 | logger.error('The pod %s has not the rights on the video of %s.', fromUrl, video.podUrl) | ||
171 | } else { | ||
172 | to_remove.push(video._id) | ||
173 | } | ||
174 | |||
175 | callback_async() | ||
176 | }, function () { | ||
177 | VideosDB.remove({ _id: { $in: to_remove } }, function (err) { | ||
178 | if (err) { | ||
179 | logger.error('Cannot remove the remote videos.') | ||
180 | return callback(err) | ||
181 | } | ||
182 | |||
183 | logger.info('Removed remote videos from %s.', fromUrl) | ||
184 | callback(null) | ||
185 | }) | ||
186 | }) | ||
187 | }) | ||
188 | } | 126 | } |
189 | 127 | ||
190 | function search (name, callback) { | 128 | function search (name, callback) { |