]>
Commit | Line | Data |
---|---|---|
aaf61f38 C |
1 | 'use strict' |
2 | ||
052937db | 3 | const createTorrent = require('create-torrent') |
aaf61f38 C |
4 | const ffmpeg = require('fluent-ffmpeg') |
5 | const fs = require('fs') | |
f285faa0 | 6 | const magnetUtil = require('magnet-uri') |
1a42c9e2 | 7 | const parallel = require('async/parallel') |
052937db | 8 | const parseTorrent = require('parse-torrent') |
aaf61f38 C |
9 | const pathUtils = require('path') |
10 | const mongoose = require('mongoose') | |
11 | ||
12 | const constants = require('../initializers/constants') | |
e4c55619 | 13 | const customVideosValidators = require('../helpers/custom-validators').videos |
aaf61f38 | 14 | const logger = require('../helpers/logger') |
0ff21c1c | 15 | const modelUtils = require('./utils') |
aaf61f38 | 16 | |
aaf61f38 C |
17 | // --------------------------------------------------------------------------- |
18 | ||
19 | // TODO: add indexes on searchable columns | |
20 | const VideoSchema = mongoose.Schema({ | |
21 | name: String, | |
558d7c23 C |
22 | extname: { |
23 | type: String, | |
24 | enum: [ '.mp4', '.webm', '.ogv' ] | |
25 | }, | |
26 | remoteId: mongoose.Schema.Types.ObjectId, | |
aaf61f38 | 27 | description: String, |
f285faa0 C |
28 | magnet: { |
29 | infoHash: String | |
30 | }, | |
49abbbbe | 31 | podHost: String, |
aaf61f38 C |
32 | author: String, |
33 | duration: Number, | |
aaf61f38 C |
34 | tags: [ String ], |
35 | createdDate: { | |
36 | type: Date, | |
37 | default: Date.now | |
38 | } | |
39 | }) | |
40 | ||
e4c55619 C |
41 | VideoSchema.path('name').validate(customVideosValidators.isVideoNameValid) |
42 | VideoSchema.path('description').validate(customVideosValidators.isVideoDescriptionValid) | |
49abbbbe | 43 | VideoSchema.path('podHost').validate(customVideosValidators.isVideoPodHostValid) |
e4c55619 C |
44 | VideoSchema.path('author').validate(customVideosValidators.isVideoAuthorValid) |
45 | VideoSchema.path('duration').validate(customVideosValidators.isVideoDurationValid) | |
e4c55619 | 46 | VideoSchema.path('tags').validate(customVideosValidators.isVideoTagsValid) |
aaf61f38 C |
47 | |
48 | VideoSchema.methods = { | |
f285faa0 C |
49 | generateMagnetUri, |
50 | getVideoFilename, | |
51 | getThumbnailName, | |
52 | getPreviewName, | |
558d7c23 | 53 | getTorrentName, |
c4403b29 C |
54 | isOwned, |
55 | toFormatedJSON, | |
56 | toRemoteJSON | |
aaf61f38 C |
57 | } |
58 | ||
59 | VideoSchema.statics = { | |
c4403b29 C |
60 | getDurationFromFile, |
61 | listForApi, | |
49abbbbe C |
62 | listByHostAndRemoteId, |
63 | listByHost, | |
c4403b29 C |
64 | listOwned, |
65 | listOwnedByAuthor, | |
66 | listRemotes, | |
67 | load, | |
a6375e69 | 68 | search |
aaf61f38 C |
69 | } |
70 | ||
71 | VideoSchema.pre('remove', function (next) { | |
72 | const video = this | |
73 | const tasks = [] | |
74 | ||
75 | tasks.push( | |
76 | function (callback) { | |
77 | removeThumbnail(video, callback) | |
78 | } | |
79 | ) | |
80 | ||
81 | if (video.isOwned()) { | |
82 | tasks.push( | |
83 | function (callback) { | |
84 | removeFile(video, callback) | |
85 | }, | |
86 | function (callback) { | |
87 | removeTorrent(video, callback) | |
6a94a109 C |
88 | }, |
89 | function (callback) { | |
90 | removePreview(video, callback) | |
aaf61f38 C |
91 | } |
92 | ) | |
93 | } | |
94 | ||
1a42c9e2 | 95 | parallel(tasks, next) |
aaf61f38 C |
96 | }) |
97 | ||
98 | VideoSchema.pre('save', function (next) { | |
99 | const video = this | |
100 | const tasks = [] | |
101 | ||
102 | if (video.isOwned()) { | |
f285faa0 | 103 | const videoPath = pathUtils.join(constants.CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename()) |
49abbbbe | 104 | this.podHost = constants.CONFIG.WEBSERVER.HOST |
aaf61f38 C |
105 | |
106 | tasks.push( | |
052937db | 107 | // TODO: refractoring |
aaf61f38 | 108 | function (callback) { |
25cad919 C |
109 | const options = { |
110 | announceList: [ | |
3737bbaf | 111 | [ constants.CONFIG.WEBSERVER.WS + '://' + constants.CONFIG.WEBSERVER.HOSTNAME + ':' + constants.CONFIG.WEBSERVER.PORT + '/tracker/socket' ] |
25cad919 C |
112 | ], |
113 | urlList: [ | |
f285faa0 | 114 | constants.CONFIG.WEBSERVER.URL + constants.STATIC_PATHS.WEBSEED + video.getVideoFilename() |
25cad919 C |
115 | ] |
116 | } | |
117 | ||
118 | createTorrent(videoPath, options, function (err, torrent) { | |
052937db C |
119 | if (err) return callback(err) |
120 | ||
558d7c23 | 121 | fs.writeFile(constants.CONFIG.STORAGE.TORRENTS_DIR + video.getTorrentName(), torrent, function (err) { |
052937db C |
122 | if (err) return callback(err) |
123 | ||
124 | const parsedTorrent = parseTorrent(torrent) | |
f285faa0 | 125 | video.magnet.infoHash = parsedTorrent.infoHash |
052937db C |
126 | |
127 | callback(null) | |
128 | }) | |
129 | }) | |
aaf61f38 C |
130 | }, |
131 | function (callback) { | |
558d7c23 | 132 | createThumbnail(video, videoPath, callback) |
6a94a109 C |
133 | }, |
134 | function (callback) { | |
558d7c23 | 135 | createPreview(video, videoPath, callback) |
aaf61f38 C |
136 | } |
137 | ) | |
138 | ||
558d7c23 | 139 | parallel(tasks, next) |
aaf61f38 | 140 | } else { |
558d7c23 | 141 | generateThumbnailFromBase64(video, video.thumbnail, next) |
aaf61f38 C |
142 | } |
143 | }) | |
144 | ||
145 | mongoose.model('Video', VideoSchema) | |
146 | ||
147 | // ------------------------------ METHODS ------------------------------ | |
148 | ||
f285faa0 C |
149 | function generateMagnetUri () { |
150 | let baseUrlHttp, baseUrlWs | |
151 | ||
152 | if (this.isOwned()) { | |
153 | baseUrlHttp = constants.CONFIG.WEBSERVER.URL | |
154 | baseUrlWs = constants.CONFIG.WEBSERVER.WS + '://' + constants.CONFIG.WEBSERVER.HOSTNAME + ':' + constants.CONFIG.WEBSERVER.PORT | |
155 | } else { | |
49abbbbe C |
156 | baseUrlHttp = constants.REMOTE_SCHEME.HTTP + '://' + this.podHost |
157 | baseUrlWs = constants.REMOTE_SCHEME.WS + this.podHost | |
f285faa0 C |
158 | } |
159 | ||
160 | const xs = baseUrlHttp + constants.STATIC_PATHS.TORRENTS + this.getTorrentName() | |
161 | const announce = baseUrlWs + '/tracker/socket' | |
162 | const urlList = [ baseUrlHttp + constants.STATIC_PATHS.WEBSEED + this.getVideoFilename() ] | |
163 | ||
164 | const magnetHash = { | |
165 | xs, | |
166 | announce, | |
167 | urlList, | |
168 | infoHash: this.magnet.infoHash, | |
169 | name: this.name | |
170 | } | |
171 | ||
172 | return magnetUtil.encode(magnetHash) | |
558d7c23 C |
173 | } |
174 | ||
f285faa0 C |
175 | function getVideoFilename () { |
176 | if (this.isOwned()) return this._id + this.extname | |
177 | ||
178 | return this.remoteId + this.extname | |
179 | } | |
180 | ||
181 | function getThumbnailName () { | |
182 | // We always have a copy of the thumbnail | |
558d7c23 C |
183 | return this._id + '.jpg' |
184 | } | |
185 | ||
f285faa0 C |
186 | function getPreviewName () { |
187 | const extension = '.jpg' | |
188 | ||
189 | if (this.isOwned()) return this._id + extension | |
190 | ||
191 | return this.remoteId + extension | |
192 | } | |
193 | ||
558d7c23 | 194 | function getTorrentName () { |
f285faa0 C |
195 | const extension = '.torrent' |
196 | ||
197 | if (this.isOwned()) return this._id + extension | |
198 | ||
199 | return this.remoteId + extension | |
558d7c23 C |
200 | } |
201 | ||
aaf61f38 | 202 | function isOwned () { |
558d7c23 | 203 | return this.remoteId === null |
aaf61f38 C |
204 | } |
205 | ||
206 | function toFormatedJSON () { | |
207 | const json = { | |
208 | id: this._id, | |
209 | name: this.name, | |
210 | description: this.description, | |
49abbbbe | 211 | podHost: this.podHost, |
aaf61f38 | 212 | isLocal: this.isOwned(), |
f285faa0 | 213 | magnetUri: this.generateMagnetUri(), |
aaf61f38 C |
214 | author: this.author, |
215 | duration: this.duration, | |
216 | tags: this.tags, | |
f285faa0 | 217 | thumbnailPath: constants.STATIC_PATHS.THUMBNAILS + '/' + this.getThumbnailName(), |
aaf61f38 C |
218 | createdDate: this.createdDate |
219 | } | |
220 | ||
221 | return json | |
222 | } | |
223 | ||
224 | function toRemoteJSON (callback) { | |
225 | const self = this | |
226 | ||
227 | // Convert thumbnail to base64 | |
f285faa0 | 228 | const thumbnailPath = pathUtils.join(constants.CONFIG.STORAGE.THUMBNAILS_DIR, this.getThumbnailName()) |
558d7c23 | 229 | fs.readFile(thumbnailPath, function (err, thumbnailData) { |
aaf61f38 C |
230 | if (err) { |
231 | logger.error('Cannot read the thumbnail of the video') | |
232 | return callback(err) | |
233 | } | |
234 | ||
235 | const remoteVideo = { | |
236 | name: self.name, | |
237 | description: self.description, | |
f285faa0 | 238 | magnet: self.magnet, |
558d7c23 | 239 | remoteId: self._id, |
aaf61f38 C |
240 | author: self.author, |
241 | duration: self.duration, | |
242 | thumbnailBase64: new Buffer(thumbnailData).toString('base64'), | |
243 | tags: self.tags, | |
244 | createdDate: self.createdDate, | |
49abbbbe | 245 | podHost: self.podHost |
aaf61f38 C |
246 | } |
247 | ||
248 | return callback(null, remoteVideo) | |
249 | }) | |
250 | } | |
251 | ||
252 | // ------------------------------ STATICS ------------------------------ | |
253 | ||
254 | function getDurationFromFile (videoPath, callback) { | |
255 | ffmpeg.ffprobe(videoPath, function (err, metadata) { | |
256 | if (err) return callback(err) | |
257 | ||
258 | return callback(null, Math.floor(metadata.format.duration)) | |
259 | }) | |
260 | } | |
261 | ||
0ff21c1c | 262 | function listForApi (start, count, sort, callback) { |
aaf61f38 | 263 | const query = {} |
5c39adb7 | 264 | return modelUtils.listForApiWithCount.call(this, query, start, count, sort, callback) |
aaf61f38 C |
265 | } |
266 | ||
49abbbbe C |
267 | function listByHostAndRemoteId (fromHost, remoteId, callback) { |
268 | this.find({ podHost: fromHost, remoteId: remoteId }, callback) | |
aaf61f38 C |
269 | } |
270 | ||
49abbbbe C |
271 | function listByHost (fromHost, callback) { |
272 | this.find({ podHost: fromHost }, callback) | |
aaf61f38 C |
273 | } |
274 | ||
275 | function listOwned (callback) { | |
558d7c23 C |
276 | // If remoteId is null this is *our* video |
277 | this.find({ remoteId: null }, callback) | |
aaf61f38 C |
278 | } |
279 | ||
9bd26629 | 280 | function listOwnedByAuthor (author, callback) { |
558d7c23 | 281 | this.find({ remoteId: null, author: author }, callback) |
9bd26629 C |
282 | } |
283 | ||
aaf61f38 | 284 | function listRemotes (callback) { |
558d7c23 | 285 | this.find({ remoteId: { $ne: null } }, callback) |
aaf61f38 C |
286 | } |
287 | ||
288 | function load (id, callback) { | |
289 | this.findById(id, callback) | |
290 | } | |
291 | ||
292 | function search (value, field, start, count, sort, callback) { | |
293 | const query = {} | |
294 | // Make an exact search with the magnet | |
55723d16 C |
295 | if (field === 'magnetUri') { |
296 | const infoHash = magnetUtil.decode(value).infoHash | |
297 | query.magnet = { | |
298 | infoHash | |
299 | } | |
300 | } else if (field === 'tags') { | |
aaf61f38 C |
301 | query[field] = value |
302 | } else { | |
cf6412e8 | 303 | query[field] = new RegExp(value, 'i') |
aaf61f38 C |
304 | } |
305 | ||
5c39adb7 | 306 | modelUtils.listForApiWithCount.call(this, query, start, count, sort, callback) |
aaf61f38 C |
307 | } |
308 | ||
aaf61f38 C |
309 | // --------------------------------------------------------------------------- |
310 | ||
aaf61f38 | 311 | function removeThumbnail (video, callback) { |
f285faa0 | 312 | fs.unlink(constants.CONFIG.STORAGE.THUMBNAILS_DIR + video.getThumbnailName(), callback) |
aaf61f38 C |
313 | } |
314 | ||
315 | function removeFile (video, callback) { | |
f285faa0 | 316 | fs.unlink(constants.CONFIG.STORAGE.VIDEOS_DIR + video.getVideoFilename(), callback) |
aaf61f38 C |
317 | } |
318 | ||
aaf61f38 | 319 | function removeTorrent (video, callback) { |
558d7c23 | 320 | fs.unlink(constants.CONFIG.STORAGE.TORRENTS_DIR + video.getTorrentName(), callback) |
aaf61f38 C |
321 | } |
322 | ||
6a94a109 C |
323 | function removePreview (video, callback) { |
324 | // Same name than video thumnail | |
f285faa0 | 325 | fs.unlink(constants.CONFIG.STORAGE.PREVIEWS_DIR + video.getPreviewName(), callback) |
6a94a109 C |
326 | } |
327 | ||
558d7c23 | 328 | function createPreview (video, videoPath, callback) { |
f285faa0 | 329 | generateImage(video, videoPath, constants.CONFIG.STORAGE.PREVIEWS_DIR, video.getPreviewName(), callback) |
6a94a109 C |
330 | } |
331 | ||
558d7c23 | 332 | function createThumbnail (video, videoPath, callback) { |
f285faa0 | 333 | generateImage(video, videoPath, constants.CONFIG.STORAGE.THUMBNAILS_DIR, video.getThumbnailName(), constants.THUMBNAILS_SIZE, callback) |
aaf61f38 C |
334 | } |
335 | ||
558d7c23 C |
336 | function generateThumbnailFromBase64 (video, thumbnailData, callback) { |
337 | // Creating the thumbnail for this remote video) | |
338 | ||
f285faa0 | 339 | const thumbnailName = video.getThumbnailName() |
558d7c23 C |
340 | const thumbnailPath = constants.CONFIG.STORAGE.THUMBNAILS_DIR + thumbnailName |
341 | fs.writeFile(thumbnailPath, thumbnailData, { encoding: 'base64' }, function (err) { | |
aaf61f38 C |
342 | if (err) return callback(err) |
343 | ||
558d7c23 C |
344 | return callback(null, thumbnailName) |
345 | }) | |
346 | } | |
347 | ||
f285faa0 | 348 | function generateImage (video, videoPath, folder, imageName, size, callback) { |
558d7c23 | 349 | const options = { |
f285faa0 | 350 | filename: imageName, |
558d7c23 C |
351 | count: 1, |
352 | folder | |
353 | } | |
aaf61f38 | 354 | |
558d7c23 C |
355 | if (!callback) { |
356 | callback = size | |
357 | } else { | |
358 | options.size = size | |
359 | } | |
360 | ||
361 | ffmpeg(videoPath) | |
362 | .on('error', callback) | |
363 | .on('end', function () { | |
f285faa0 | 364 | callback(null, imageName) |
aaf61f38 | 365 | }) |
558d7c23 | 366 | .thumbnail(options) |
aaf61f38 | 367 | } |