]>
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') |
7920c273 | 7 | const map = require('lodash/map') |
1a42c9e2 | 8 | const parallel = require('async/parallel') |
052937db | 9 | const parseTorrent = require('parse-torrent') |
aaf61f38 | 10 | const pathUtils = require('path') |
67bf9b96 | 11 | const values = require('lodash/values') |
aaf61f38 C |
12 | |
13 | const constants = require('../initializers/constants') | |
aaf61f38 | 14 | const logger = require('../helpers/logger') |
98ac898a | 15 | const friends = require('../lib/friends') |
0ff21c1c | 16 | const modelUtils = require('./utils') |
67bf9b96 | 17 | const customVideosValidators = require('../helpers/custom-validators').videos |
aaf61f38 | 18 | |
aaf61f38 C |
19 | // --------------------------------------------------------------------------- |
20 | ||
feb4bdfd | 21 | module.exports = function (sequelize, DataTypes) { |
b769007f | 22 | // TODO: add indexes on searchable columns |
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 | ||
109 | generateThumbnailFromBase64, | |
110 | getDurationFromFile, | |
b769007f | 111 | list, |
feb4bdfd C |
112 | listForApi, |
113 | listByHostAndRemoteId, | |
7920c273 | 114 | listOwnedAndPopulateAuthorAndTags, |
feb4bdfd C |
115 | listOwnedByAuthor, |
116 | load, | |
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, | |
129 | toRemoteJSON | |
130 | }, | |
131 | hooks: { | |
67bf9b96 | 132 | beforeValidate, |
feb4bdfd C |
133 | beforeCreate, |
134 | afterDestroy | |
135 | } | |
136 | } | |
137 | ) | |
aaf61f38 | 138 | |
feb4bdfd C |
139 | return Video |
140 | } | |
aaf61f38 | 141 | |
67bf9b96 C |
142 | function beforeValidate (video, options, next) { |
143 | if (video.isOwned()) { | |
144 | // 40 hexa length | |
145 | video.infoHash = '0123456789abcdef0123456789abcdef01234567' | |
146 | } | |
147 | ||
148 | return next(null) | |
149 | } | |
feb4bdfd C |
150 | |
151 | function beforeCreate (video, options, next) { | |
aaf61f38 C |
152 | const tasks = [] |
153 | ||
154 | if (video.isOwned()) { | |
f285faa0 | 155 | const videoPath = pathUtils.join(constants.CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename()) |
aaf61f38 C |
156 | |
157 | tasks.push( | |
052937db | 158 | // TODO: refractoring |
aaf61f38 | 159 | function (callback) { |
25cad919 C |
160 | const options = { |
161 | announceList: [ | |
3737bbaf | 162 | [ constants.CONFIG.WEBSERVER.WS + '://' + constants.CONFIG.WEBSERVER.HOSTNAME + ':' + constants.CONFIG.WEBSERVER.PORT + '/tracker/socket' ] |
25cad919 C |
163 | ], |
164 | urlList: [ | |
f285faa0 | 165 | constants.CONFIG.WEBSERVER.URL + constants.STATIC_PATHS.WEBSEED + video.getVideoFilename() |
25cad919 C |
166 | ] |
167 | } | |
168 | ||
169 | createTorrent(videoPath, options, function (err, torrent) { | |
052937db C |
170 | if (err) return callback(err) |
171 | ||
558d7c23 | 172 | fs.writeFile(constants.CONFIG.STORAGE.TORRENTS_DIR + video.getTorrentName(), torrent, function (err) { |
052937db C |
173 | if (err) return callback(err) |
174 | ||
175 | const parsedTorrent = parseTorrent(torrent) | |
67bf9b96 C |
176 | video.set('infoHash', parsedTorrent.infoHash) |
177 | video.validate().asCallback(callback) | |
052937db C |
178 | }) |
179 | }) | |
aaf61f38 C |
180 | }, |
181 | function (callback) { | |
558d7c23 | 182 | createThumbnail(video, videoPath, callback) |
6a94a109 C |
183 | }, |
184 | function (callback) { | |
558d7c23 | 185 | createPreview(video, videoPath, callback) |
aaf61f38 C |
186 | } |
187 | ) | |
188 | ||
c77fa067 | 189 | return parallel(tasks, next) |
aaf61f38 | 190 | } |
c77fa067 C |
191 | |
192 | return next() | |
feb4bdfd | 193 | } |
aaf61f38 | 194 | |
feb4bdfd C |
195 | function afterDestroy (video, options, next) { |
196 | const tasks = [] | |
197 | ||
198 | tasks.push( | |
199 | function (callback) { | |
200 | removeThumbnail(video, callback) | |
201 | } | |
202 | ) | |
203 | ||
204 | if (video.isOwned()) { | |
205 | tasks.push( | |
206 | function (callback) { | |
207 | removeFile(video, callback) | |
208 | }, | |
98ac898a | 209 | |
feb4bdfd C |
210 | function (callback) { |
211 | removeTorrent(video, callback) | |
212 | }, | |
98ac898a | 213 | |
feb4bdfd C |
214 | function (callback) { |
215 | removePreview(video, callback) | |
98ac898a C |
216 | }, |
217 | ||
218 | function (callback) { | |
219 | const params = { | |
220 | name: video.name, | |
221 | remoteId: video.id | |
222 | } | |
223 | ||
224 | friends.removeVideoToFriends(params) | |
225 | ||
226 | return callback() | |
feb4bdfd C |
227 | } |
228 | ) | |
229 | } | |
230 | ||
231 | parallel(tasks, next) | |
232 | } | |
aaf61f38 C |
233 | |
234 | // ------------------------------ METHODS ------------------------------ | |
235 | ||
feb4bdfd C |
236 | function associate (models) { |
237 | this.belongsTo(models.Author, { | |
238 | foreignKey: { | |
239 | name: 'authorId', | |
240 | allowNull: false | |
241 | }, | |
242 | onDelete: 'cascade' | |
243 | }) | |
7920c273 C |
244 | |
245 | this.belongsToMany(models.Tag, { | |
246 | foreignKey: 'videoId', | |
247 | through: models.VideoTag, | |
248 | onDelete: 'cascade' | |
249 | }) | |
feb4bdfd C |
250 | } |
251 | ||
f285faa0 C |
252 | function generateMagnetUri () { |
253 | let baseUrlHttp, baseUrlWs | |
254 | ||
255 | if (this.isOwned()) { | |
256 | baseUrlHttp = constants.CONFIG.WEBSERVER.URL | |
257 | baseUrlWs = constants.CONFIG.WEBSERVER.WS + '://' + constants.CONFIG.WEBSERVER.HOSTNAME + ':' + constants.CONFIG.WEBSERVER.PORT | |
258 | } else { | |
feb4bdfd C |
259 | baseUrlHttp = constants.REMOTE_SCHEME.HTTP + '://' + this.Author.Pod.host |
260 | baseUrlWs = constants.REMOTE_SCHEME.WS + '://' + this.Author.Pod.host | |
f285faa0 C |
261 | } |
262 | ||
263 | const xs = baseUrlHttp + constants.STATIC_PATHS.TORRENTS + this.getTorrentName() | |
264 | const announce = baseUrlWs + '/tracker/socket' | |
265 | const urlList = [ baseUrlHttp + constants.STATIC_PATHS.WEBSEED + this.getVideoFilename() ] | |
266 | ||
267 | const magnetHash = { | |
268 | xs, | |
269 | announce, | |
270 | urlList, | |
feb4bdfd | 271 | infoHash: this.infoHash, |
f285faa0 C |
272 | name: this.name |
273 | } | |
274 | ||
275 | return magnetUtil.encode(magnetHash) | |
558d7c23 C |
276 | } |
277 | ||
f285faa0 | 278 | function getVideoFilename () { |
feb4bdfd | 279 | if (this.isOwned()) return this.id + this.extname |
f285faa0 C |
280 | |
281 | return this.remoteId + this.extname | |
282 | } | |
283 | ||
284 | function getThumbnailName () { | |
285 | // We always have a copy of the thumbnail | |
feb4bdfd | 286 | return this.id + '.jpg' |
558d7c23 C |
287 | } |
288 | ||
f285faa0 C |
289 | function getPreviewName () { |
290 | const extension = '.jpg' | |
291 | ||
feb4bdfd | 292 | if (this.isOwned()) return this.id + extension |
f285faa0 C |
293 | |
294 | return this.remoteId + extension | |
295 | } | |
296 | ||
558d7c23 | 297 | function getTorrentName () { |
f285faa0 C |
298 | const extension = '.torrent' |
299 | ||
feb4bdfd | 300 | if (this.isOwned()) return this.id + extension |
f285faa0 C |
301 | |
302 | return this.remoteId + extension | |
558d7c23 C |
303 | } |
304 | ||
aaf61f38 | 305 | function isOwned () { |
558d7c23 | 306 | return this.remoteId === null |
aaf61f38 C |
307 | } |
308 | ||
309 | function toFormatedJSON () { | |
feb4bdfd C |
310 | let podHost |
311 | ||
312 | if (this.Author.Pod) { | |
313 | podHost = this.Author.Pod.host | |
314 | } else { | |
315 | // It means it's our video | |
316 | podHost = constants.CONFIG.WEBSERVER.HOST | |
317 | } | |
318 | ||
aaf61f38 | 319 | const json = { |
feb4bdfd | 320 | id: this.id, |
aaf61f38 C |
321 | name: this.name, |
322 | description: this.description, | |
feb4bdfd | 323 | podHost, |
aaf61f38 | 324 | isLocal: this.isOwned(), |
f285faa0 | 325 | magnetUri: this.generateMagnetUri(), |
feb4bdfd | 326 | author: this.Author.name, |
aaf61f38 | 327 | duration: this.duration, |
7920c273 | 328 | tags: map(this.Tags, 'name'), |
f285faa0 | 329 | thumbnailPath: constants.STATIC_PATHS.THUMBNAILS + '/' + this.getThumbnailName(), |
feb4bdfd | 330 | createdAt: this.createdAt |
aaf61f38 C |
331 | } |
332 | ||
333 | return json | |
334 | } | |
335 | ||
336 | function toRemoteJSON (callback) { | |
337 | const self = this | |
338 | ||
339 | // Convert thumbnail to base64 | |
f285faa0 | 340 | const thumbnailPath = pathUtils.join(constants.CONFIG.STORAGE.THUMBNAILS_DIR, this.getThumbnailName()) |
558d7c23 | 341 | fs.readFile(thumbnailPath, function (err, thumbnailData) { |
aaf61f38 C |
342 | if (err) { |
343 | logger.error('Cannot read the thumbnail of the video') | |
344 | return callback(err) | |
345 | } | |
346 | ||
347 | const remoteVideo = { | |
348 | name: self.name, | |
349 | description: self.description, | |
feb4bdfd C |
350 | infoHash: self.infoHash, |
351 | remoteId: self.id, | |
352 | author: self.Author.name, | |
aaf61f38 C |
353 | duration: self.duration, |
354 | thumbnailBase64: new Buffer(thumbnailData).toString('base64'), | |
7920c273 | 355 | tags: map(self.Tags, 'name'), |
feb4bdfd | 356 | createdAt: self.createdAt, |
8f217302 | 357 | extname: self.extname |
aaf61f38 C |
358 | } |
359 | ||
360 | return callback(null, remoteVideo) | |
361 | }) | |
362 | } | |
363 | ||
364 | // ------------------------------ STATICS ------------------------------ | |
365 | ||
c77fa067 C |
366 | function generateThumbnailFromBase64 (video, thumbnailData, callback) { |
367 | // Creating the thumbnail for a remote video | |
368 | ||
369 | const thumbnailName = video.getThumbnailName() | |
370 | const thumbnailPath = constants.CONFIG.STORAGE.THUMBNAILS_DIR + thumbnailName | |
371 | fs.writeFile(thumbnailPath, thumbnailData, { encoding: 'base64' }, function (err) { | |
372 | if (err) return callback(err) | |
373 | ||
374 | return callback(null, thumbnailName) | |
375 | }) | |
376 | } | |
377 | ||
aaf61f38 C |
378 | function getDurationFromFile (videoPath, callback) { |
379 | ffmpeg.ffprobe(videoPath, function (err, metadata) { | |
380 | if (err) return callback(err) | |
381 | ||
382 | return callback(null, Math.floor(metadata.format.duration)) | |
383 | }) | |
384 | } | |
385 | ||
b769007f C |
386 | function list (callback) { |
387 | return this.find().asCallback() | |
388 | } | |
389 | ||
0ff21c1c | 390 | function listForApi (start, count, sort, callback) { |
feb4bdfd C |
391 | const query = { |
392 | offset: start, | |
393 | limit: count, | |
7920c273 | 394 | distinct: true, // For the count, a video can have many tags |
178edb20 | 395 | order: [ modelUtils.getSort(sort), [ this.sequelize.models.Tag, 'name', 'ASC' ] ], |
feb4bdfd C |
396 | include: [ |
397 | { | |
398 | model: this.sequelize.models.Author, | |
7920c273 C |
399 | include: [ { model: this.sequelize.models.Pod, required: false } ] |
400 | }, | |
401 | ||
402 | this.sequelize.models.Tag | |
feb4bdfd C |
403 | ] |
404 | } | |
405 | ||
406 | return this.findAndCountAll(query).asCallback(function (err, result) { | |
407 | if (err) return callback(err) | |
408 | ||
409 | return callback(null, result.rows, result.count) | |
410 | }) | |
aaf61f38 C |
411 | } |
412 | ||
49abbbbe | 413 | function listByHostAndRemoteId (fromHost, remoteId, callback) { |
feb4bdfd C |
414 | const query = { |
415 | where: { | |
416 | remoteId: remoteId | |
417 | }, | |
418 | include: [ | |
419 | { | |
420 | model: this.sequelize.models.Author, | |
421 | include: [ | |
422 | { | |
423 | model: this.sequelize.models.Pod, | |
7920c273 | 424 | required: true, |
feb4bdfd C |
425 | where: { |
426 | host: fromHost | |
427 | } | |
428 | } | |
429 | ] | |
430 | } | |
431 | ] | |
432 | } | |
aaf61f38 | 433 | |
feb4bdfd | 434 | return this.findAll(query).asCallback(callback) |
aaf61f38 C |
435 | } |
436 | ||
7920c273 | 437 | function listOwnedAndPopulateAuthorAndTags (callback) { |
558d7c23 | 438 | // If remoteId is null this is *our* video |
feb4bdfd C |
439 | const query = { |
440 | where: { | |
441 | remoteId: null | |
442 | }, | |
7920c273 | 443 | include: [ this.sequelize.models.Author, this.sequelize.models.Tag ] |
feb4bdfd C |
444 | } |
445 | ||
446 | return this.findAll(query).asCallback(callback) | |
aaf61f38 C |
447 | } |
448 | ||
9bd26629 | 449 | function listOwnedByAuthor (author, callback) { |
feb4bdfd C |
450 | const query = { |
451 | where: { | |
452 | remoteId: null | |
453 | }, | |
454 | include: [ | |
455 | { | |
456 | model: this.sequelize.models.Author, | |
457 | where: { | |
458 | name: author | |
459 | } | |
460 | } | |
461 | ] | |
462 | } | |
9bd26629 | 463 | |
feb4bdfd | 464 | return this.findAll(query).asCallback(callback) |
aaf61f38 C |
465 | } |
466 | ||
467 | function load (id, callback) { | |
feb4bdfd C |
468 | return this.findById(id).asCallback(callback) |
469 | } | |
470 | ||
471 | function loadAndPopulateAuthor (id, callback) { | |
472 | const options = { | |
473 | include: [ this.sequelize.models.Author ] | |
474 | } | |
475 | ||
476 | return this.findById(id, options).asCallback(callback) | |
477 | } | |
478 | ||
7920c273 | 479 | function loadAndPopulateAuthorAndPodAndTags (id, callback) { |
feb4bdfd C |
480 | const options = { |
481 | include: [ | |
482 | { | |
483 | model: this.sequelize.models.Author, | |
7920c273 C |
484 | include: [ { model: this.sequelize.models.Pod, required: false } ] |
485 | }, | |
486 | this.sequelize.models.Tag | |
feb4bdfd C |
487 | ] |
488 | } | |
489 | ||
490 | return this.findById(id, options).asCallback(callback) | |
aaf61f38 C |
491 | } |
492 | ||
7920c273 | 493 | function searchAndPopulateAuthorAndPodAndTags (value, field, start, count, sort, callback) { |
feb4bdfd | 494 | const podInclude = { |
7920c273 C |
495 | model: this.sequelize.models.Pod, |
496 | required: false | |
feb4bdfd | 497 | } |
7920c273 | 498 | |
feb4bdfd C |
499 | const authorInclude = { |
500 | model: this.sequelize.models.Author, | |
501 | include: [ | |
502 | podInclude | |
503 | ] | |
504 | } | |
505 | ||
7920c273 C |
506 | const tagInclude = { |
507 | model: this.sequelize.models.Tag | |
508 | } | |
509 | ||
feb4bdfd C |
510 | const query = { |
511 | where: {}, | |
feb4bdfd C |
512 | offset: start, |
513 | limit: count, | |
7920c273 | 514 | distinct: true, // For the count, a video can have many tags |
178edb20 | 515 | order: [ modelUtils.getSort(sort), [ this.sequelize.models.Tag, 'name', 'ASC' ] ] |
feb4bdfd C |
516 | } |
517 | ||
aaf61f38 | 518 | // Make an exact search with the magnet |
55723d16 C |
519 | if (field === 'magnetUri') { |
520 | const infoHash = magnetUtil.decode(value).infoHash | |
feb4bdfd | 521 | query.where.infoHash = infoHash |
55723d16 | 522 | } else if (field === 'tags') { |
7920c273 C |
523 | const escapedValue = this.sequelize.escape('%' + value + '%') |
524 | query.where = { | |
525 | id: { | |
526 | $in: this.sequelize.literal( | |
527 | '(SELECT "VideoTags"."videoId" FROM "Tags" INNER JOIN "VideoTags" ON "Tags"."id" = "VideoTags"."tagId" WHERE name LIKE ' + escapedValue + ')' | |
528 | ) | |
feb4bdfd C |
529 | } |
530 | } | |
7920c273 C |
531 | } else if (field === 'host') { |
532 | // FIXME: Include our pod? (not stored in the database) | |
533 | podInclude.where = { | |
534 | host: { | |
535 | $like: '%' + value + '%' | |
feb4bdfd | 536 | } |
feb4bdfd | 537 | } |
7920c273 | 538 | podInclude.required = true |
feb4bdfd | 539 | } else if (field === 'author') { |
7920c273 C |
540 | authorInclude.where = { |
541 | name: { | |
feb4bdfd C |
542 | $like: '%' + value + '%' |
543 | } | |
544 | } | |
7920c273 C |
545 | |
546 | // authorInclude.or = true | |
aaf61f38 | 547 | } else { |
feb4bdfd C |
548 | query.where[field] = { |
549 | $like: '%' + value + '%' | |
550 | } | |
aaf61f38 C |
551 | } |
552 | ||
7920c273 C |
553 | query.include = [ |
554 | authorInclude, tagInclude | |
555 | ] | |
556 | ||
557 | if (tagInclude.where) { | |
558 | // query.include.push([ this.sequelize.models.Tag ]) | |
559 | } | |
560 | ||
feb4bdfd C |
561 | return this.findAndCountAll(query).asCallback(function (err, result) { |
562 | if (err) return callback(err) | |
563 | ||
564 | return callback(null, result.rows, result.count) | |
565 | }) | |
aaf61f38 C |
566 | } |
567 | ||
aaf61f38 C |
568 | // --------------------------------------------------------------------------- |
569 | ||
aaf61f38 | 570 | function removeThumbnail (video, callback) { |
f285faa0 | 571 | fs.unlink(constants.CONFIG.STORAGE.THUMBNAILS_DIR + video.getThumbnailName(), callback) |
aaf61f38 C |
572 | } |
573 | ||
574 | function removeFile (video, callback) { | |
f285faa0 | 575 | fs.unlink(constants.CONFIG.STORAGE.VIDEOS_DIR + video.getVideoFilename(), callback) |
aaf61f38 C |
576 | } |
577 | ||
aaf61f38 | 578 | function removeTorrent (video, callback) { |
558d7c23 | 579 | fs.unlink(constants.CONFIG.STORAGE.TORRENTS_DIR + video.getTorrentName(), callback) |
aaf61f38 C |
580 | } |
581 | ||
6a94a109 C |
582 | function removePreview (video, callback) { |
583 | // Same name than video thumnail | |
f285faa0 | 584 | fs.unlink(constants.CONFIG.STORAGE.PREVIEWS_DIR + video.getPreviewName(), callback) |
6a94a109 C |
585 | } |
586 | ||
558d7c23 | 587 | function createPreview (video, videoPath, callback) { |
f285faa0 | 588 | generateImage(video, videoPath, constants.CONFIG.STORAGE.PREVIEWS_DIR, video.getPreviewName(), callback) |
6a94a109 C |
589 | } |
590 | ||
558d7c23 | 591 | function createThumbnail (video, videoPath, callback) { |
f285faa0 | 592 | generateImage(video, videoPath, constants.CONFIG.STORAGE.THUMBNAILS_DIR, video.getThumbnailName(), constants.THUMBNAILS_SIZE, callback) |
aaf61f38 C |
593 | } |
594 | ||
f285faa0 | 595 | function generateImage (video, videoPath, folder, imageName, size, callback) { |
558d7c23 | 596 | const options = { |
f285faa0 | 597 | filename: imageName, |
558d7c23 C |
598 | count: 1, |
599 | folder | |
600 | } | |
aaf61f38 | 601 | |
558d7c23 C |
602 | if (!callback) { |
603 | callback = size | |
604 | } else { | |
605 | options.size = size | |
606 | } | |
607 | ||
608 | ffmpeg(videoPath) | |
609 | .on('error', callback) | |
610 | .on('end', function () { | |
f285faa0 | 611 | callback(null, imageName) |
aaf61f38 | 612 | }) |
558d7c23 | 613 | .thumbnail(options) |
aaf61f38 | 614 | } |