]>
Commit | Line | Data |
---|---|---|
aaf61f38 C |
1 | 'use strict' |
2 | ||
4d324488 | 3 | const Buffer = require('safe-buffer').Buffer |
052937db | 4 | const createTorrent = require('create-torrent') |
aaf61f38 C |
5 | const ffmpeg = require('fluent-ffmpeg') |
6 | const fs = require('fs') | |
f285faa0 | 7 | const magnetUtil = require('magnet-uri') |
7920c273 | 8 | const map = require('lodash/map') |
1a42c9e2 | 9 | const parallel = require('async/parallel') |
052937db | 10 | const parseTorrent = require('parse-torrent') |
aaf61f38 | 11 | const pathUtils = require('path') |
67bf9b96 | 12 | const values = require('lodash/values') |
aaf61f38 C |
13 | |
14 | const constants = require('../initializers/constants') | |
aaf61f38 | 15 | const logger = require('../helpers/logger') |
98ac898a | 16 | const friends = require('../lib/friends') |
0ff21c1c | 17 | const modelUtils = require('./utils') |
67bf9b96 | 18 | const customVideosValidators = require('../helpers/custom-validators').videos |
aaf61f38 | 19 | |
aaf61f38 C |
20 | // --------------------------------------------------------------------------- |
21 | ||
feb4bdfd | 22 | module.exports = function (sequelize, DataTypes) { |
feb4bdfd C |
23 | const Video = sequelize.define('Video', |
24 | { | |
25 | id: { | |
26 | type: DataTypes.UUID, | |
27 | defaultValue: DataTypes.UUIDV4, | |
67bf9b96 C |
28 | primaryKey: true, |
29 | validate: { | |
30 | isUUID: 4 | |
31 | } | |
aaf61f38 | 32 | }, |
feb4bdfd | 33 | name: { |
67bf9b96 C |
34 | type: DataTypes.STRING, |
35 | allowNull: false, | |
36 | validate: { | |
37 | nameValid: function (value) { | |
38 | const res = customVideosValidators.isVideoNameValid(value) | |
39 | if (res === false) throw new Error('Video name is not valid.') | |
40 | } | |
41 | } | |
6a94a109 | 42 | }, |
feb4bdfd | 43 | extname: { |
67bf9b96 C |
44 | type: DataTypes.ENUM(values(constants.CONSTRAINTS_FIELDS.VIDEOS.EXTNAME)), |
45 | allowNull: false | |
feb4bdfd C |
46 | }, |
47 | remoteId: { | |
67bf9b96 C |
48 | type: DataTypes.UUID, |
49 | allowNull: true, | |
50 | validate: { | |
51 | isUUID: 4 | |
52 | } | |
feb4bdfd C |
53 | }, |
54 | description: { | |
67bf9b96 C |
55 | type: DataTypes.STRING, |
56 | allowNull: false, | |
57 | validate: { | |
58 | descriptionValid: function (value) { | |
59 | const res = customVideosValidators.isVideoDescriptionValid(value) | |
60 | if (res === false) throw new Error('Video description is not valid.') | |
61 | } | |
62 | } | |
feb4bdfd C |
63 | }, |
64 | infoHash: { | |
67bf9b96 C |
65 | type: DataTypes.STRING, |
66 | allowNull: false, | |
67 | validate: { | |
68 | infoHashValid: function (value) { | |
69 | const res = customVideosValidators.isVideoInfoHashValid(value) | |
70 | if (res === false) throw new Error('Video info hash is not valid.') | |
71 | } | |
72 | } | |
feb4bdfd C |
73 | }, |
74 | duration: { | |
67bf9b96 C |
75 | type: DataTypes.INTEGER, |
76 | allowNull: false, | |
77 | validate: { | |
78 | durationValid: function (value) { | |
79 | const res = customVideosValidators.isVideoDurationValid(value) | |
80 | if (res === false) throw new Error('Video duration is not valid.') | |
81 | } | |
82 | } | |
aaf61f38 | 83 | } |
feb4bdfd C |
84 | }, |
85 | { | |
319d072e C |
86 | indexes: [ |
87 | { | |
88 | fields: [ 'authorId' ] | |
89 | }, | |
90 | { | |
91 | fields: [ 'remoteId' ] | |
92 | }, | |
93 | { | |
94 | fields: [ 'name' ] | |
95 | }, | |
96 | { | |
97 | fields: [ 'createdAt' ] | |
98 | }, | |
99 | { | |
100 | fields: [ 'duration' ] | |
101 | }, | |
102 | { | |
103 | fields: [ 'infoHash' ] | |
104 | } | |
105 | ], | |
feb4bdfd C |
106 | classMethods: { |
107 | associate, | |
108 | ||
4d324488 | 109 | generateThumbnailFromData, |
feb4bdfd | 110 | getDurationFromFile, |
b769007f | 111 | list, |
feb4bdfd | 112 | listForApi, |
7920c273 | 113 | listOwnedAndPopulateAuthorAndTags, |
feb4bdfd C |
114 | listOwnedByAuthor, |
115 | load, | |
3d118fb5 | 116 | loadByHostAndRemoteId, |
feb4bdfd | 117 | loadAndPopulateAuthor, |
7920c273 C |
118 | loadAndPopulateAuthorAndPodAndTags, |
119 | searchAndPopulateAuthorAndPodAndTags | |
feb4bdfd C |
120 | }, |
121 | instanceMethods: { | |
122 | generateMagnetUri, | |
123 | getVideoFilename, | |
124 | getThumbnailName, | |
125 | getPreviewName, | |
126 | getTorrentName, | |
127 | isOwned, | |
128 | toFormatedJSON, | |
7b1f49de C |
129 | toAddRemoteJSON, |
130 | toUpdateRemoteJSON | |
feb4bdfd C |
131 | }, |
132 | hooks: { | |
67bf9b96 | 133 | beforeValidate, |
feb4bdfd C |
134 | beforeCreate, |
135 | afterDestroy | |
136 | } | |
137 | } | |
138 | ) | |
aaf61f38 | 139 | |
feb4bdfd C |
140 | return Video |
141 | } | |
aaf61f38 | 142 | |
67bf9b96 | 143 | function beforeValidate (video, options, next) { |
7f4e7c36 C |
144 | // Put a fake infoHash if it does not exists yet |
145 | if (video.isOwned() && !video.infoHash) { | |
67bf9b96 C |
146 | // 40 hexa length |
147 | video.infoHash = '0123456789abcdef0123456789abcdef01234567' | |
148 | } | |
149 | ||
150 | return next(null) | |
151 | } | |
feb4bdfd C |
152 | |
153 | function beforeCreate (video, options, next) { | |
aaf61f38 C |
154 | const tasks = [] |
155 | ||
156 | if (video.isOwned()) { | |
f285faa0 | 157 | const videoPath = pathUtils.join(constants.CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename()) |
aaf61f38 C |
158 | |
159 | tasks.push( | |
052937db | 160 | // TODO: refractoring |
aaf61f38 | 161 | function (callback) { |
25cad919 C |
162 | const options = { |
163 | announceList: [ | |
3737bbaf | 164 | [ constants.CONFIG.WEBSERVER.WS + '://' + constants.CONFIG.WEBSERVER.HOSTNAME + ':' + constants.CONFIG.WEBSERVER.PORT + '/tracker/socket' ] |
25cad919 C |
165 | ], |
166 | urlList: [ | |
f285faa0 | 167 | constants.CONFIG.WEBSERVER.URL + constants.STATIC_PATHS.WEBSEED + video.getVideoFilename() |
25cad919 C |
168 | ] |
169 | } | |
170 | ||
171 | createTorrent(videoPath, options, function (err, torrent) { | |
052937db C |
172 | if (err) return callback(err) |
173 | ||
558d7c23 | 174 | fs.writeFile(constants.CONFIG.STORAGE.TORRENTS_DIR + video.getTorrentName(), torrent, function (err) { |
052937db C |
175 | if (err) return callback(err) |
176 | ||
177 | const parsedTorrent = parseTorrent(torrent) | |
67bf9b96 C |
178 | video.set('infoHash', parsedTorrent.infoHash) |
179 | video.validate().asCallback(callback) | |
052937db C |
180 | }) |
181 | }) | |
aaf61f38 C |
182 | }, |
183 | function (callback) { | |
558d7c23 | 184 | createThumbnail(video, videoPath, callback) |
6a94a109 C |
185 | }, |
186 | function (callback) { | |
558d7c23 | 187 | createPreview(video, videoPath, callback) |
aaf61f38 C |
188 | } |
189 | ) | |
190 | ||
c77fa067 | 191 | return parallel(tasks, next) |
aaf61f38 | 192 | } |
c77fa067 C |
193 | |
194 | return next() | |
feb4bdfd | 195 | } |
aaf61f38 | 196 | |
feb4bdfd C |
197 | function afterDestroy (video, options, next) { |
198 | const tasks = [] | |
199 | ||
200 | tasks.push( | |
201 | function (callback) { | |
202 | removeThumbnail(video, callback) | |
203 | } | |
204 | ) | |
205 | ||
206 | if (video.isOwned()) { | |
207 | tasks.push( | |
208 | function (callback) { | |
209 | removeFile(video, callback) | |
210 | }, | |
98ac898a | 211 | |
feb4bdfd C |
212 | function (callback) { |
213 | removeTorrent(video, callback) | |
214 | }, | |
98ac898a | 215 | |
feb4bdfd C |
216 | function (callback) { |
217 | removePreview(video, callback) | |
98ac898a C |
218 | }, |
219 | ||
220 | function (callback) { | |
221 | const params = { | |
98ac898a C |
222 | remoteId: video.id |
223 | } | |
224 | ||
225 | friends.removeVideoToFriends(params) | |
226 | ||
227 | return callback() | |
feb4bdfd C |
228 | } |
229 | ) | |
230 | } | |
231 | ||
232 | parallel(tasks, next) | |
233 | } | |
aaf61f38 C |
234 | |
235 | // ------------------------------ METHODS ------------------------------ | |
236 | ||
feb4bdfd C |
237 | function associate (models) { |
238 | this.belongsTo(models.Author, { | |
239 | foreignKey: { | |
240 | name: 'authorId', | |
241 | allowNull: false | |
242 | }, | |
243 | onDelete: 'cascade' | |
244 | }) | |
7920c273 C |
245 | |
246 | this.belongsToMany(models.Tag, { | |
247 | foreignKey: 'videoId', | |
248 | through: models.VideoTag, | |
249 | onDelete: 'cascade' | |
250 | }) | |
55fa55a9 C |
251 | |
252 | this.hasMany(models.VideoAbuse, { | |
253 | foreignKey: { | |
254 | name: 'videoId', | |
255 | allowNull: false | |
256 | }, | |
257 | onDelete: 'cascade' | |
258 | }) | |
feb4bdfd C |
259 | } |
260 | ||
f285faa0 C |
261 | function generateMagnetUri () { |
262 | let baseUrlHttp, baseUrlWs | |
263 | ||
264 | if (this.isOwned()) { | |
265 | baseUrlHttp = constants.CONFIG.WEBSERVER.URL | |
266 | baseUrlWs = constants.CONFIG.WEBSERVER.WS + '://' + constants.CONFIG.WEBSERVER.HOSTNAME + ':' + constants.CONFIG.WEBSERVER.PORT | |
267 | } else { | |
feb4bdfd C |
268 | baseUrlHttp = constants.REMOTE_SCHEME.HTTP + '://' + this.Author.Pod.host |
269 | baseUrlWs = constants.REMOTE_SCHEME.WS + '://' + this.Author.Pod.host | |
f285faa0 C |
270 | } |
271 | ||
272 | const xs = baseUrlHttp + constants.STATIC_PATHS.TORRENTS + this.getTorrentName() | |
273 | const announce = baseUrlWs + '/tracker/socket' | |
274 | const urlList = [ baseUrlHttp + constants.STATIC_PATHS.WEBSEED + this.getVideoFilename() ] | |
275 | ||
276 | const magnetHash = { | |
277 | xs, | |
278 | announce, | |
279 | urlList, | |
feb4bdfd | 280 | infoHash: this.infoHash, |
f285faa0 C |
281 | name: this.name |
282 | } | |
283 | ||
284 | return magnetUtil.encode(magnetHash) | |
558d7c23 C |
285 | } |
286 | ||
f285faa0 | 287 | function getVideoFilename () { |
feb4bdfd | 288 | if (this.isOwned()) return this.id + this.extname |
f285faa0 C |
289 | |
290 | return this.remoteId + this.extname | |
291 | } | |
292 | ||
293 | function getThumbnailName () { | |
294 | // We always have a copy of the thumbnail | |
feb4bdfd | 295 | return this.id + '.jpg' |
558d7c23 C |
296 | } |
297 | ||
f285faa0 C |
298 | function getPreviewName () { |
299 | const extension = '.jpg' | |
300 | ||
feb4bdfd | 301 | if (this.isOwned()) return this.id + extension |
f285faa0 C |
302 | |
303 | return this.remoteId + extension | |
304 | } | |
305 | ||
558d7c23 | 306 | function getTorrentName () { |
f285faa0 C |
307 | const extension = '.torrent' |
308 | ||
feb4bdfd | 309 | if (this.isOwned()) return this.id + extension |
f285faa0 C |
310 | |
311 | return this.remoteId + extension | |
558d7c23 C |
312 | } |
313 | ||
aaf61f38 | 314 | function isOwned () { |
558d7c23 | 315 | return this.remoteId === null |
aaf61f38 C |
316 | } |
317 | ||
318 | function toFormatedJSON () { | |
feb4bdfd C |
319 | let podHost |
320 | ||
321 | if (this.Author.Pod) { | |
322 | podHost = this.Author.Pod.host | |
323 | } else { | |
324 | // It means it's our video | |
325 | podHost = constants.CONFIG.WEBSERVER.HOST | |
326 | } | |
327 | ||
aaf61f38 | 328 | const json = { |
feb4bdfd | 329 | id: this.id, |
aaf61f38 C |
330 | name: this.name, |
331 | description: this.description, | |
feb4bdfd | 332 | podHost, |
aaf61f38 | 333 | isLocal: this.isOwned(), |
f285faa0 | 334 | magnetUri: this.generateMagnetUri(), |
feb4bdfd | 335 | author: this.Author.name, |
aaf61f38 | 336 | duration: this.duration, |
7920c273 | 337 | tags: map(this.Tags, 'name'), |
91cc839a | 338 | thumbnailPath: pathUtils.join(constants.STATIC_PATHS.THUMBNAILS, this.getThumbnailName()), |
79066fdf C |
339 | createdAt: this.createdAt, |
340 | updatedAt: this.updatedAt | |
aaf61f38 C |
341 | } |
342 | ||
343 | return json | |
344 | } | |
345 | ||
7b1f49de | 346 | function toAddRemoteJSON (callback) { |
aaf61f38 C |
347 | const self = this |
348 | ||
4d324488 | 349 | // Get thumbnail data to send to the other pod |
f285faa0 | 350 | const thumbnailPath = pathUtils.join(constants.CONFIG.STORAGE.THUMBNAILS_DIR, this.getThumbnailName()) |
558d7c23 | 351 | fs.readFile(thumbnailPath, function (err, thumbnailData) { |
aaf61f38 C |
352 | if (err) { |
353 | logger.error('Cannot read the thumbnail of the video') | |
354 | return callback(err) | |
355 | } | |
356 | ||
357 | const remoteVideo = { | |
358 | name: self.name, | |
359 | description: self.description, | |
feb4bdfd C |
360 | infoHash: self.infoHash, |
361 | remoteId: self.id, | |
362 | author: self.Author.name, | |
aaf61f38 | 363 | duration: self.duration, |
4d324488 | 364 | thumbnailData: thumbnailData.toString('binary'), |
7920c273 | 365 | tags: map(self.Tags, 'name'), |
feb4bdfd | 366 | createdAt: self.createdAt, |
79066fdf | 367 | updatedAt: self.updatedAt, |
8f217302 | 368 | extname: self.extname |
aaf61f38 C |
369 | } |
370 | ||
371 | return callback(null, remoteVideo) | |
372 | }) | |
373 | } | |
374 | ||
7b1f49de C |
375 | function toUpdateRemoteJSON (callback) { |
376 | const json = { | |
377 | name: this.name, | |
378 | description: this.description, | |
379 | infoHash: this.infoHash, | |
380 | remoteId: this.id, | |
381 | author: this.Author.name, | |
382 | duration: this.duration, | |
383 | tags: map(this.Tags, 'name'), | |
384 | createdAt: this.createdAt, | |
79066fdf | 385 | updatedAt: this.updatedAt, |
7b1f49de C |
386 | extname: this.extname |
387 | } | |
388 | ||
389 | return json | |
390 | } | |
391 | ||
aaf61f38 C |
392 | // ------------------------------ STATICS ------------------------------ |
393 | ||
4d324488 | 394 | function generateThumbnailFromData (video, thumbnailData, callback) { |
c77fa067 C |
395 | // Creating the thumbnail for a remote video |
396 | ||
397 | const thumbnailName = video.getThumbnailName() | |
398 | const thumbnailPath = constants.CONFIG.STORAGE.THUMBNAILS_DIR + thumbnailName | |
4d324488 | 399 | fs.writeFile(thumbnailPath, Buffer.from(thumbnailData, 'binary'), function (err) { |
c77fa067 C |
400 | if (err) return callback(err) |
401 | ||
402 | return callback(null, thumbnailName) | |
403 | }) | |
404 | } | |
405 | ||
aaf61f38 C |
406 | function getDurationFromFile (videoPath, callback) { |
407 | ffmpeg.ffprobe(videoPath, function (err, metadata) { | |
408 | if (err) return callback(err) | |
409 | ||
410 | return callback(null, Math.floor(metadata.format.duration)) | |
411 | }) | |
412 | } | |
413 | ||
b769007f C |
414 | function list (callback) { |
415 | return this.find().asCallback() | |
416 | } | |
417 | ||
0ff21c1c | 418 | function listForApi (start, count, sort, callback) { |
feb4bdfd C |
419 | const query = { |
420 | offset: start, | |
421 | limit: count, | |
7920c273 | 422 | distinct: true, // For the count, a video can have many tags |
178edb20 | 423 | order: [ modelUtils.getSort(sort), [ this.sequelize.models.Tag, 'name', 'ASC' ] ], |
feb4bdfd C |
424 | include: [ |
425 | { | |
426 | model: this.sequelize.models.Author, | |
7920c273 C |
427 | include: [ { model: this.sequelize.models.Pod, required: false } ] |
428 | }, | |
429 | ||
430 | this.sequelize.models.Tag | |
feb4bdfd C |
431 | ] |
432 | } | |
433 | ||
434 | return this.findAndCountAll(query).asCallback(function (err, result) { | |
435 | if (err) return callback(err) | |
436 | ||
437 | return callback(null, result.rows, result.count) | |
438 | }) | |
aaf61f38 C |
439 | } |
440 | ||
3d118fb5 | 441 | function loadByHostAndRemoteId (fromHost, remoteId, callback) { |
feb4bdfd C |
442 | const query = { |
443 | where: { | |
444 | remoteId: remoteId | |
445 | }, | |
446 | include: [ | |
447 | { | |
448 | model: this.sequelize.models.Author, | |
449 | include: [ | |
450 | { | |
451 | model: this.sequelize.models.Pod, | |
7920c273 | 452 | required: true, |
feb4bdfd C |
453 | where: { |
454 | host: fromHost | |
455 | } | |
456 | } | |
457 | ] | |
458 | } | |
459 | ] | |
460 | } | |
aaf61f38 | 461 | |
3d118fb5 | 462 | return this.findOne(query).asCallback(callback) |
aaf61f38 C |
463 | } |
464 | ||
7920c273 | 465 | function listOwnedAndPopulateAuthorAndTags (callback) { |
558d7c23 | 466 | // If remoteId is null this is *our* video |
feb4bdfd C |
467 | const query = { |
468 | where: { | |
469 | remoteId: null | |
470 | }, | |
7920c273 | 471 | include: [ this.sequelize.models.Author, this.sequelize.models.Tag ] |
feb4bdfd C |
472 | } |
473 | ||
474 | return this.findAll(query).asCallback(callback) | |
aaf61f38 C |
475 | } |
476 | ||
9bd26629 | 477 | function listOwnedByAuthor (author, callback) { |
feb4bdfd C |
478 | const query = { |
479 | where: { | |
480 | remoteId: null | |
481 | }, | |
482 | include: [ | |
483 | { | |
484 | model: this.sequelize.models.Author, | |
485 | where: { | |
486 | name: author | |
487 | } | |
488 | } | |
489 | ] | |
490 | } | |
9bd26629 | 491 | |
feb4bdfd | 492 | return this.findAll(query).asCallback(callback) |
aaf61f38 C |
493 | } |
494 | ||
495 | function load (id, callback) { | |
feb4bdfd C |
496 | return this.findById(id).asCallback(callback) |
497 | } | |
498 | ||
499 | function loadAndPopulateAuthor (id, callback) { | |
500 | const options = { | |
501 | include: [ this.sequelize.models.Author ] | |
502 | } | |
503 | ||
504 | return this.findById(id, options).asCallback(callback) | |
505 | } | |
506 | ||
7920c273 | 507 | function loadAndPopulateAuthorAndPodAndTags (id, callback) { |
feb4bdfd C |
508 | const options = { |
509 | include: [ | |
510 | { | |
511 | model: this.sequelize.models.Author, | |
7920c273 C |
512 | include: [ { model: this.sequelize.models.Pod, required: false } ] |
513 | }, | |
514 | this.sequelize.models.Tag | |
feb4bdfd C |
515 | ] |
516 | } | |
517 | ||
518 | return this.findById(id, options).asCallback(callback) | |
aaf61f38 C |
519 | } |
520 | ||
7920c273 | 521 | function searchAndPopulateAuthorAndPodAndTags (value, field, start, count, sort, callback) { |
feb4bdfd | 522 | const podInclude = { |
7920c273 C |
523 | model: this.sequelize.models.Pod, |
524 | required: false | |
feb4bdfd | 525 | } |
7920c273 | 526 | |
feb4bdfd C |
527 | const authorInclude = { |
528 | model: this.sequelize.models.Author, | |
529 | include: [ | |
530 | podInclude | |
531 | ] | |
532 | } | |
533 | ||
7920c273 C |
534 | const tagInclude = { |
535 | model: this.sequelize.models.Tag | |
536 | } | |
537 | ||
feb4bdfd C |
538 | const query = { |
539 | where: {}, | |
feb4bdfd C |
540 | offset: start, |
541 | limit: count, | |
7920c273 | 542 | distinct: true, // For the count, a video can have many tags |
178edb20 | 543 | order: [ modelUtils.getSort(sort), [ this.sequelize.models.Tag, 'name', 'ASC' ] ] |
feb4bdfd C |
544 | } |
545 | ||
aaf61f38 | 546 | // Make an exact search with the magnet |
55723d16 C |
547 | if (field === 'magnetUri') { |
548 | const infoHash = magnetUtil.decode(value).infoHash | |
feb4bdfd | 549 | query.where.infoHash = infoHash |
55723d16 | 550 | } else if (field === 'tags') { |
7920c273 C |
551 | const escapedValue = this.sequelize.escape('%' + value + '%') |
552 | query.where = { | |
553 | id: { | |
554 | $in: this.sequelize.literal( | |
555 | '(SELECT "VideoTags"."videoId" FROM "Tags" INNER JOIN "VideoTags" ON "Tags"."id" = "VideoTags"."tagId" WHERE name LIKE ' + escapedValue + ')' | |
556 | ) | |
feb4bdfd C |
557 | } |
558 | } | |
7920c273 C |
559 | } else if (field === 'host') { |
560 | // FIXME: Include our pod? (not stored in the database) | |
561 | podInclude.where = { | |
562 | host: { | |
563 | $like: '%' + value + '%' | |
feb4bdfd | 564 | } |
feb4bdfd | 565 | } |
7920c273 | 566 | podInclude.required = true |
feb4bdfd | 567 | } else if (field === 'author') { |
7920c273 C |
568 | authorInclude.where = { |
569 | name: { | |
feb4bdfd C |
570 | $like: '%' + value + '%' |
571 | } | |
572 | } | |
7920c273 C |
573 | |
574 | // authorInclude.or = true | |
aaf61f38 | 575 | } else { |
feb4bdfd C |
576 | query.where[field] = { |
577 | $like: '%' + value + '%' | |
578 | } | |
aaf61f38 C |
579 | } |
580 | ||
7920c273 C |
581 | query.include = [ |
582 | authorInclude, tagInclude | |
583 | ] | |
584 | ||
585 | if (tagInclude.where) { | |
586 | // query.include.push([ this.sequelize.models.Tag ]) | |
587 | } | |
588 | ||
feb4bdfd C |
589 | return this.findAndCountAll(query).asCallback(function (err, result) { |
590 | if (err) return callback(err) | |
591 | ||
592 | return callback(null, result.rows, result.count) | |
593 | }) | |
aaf61f38 C |
594 | } |
595 | ||
aaf61f38 C |
596 | // --------------------------------------------------------------------------- |
597 | ||
aaf61f38 | 598 | function removeThumbnail (video, callback) { |
f285faa0 | 599 | fs.unlink(constants.CONFIG.STORAGE.THUMBNAILS_DIR + video.getThumbnailName(), callback) |
aaf61f38 C |
600 | } |
601 | ||
602 | function removeFile (video, callback) { | |
f285faa0 | 603 | fs.unlink(constants.CONFIG.STORAGE.VIDEOS_DIR + video.getVideoFilename(), callback) |
aaf61f38 C |
604 | } |
605 | ||
aaf61f38 | 606 | function removeTorrent (video, callback) { |
558d7c23 | 607 | fs.unlink(constants.CONFIG.STORAGE.TORRENTS_DIR + video.getTorrentName(), callback) |
aaf61f38 C |
608 | } |
609 | ||
6a94a109 C |
610 | function removePreview (video, callback) { |
611 | // Same name than video thumnail | |
f285faa0 | 612 | fs.unlink(constants.CONFIG.STORAGE.PREVIEWS_DIR + video.getPreviewName(), callback) |
6a94a109 C |
613 | } |
614 | ||
558d7c23 | 615 | function createPreview (video, videoPath, callback) { |
f285faa0 | 616 | generateImage(video, videoPath, constants.CONFIG.STORAGE.PREVIEWS_DIR, video.getPreviewName(), callback) |
6a94a109 C |
617 | } |
618 | ||
558d7c23 | 619 | function createThumbnail (video, videoPath, callback) { |
f285faa0 | 620 | generateImage(video, videoPath, constants.CONFIG.STORAGE.THUMBNAILS_DIR, video.getThumbnailName(), constants.THUMBNAILS_SIZE, callback) |
aaf61f38 C |
621 | } |
622 | ||
f285faa0 | 623 | function generateImage (video, videoPath, folder, imageName, size, callback) { |
558d7c23 | 624 | const options = { |
f285faa0 | 625 | filename: imageName, |
558d7c23 C |
626 | count: 1, |
627 | folder | |
628 | } | |
aaf61f38 | 629 | |
558d7c23 C |
630 | if (!callback) { |
631 | callback = size | |
632 | } else { | |
633 | options.size = size | |
634 | } | |
635 | ||
636 | ffmpeg(videoPath) | |
637 | .on('error', callback) | |
638 | .on('end', function () { | |
f285faa0 | 639 | callback(null, imageName) |
aaf61f38 | 640 | }) |
558d7c23 | 641 | .thumbnail(options) |
aaf61f38 | 642 | } |