]>
Commit | Line | Data |
---|---|---|
9f10b292 C |
1 | 'use strict' |
2 | ||
f0f5567b C |
3 | const async = require('async') |
4 | const config = require('config') | |
5 | const dz = require('dezalgo') | |
6 | const fs = require('fs') | |
7 | const mongoose = require('mongoose') | |
8 | const path = require('path') | |
9f10b292 | 9 | |
f0f5567b | 10 | const logger = require('../helpers/logger') |
9f10b292 | 11 | |
f0f5567b C |
12 | const http = config.get('webserver.https') === true ? 'https' : 'http' |
13 | const host = config.get('webserver.host') | |
14 | const port = config.get('webserver.port') | |
15 | const uploadDir = path.join(__dirname, '..', '..', config.get('storage.uploads')) | |
9f10b292 C |
16 | |
17 | // --------------------------------------------------------------------------- | |
18 | ||
f0f5567b | 19 | const videosSchema = mongoose.Schema({ |
9f10b292 C |
20 | name: String, |
21 | namePath: String, | |
22 | description: String, | |
23 | magnetUri: String, | |
0c1cbbfe | 24 | podUrl: String, |
3a8a8b51 C |
25 | author: String, |
26 | duration: Number | |
9f10b292 | 27 | }) |
f0f5567b | 28 | const VideosDB = mongoose.model('videos', videosSchema) |
9f10b292 C |
29 | |
30 | // --------------------------------------------------------------------------- | |
31 | ||
f0f5567b | 32 | const Videos = { |
9f10b292 C |
33 | add: add, |
34 | addRemotes: addRemotes, | |
35 | get: get, | |
9f10b292 C |
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 | ||
f0f5567b | 48 | const params = video |
9f10b292 C |
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) | |
c173e565 | 58 | }) |
9f10b292 | 59 | } |
8c308c2b | 60 | |
9f10b292 C |
61 | // TODO: avoid doublons |
62 | function addRemotes (videos, callback) { | |
63 | if (!callback) callback = function () {} | |
c45f7f84 | 64 | |
f0f5567b | 65 | const to_add = [] |
c45f7f84 | 66 | |
9f10b292 C |
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) | |
c45f7f84 | 70 | |
f0f5567b | 71 | const params = { |
9f10b292 C |
72 | name: video.name, |
73 | namePath: null, | |
74 | description: video.description, | |
75 | magnetUri: video.magnetUri, | |
3a8a8b51 C |
76 | podUrl: video.podUrl, |
77 | duration: video.duration | |
9f10b292 | 78 | } |
c45f7f84 | 79 | |
9f10b292 | 80 | to_add.push(params) |
c45f7f84 | 81 | |
9f10b292 C |
82 | callback_each() |
83 | }, function () { | |
84 | VideosDB.create(to_add, function (err, videos) { | |
c45f7f84 | 85 | if (err) { |
9f10b292 | 86 | logger.error('Cannot insert this remote video.') |
c45f7f84 C |
87 | return callback(err) |
88 | } | |
89 | ||
9f10b292 | 90 | return callback(null, videos) |
c45f7f84 | 91 | }) |
9f10b292 C |
92 | }) |
93 | } | |
c45f7f84 | 94 | |
9f10b292 C |
95 | function get (id, callback) { |
96 | VideosDB.findById(id, function (err, video) { | |
97 | if (err) { | |
98 | logger.error('Cannot get this video.') | |
99 | return callback(err) | |
100 | } | |
c173e565 | 101 | |
9f10b292 C |
102 | return callback(null, video) |
103 | }) | |
104 | } | |
c173e565 | 105 | |
9f10b292 C |
106 | function list (callback) { |
107 | VideosDB.find(function (err, videos_list) { | |
108 | if (err) { | |
109 | logger.error('Cannot get the list of the videos.') | |
110 | return callback(err) | |
111 | } | |
c173e565 | 112 | |
9f10b292 C |
113 | return callback(null, videos_list) |
114 | }) | |
115 | } | |
c45f7f84 | 116 | |
9f10b292 C |
117 | function listOwned (callback) { |
118 | // If namePath is not null this is *our* video | |
119 | VideosDB.find({ namePath: { $ne: null } }, function (err, videos_list) { | |
120 | if (err) { | |
121 | logger.error('Cannot get the list of owned videos.') | |
122 | return callback(err) | |
123 | } | |
124 | ||
125 | return callback(null, videos_list) | |
126 | }) | |
127 | } | |
128 | ||
129 | function removeOwned (id, callback) { | |
130 | VideosDB.findByIdAndRemove(id, function (err, video) { | |
131 | if (err) { | |
132 | logger.error('Cannot remove the torrent.') | |
133 | return callback(err) | |
134 | } | |
c45f7f84 | 135 | |
9f10b292 | 136 | fs.unlink(uploadDir + video.namePath, function (err) { |
c45f7f84 | 137 | if (err) { |
9f10b292 | 138 | logger.error('Cannot remove this video file.') |
c45f7f84 C |
139 | return callback(err) |
140 | } | |
141 | ||
9f10b292 | 142 | callback(null) |
c45f7f84 | 143 | }) |
9f10b292 C |
144 | }) |
145 | } | |
146 | ||
147 | function removeAllRemotes (callback) { | |
148 | VideosDB.remove({ namePath: null }, callback) | |
149 | } | |
150 | ||
151 | function removeAllRemotesOf (fromUrl, callback) { | |
9f10b292 C |
152 | VideosDB.remove({ podUrl: fromUrl }, callback) |
153 | } | |
154 | ||
155 | // Use the magnet Uri because the _id field is not the same on different servers | |
156 | function removeRemotesOfByMagnetUris (fromUrl, magnetUris, callback) { | |
157 | if (callback === undefined) callback = function () {} | |
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 | ||
f0f5567b | 165 | const to_remove = [] |
9f10b292 C |
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) | |
8c308c2b C |
173 | } |
174 | ||
9f10b292 C |
175 | callback_async() |
176 | }, function () { | |
177 | VideosDB.remove({ _id: { $in: to_remove } }, function (err) { | |
c173e565 | 178 | if (err) { |
9f10b292 | 179 | logger.error('Cannot remove the remote videos.') |
c173e565 C |
180 | return callback(err) |
181 | } | |
8c308c2b | 182 | |
9f10b292 | 183 | logger.info('Removed remote videos from %s.', fromUrl) |
c173e565 | 184 | callback(null) |
8c308c2b C |
185 | }) |
186 | }) | |
9f10b292 C |
187 | }) |
188 | } | |
8c308c2b | 189 | |
9f10b292 C |
190 | function search (name, callback) { |
191 | VideosDB.find({ name: new RegExp(name) }, function (err, videos) { | |
192 | if (err) { | |
193 | logger.error('Cannot search the videos.') | |
194 | return callback(err) | |
195 | } | |
8c308c2b | 196 | |
9f10b292 C |
197 | return callback(null, videos) |
198 | }) | |
199 | } | |
8c308c2b | 200 | |
9f10b292 | 201 | // --------------------------------------------------------------------------- |
c45f7f84 | 202 | |
9f10b292 | 203 | module.exports = Videos |