]>
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 C |
24 | podUrl: String, |
25 | author: String | |
9f10b292 | 26 | }) |
f0f5567b | 27 | const VideosDB = mongoose.model('videos', videosSchema) |
9f10b292 C |
28 | |
29 | // --------------------------------------------------------------------------- | |
30 | ||
f0f5567b | 31 | const Videos = { |
9f10b292 C |
32 | add: add, |
33 | addRemotes: addRemotes, | |
34 | get: get, | |
9f10b292 C |
35 | list: list, |
36 | listOwned: listOwned, | |
37 | removeOwned: removeOwned, | |
38 | removeAllRemotes: removeAllRemotes, | |
39 | removeAllRemotesOf: removeAllRemotesOf, | |
40 | removeRemotesOfByMagnetUris: removeRemotesOfByMagnetUris, | |
41 | search: search | |
42 | } | |
43 | ||
44 | function add (video, callback) { | |
45 | logger.info('Adding %s video to database.', video.name) | |
46 | ||
f0f5567b | 47 | const params = video |
9f10b292 C |
48 | params.podUrl = http + '://' + host + ':' + port |
49 | ||
50 | VideosDB.create(params, function (err, video) { | |
51 | if (err) { | |
52 | logger.error('Cannot insert this video into database.') | |
53 | return callback(err) | |
54 | } | |
55 | ||
56 | callback(null) | |
c173e565 | 57 | }) |
9f10b292 | 58 | } |
8c308c2b | 59 | |
9f10b292 C |
60 | // TODO: avoid doublons |
61 | function addRemotes (videos, callback) { | |
62 | if (!callback) callback = function () {} | |
c45f7f84 | 63 | |
f0f5567b | 64 | const to_add = [] |
c45f7f84 | 65 | |
9f10b292 C |
66 | async.each(videos, function (video, callback_each) { |
67 | callback_each = dz(callback_each) | |
68 | logger.debug('Add remote video from pod: %s', video.podUrl) | |
c45f7f84 | 69 | |
f0f5567b | 70 | const params = { |
9f10b292 C |
71 | name: video.name, |
72 | namePath: null, | |
73 | description: video.description, | |
74 | magnetUri: video.magnetUri, | |
75 | podUrl: video.podUrl | |
76 | } | |
c45f7f84 | 77 | |
9f10b292 | 78 | to_add.push(params) |
c45f7f84 | 79 | |
9f10b292 C |
80 | callback_each() |
81 | }, function () { | |
82 | VideosDB.create(to_add, function (err, videos) { | |
c45f7f84 | 83 | if (err) { |
9f10b292 | 84 | logger.error('Cannot insert this remote video.') |
c45f7f84 C |
85 | return callback(err) |
86 | } | |
87 | ||
9f10b292 | 88 | return callback(null, videos) |
c45f7f84 | 89 | }) |
9f10b292 C |
90 | }) |
91 | } | |
c45f7f84 | 92 | |
9f10b292 C |
93 | function get (id, callback) { |
94 | VideosDB.findById(id, function (err, video) { | |
95 | if (err) { | |
96 | logger.error('Cannot get this video.') | |
97 | return callback(err) | |
98 | } | |
c173e565 | 99 | |
9f10b292 C |
100 | return callback(null, video) |
101 | }) | |
102 | } | |
c173e565 | 103 | |
9f10b292 C |
104 | function list (callback) { |
105 | VideosDB.find(function (err, videos_list) { | |
106 | if (err) { | |
107 | logger.error('Cannot get the list of the videos.') | |
108 | return callback(err) | |
109 | } | |
c173e565 | 110 | |
9f10b292 C |
111 | return callback(null, videos_list) |
112 | }) | |
113 | } | |
c45f7f84 | 114 | |
9f10b292 C |
115 | function listOwned (callback) { |
116 | // If namePath is not null this is *our* video | |
117 | VideosDB.find({ namePath: { $ne: null } }, function (err, videos_list) { | |
118 | if (err) { | |
119 | logger.error('Cannot get the list of owned videos.') | |
120 | return callback(err) | |
121 | } | |
122 | ||
123 | return callback(null, videos_list) | |
124 | }) | |
125 | } | |
126 | ||
127 | function removeOwned (id, callback) { | |
128 | VideosDB.findByIdAndRemove(id, function (err, video) { | |
129 | if (err) { | |
130 | logger.error('Cannot remove the torrent.') | |
131 | return callback(err) | |
132 | } | |
c45f7f84 | 133 | |
9f10b292 | 134 | fs.unlink(uploadDir + video.namePath, function (err) { |
c45f7f84 | 135 | if (err) { |
9f10b292 | 136 | logger.error('Cannot remove this video file.') |
c45f7f84 C |
137 | return callback(err) |
138 | } | |
139 | ||
9f10b292 | 140 | callback(null) |
c45f7f84 | 141 | }) |
9f10b292 C |
142 | }) |
143 | } | |
144 | ||
145 | function removeAllRemotes (callback) { | |
146 | VideosDB.remove({ namePath: null }, callback) | |
147 | } | |
148 | ||
149 | function removeAllRemotesOf (fromUrl, callback) { | |
9f10b292 C |
150 | VideosDB.remove({ podUrl: fromUrl }, callback) |
151 | } | |
152 | ||
153 | // Use the magnet Uri because the _id field is not the same on different servers | |
154 | function removeRemotesOfByMagnetUris (fromUrl, magnetUris, callback) { | |
155 | if (callback === undefined) callback = function () {} | |
156 | ||
157 | VideosDB.find({ magnetUri: { $in: magnetUris } }, function (err, videos) { | |
158 | if (err || !videos) { | |
159 | logger.error('Cannot find the torrent URI of these remote videos.') | |
160 | return callback(err) | |
161 | } | |
162 | ||
f0f5567b | 163 | const to_remove = [] |
9f10b292 C |
164 | async.each(videos, function (video, callback_async) { |
165 | callback_async = dz(callback_async) | |
166 | ||
167 | if (video.podUrl !== fromUrl) { | |
168 | logger.error('The pod %s has not the rights on the video of %s.', fromUrl, video.podUrl) | |
169 | } else { | |
170 | to_remove.push(video._id) | |
8c308c2b C |
171 | } |
172 | ||
9f10b292 C |
173 | callback_async() |
174 | }, function () { | |
175 | VideosDB.remove({ _id: { $in: to_remove } }, function (err) { | |
c173e565 | 176 | if (err) { |
9f10b292 | 177 | logger.error('Cannot remove the remote videos.') |
c173e565 C |
178 | return callback(err) |
179 | } | |
8c308c2b | 180 | |
9f10b292 | 181 | logger.info('Removed remote videos from %s.', fromUrl) |
c173e565 | 182 | callback(null) |
8c308c2b C |
183 | }) |
184 | }) | |
9f10b292 C |
185 | }) |
186 | } | |
8c308c2b | 187 | |
9f10b292 C |
188 | function search (name, callback) { |
189 | VideosDB.find({ name: new RegExp(name) }, function (err, videos) { | |
190 | if (err) { | |
191 | logger.error('Cannot search the videos.') | |
192 | return callback(err) | |
193 | } | |
8c308c2b | 194 | |
9f10b292 C |
195 | return callback(null, videos) |
196 | }) | |
197 | } | |
8c308c2b | 198 | |
9f10b292 | 199 | // --------------------------------------------------------------------------- |
c45f7f84 | 200 | |
9f10b292 | 201 | module.exports = Videos |