]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/video.ts
Type models
[github/Chocobozzz/PeerTube.git] / server / models / video.ts
1 import safeBuffer = require('safe-buffer')
2 const Buffer = safeBuffer.Buffer
3 import createTorrent = require('create-torrent')
4 import ffmpeg = require('fluent-ffmpeg')
5 import fs = require('fs')
6 import magnetUtil = require('magnet-uri')
7 import { map, values } from 'lodash'
8 import { parallel, series } from 'async'
9 import parseTorrent = require('parse-torrent')
10 import { join } from 'path'
11 import * as Sequelize from 'sequelize'
12
13 import { database as db } from '../initializers/database'
14 import {
15 logger,
16 isVideoNameValid,
17 isVideoCategoryValid,
18 isVideoLicenceValid,
19 isVideoLanguageValid,
20 isVideoNSFWValid,
21 isVideoDescriptionValid,
22 isVideoInfoHashValid,
23 isVideoDurationValid
24 } from '../helpers'
25 import {
26 CONSTRAINTS_FIELDS,
27 CONFIG,
28 REMOTE_SCHEME,
29 STATIC_PATHS,
30 VIDEO_CATEGORIES,
31 VIDEO_LICENCES,
32 VIDEO_LANGUAGES,
33 THUMBNAILS_SIZE
34 } from '../initializers'
35 import { JobScheduler, removeVideoToFriends } from '../lib'
36
37 import { addMethodsToModel, getSort } from './utils'
38 import {
39 VideoClass,
40 VideoInstance,
41 VideoAttributes,
42
43 VideoMethods
44 } from './video-interface'
45
46 let Video: Sequelize.Model<VideoInstance, VideoAttributes>
47 let generateMagnetUri: VideoMethods.GenerateMagnetUri
48 let getVideoFilename: VideoMethods.GetVideoFilename
49 let getThumbnailName: VideoMethods.GetThumbnailName
50 let getPreviewName: VideoMethods.GetPreviewName
51 let getTorrentName: VideoMethods.GetTorrentName
52 let isOwned: VideoMethods.IsOwned
53 let toFormatedJSON: VideoMethods.ToFormatedJSON
54 let toAddRemoteJSON: VideoMethods.ToAddRemoteJSON
55 let toUpdateRemoteJSON: VideoMethods.ToUpdateRemoteJSON
56 let transcodeVideofile: VideoMethods.TranscodeVideofile
57
58 let generateThumbnailFromData: VideoMethods.GenerateThumbnailFromData
59 let getDurationFromFile: VideoMethods.GetDurationFromFile
60 let list: VideoMethods.List
61 let listForApi: VideoMethods.ListForApi
62 let loadByHostAndRemoteId: VideoMethods.LoadByHostAndRemoteId
63 let listOwnedAndPopulateAuthorAndTags: VideoMethods.ListOwnedAndPopulateAuthorAndTags
64 let listOwnedByAuthor: VideoMethods.ListOwnedByAuthor
65 let load: VideoMethods.Load
66 let loadAndPopulateAuthor: VideoMethods.LoadAndPopulateAuthor
67 let loadAndPopulateAuthorAndPodAndTags: VideoMethods.LoadAndPopulateAuthorAndPodAndTags
68 let searchAndPopulateAuthorAndPodAndTags: VideoMethods.SearchAndPopulateAuthorAndPodAndTags
69
70 export default function (sequelize, DataTypes) {
71 Video = sequelize.define('Video',
72 {
73 id: {
74 type: DataTypes.UUID,
75 defaultValue: DataTypes.UUIDV4,
76 primaryKey: true,
77 validate: {
78 isUUID: 4
79 }
80 },
81 name: {
82 type: DataTypes.STRING,
83 allowNull: false,
84 validate: {
85 nameValid: function (value) {
86 const res = isVideoNameValid(value)
87 if (res === false) throw new Error('Video name is not valid.')
88 }
89 }
90 },
91 extname: {
92 type: DataTypes.ENUM(values(CONSTRAINTS_FIELDS.VIDEOS.EXTNAME)),
93 allowNull: false
94 },
95 remoteId: {
96 type: DataTypes.UUID,
97 allowNull: true,
98 validate: {
99 isUUID: 4
100 }
101 },
102 category: {
103 type: DataTypes.INTEGER,
104 allowNull: false,
105 validate: {
106 categoryValid: function (value) {
107 const res = isVideoCategoryValid(value)
108 if (res === false) throw new Error('Video category is not valid.')
109 }
110 }
111 },
112 licence: {
113 type: DataTypes.INTEGER,
114 allowNull: false,
115 defaultValue: null,
116 validate: {
117 licenceValid: function (value) {
118 const res = isVideoLicenceValid(value)
119 if (res === false) throw new Error('Video licence is not valid.')
120 }
121 }
122 },
123 language: {
124 type: DataTypes.INTEGER,
125 allowNull: true,
126 validate: {
127 languageValid: function (value) {
128 const res = isVideoLanguageValid(value)
129 if (res === false) throw new Error('Video language is not valid.')
130 }
131 }
132 },
133 nsfw: {
134 type: DataTypes.BOOLEAN,
135 allowNull: false,
136 validate: {
137 nsfwValid: function (value) {
138 const res = isVideoNSFWValid(value)
139 if (res === false) throw new Error('Video nsfw attribute is not valid.')
140 }
141 }
142 },
143 description: {
144 type: DataTypes.STRING,
145 allowNull: false,
146 validate: {
147 descriptionValid: function (value) {
148 const res = isVideoDescriptionValid(value)
149 if (res === false) throw new Error('Video description is not valid.')
150 }
151 }
152 },
153 infoHash: {
154 type: DataTypes.STRING,
155 allowNull: false,
156 validate: {
157 infoHashValid: function (value) {
158 const res = isVideoInfoHashValid(value)
159 if (res === false) throw new Error('Video info hash is not valid.')
160 }
161 }
162 },
163 duration: {
164 type: DataTypes.INTEGER,
165 allowNull: false,
166 validate: {
167 durationValid: function (value) {
168 const res = isVideoDurationValid(value)
169 if (res === false) throw new Error('Video duration is not valid.')
170 }
171 }
172 },
173 views: {
174 type: DataTypes.INTEGER,
175 allowNull: false,
176 defaultValue: 0,
177 validate: {
178 min: 0,
179 isInt: true
180 }
181 },
182 likes: {
183 type: DataTypes.INTEGER,
184 allowNull: false,
185 defaultValue: 0,
186 validate: {
187 min: 0,
188 isInt: true
189 }
190 },
191 dislikes: {
192 type: DataTypes.INTEGER,
193 allowNull: false,
194 defaultValue: 0,
195 validate: {
196 min: 0,
197 isInt: true
198 }
199 }
200 },
201 {
202 indexes: [
203 {
204 fields: [ 'authorId' ]
205 },
206 {
207 fields: [ 'remoteId' ]
208 },
209 {
210 fields: [ 'name' ]
211 },
212 {
213 fields: [ 'createdAt' ]
214 },
215 {
216 fields: [ 'duration' ]
217 },
218 {
219 fields: [ 'infoHash' ]
220 },
221 {
222 fields: [ 'views' ]
223 },
224 {
225 fields: [ 'likes' ]
226 }
227 ],
228 hooks: {
229 beforeValidate,
230 beforeCreate,
231 afterDestroy
232 }
233 }
234 )
235
236 const classMethods = [
237 associate,
238
239 generateThumbnailFromData,
240 getDurationFromFile,
241 list,
242 listForApi,
243 listOwnedAndPopulateAuthorAndTags,
244 listOwnedByAuthor,
245 load,
246 loadByHostAndRemoteId,
247 loadAndPopulateAuthor,
248 loadAndPopulateAuthorAndPodAndTags,
249 searchAndPopulateAuthorAndPodAndTags
250 ]
251 const instanceMethods = [
252 generateMagnetUri,
253 getVideoFilename,
254 getThumbnailName,
255 getPreviewName,
256 getTorrentName,
257 isOwned,
258 toFormatedJSON,
259 toAddRemoteJSON,
260 toUpdateRemoteJSON,
261 transcodeVideofile,
262 removeFromBlacklist
263 ]
264 addMethodsToModel(Video, classMethods, instanceMethods)
265
266 return Video
267 }
268
269 function beforeValidate (video, options) {
270 // Put a fake infoHash if it does not exists yet
271 if (video.isOwned() && !video.infoHash) {
272 // 40 hexa length
273 video.infoHash = '0123456789abcdef0123456789abcdef01234567'
274 }
275 }
276
277 function beforeCreate (video, options) {
278 return new Promise(function (resolve, reject) {
279 const tasks = []
280
281 if (video.isOwned()) {
282 const videoPath = join(CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename())
283
284 tasks.push(
285 function createVideoTorrent (callback) {
286 createTorrentFromVideo(video, videoPath, callback)
287 },
288
289 function createVideoThumbnail (callback) {
290 createThumbnail(video, videoPath, callback)
291 },
292
293 function createVideoPreview (callback) {
294 createPreview(video, videoPath, callback)
295 }
296 )
297
298 if (CONFIG.TRANSCODING.ENABLED === true) {
299 tasks.push(
300 function createVideoTranscoderJob (callback) {
301 const dataInput = {
302 id: video.id
303 }
304
305 JobScheduler.Instance.createJob(options.transaction, 'videoTranscoder', dataInput, callback)
306 }
307 )
308 }
309
310 return parallel(tasks, function (err) {
311 if (err) return reject(err)
312
313 return resolve()
314 })
315 }
316
317 return resolve()
318 })
319 }
320
321 function afterDestroy (video, options) {
322 return new Promise(function (resolve, reject) {
323 const tasks = []
324
325 tasks.push(
326 function (callback) {
327 removeThumbnail(video, callback)
328 }
329 )
330
331 if (video.isOwned()) {
332 tasks.push(
333 function removeVideoFile (callback) {
334 removeFile(video, callback)
335 },
336
337 function removeVideoTorrent (callback) {
338 removeTorrent(video, callback)
339 },
340
341 function removeVideoPreview (callback) {
342 removePreview(video, callback)
343 },
344
345 function notifyFriends (callback) {
346 const params = {
347 remoteId: video.id
348 }
349
350 removeVideoToFriends(params)
351
352 return callback()
353 }
354 )
355 }
356
357 parallel(tasks, function (err) {
358 if (err) return reject(err)
359
360 return resolve()
361 })
362 })
363 }
364
365 // ------------------------------ METHODS ------------------------------
366
367 function associate (models) {
368 Video.belongsTo(models.Author, {
369 foreignKey: {
370 name: 'authorId',
371 allowNull: false
372 },
373 onDelete: 'cascade'
374 })
375
376 Video.belongsToMany(models.Tag, {
377 foreignKey: 'videoId',
378 through: models.VideoTag,
379 onDelete: 'cascade'
380 })
381
382 Video.hasMany(models.VideoAbuse, {
383 foreignKey: {
384 name: 'videoId',
385 allowNull: false
386 },
387 onDelete: 'cascade'
388 })
389 }
390
391 generateMagnetUri = function () {
392 let baseUrlHttp
393 let baseUrlWs
394
395 if (this.isOwned()) {
396 baseUrlHttp = CONFIG.WEBSERVER.URL
397 baseUrlWs = CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT
398 } else {
399 baseUrlHttp = REMOTE_SCHEME.HTTP + '://' + this.Author.Pod.host
400 baseUrlWs = REMOTE_SCHEME.WS + '://' + this.Author.Pod.host
401 }
402
403 const xs = baseUrlHttp + STATIC_PATHS.TORRENTS + this.getTorrentName()
404 const announce = baseUrlWs + '/tracker/socket'
405 const urlList = [ baseUrlHttp + STATIC_PATHS.WEBSEED + this.getVideoFilename() ]
406
407 const magnetHash = {
408 xs,
409 announce,
410 urlList,
411 infoHash: this.infoHash,
412 name: this.name
413 }
414
415 return magnetUtil.encode(magnetHash)
416 }
417
418 getVideoFilename = function () {
419 if (this.isOwned()) return this.id + this.extname
420
421 return this.remoteId + this.extname
422 }
423
424 getThumbnailName = function () {
425 // We always have a copy of the thumbnail
426 return this.id + '.jpg'
427 }
428
429 getPreviewName = function () {
430 const extension = '.jpg'
431
432 if (this.isOwned()) return this.id + extension
433
434 return this.remoteId + extension
435 }
436
437 getTorrentName = function () {
438 const extension = '.torrent'
439
440 if (this.isOwned()) return this.id + extension
441
442 return this.remoteId + extension
443 }
444
445 isOwned = function () {
446 return this.remoteId === null
447 }
448
449 toFormatedJSON = function () {
450 let podHost
451
452 if (this.Author.Pod) {
453 podHost = this.Author.Pod.host
454 } else {
455 // It means it's our video
456 podHost = CONFIG.WEBSERVER.HOST
457 }
458
459 // Maybe our pod is not up to date and there are new categories since our version
460 let categoryLabel = VIDEO_CATEGORIES[this.category]
461 if (!categoryLabel) categoryLabel = 'Misc'
462
463 // Maybe our pod is not up to date and there are new licences since our version
464 let licenceLabel = VIDEO_LICENCES[this.licence]
465 if (!licenceLabel) licenceLabel = 'Unknown'
466
467 // Language is an optional attribute
468 let languageLabel = VIDEO_LANGUAGES[this.language]
469 if (!languageLabel) languageLabel = 'Unknown'
470
471 const json = {
472 id: this.id,
473 name: this.name,
474 category: this.category,
475 categoryLabel,
476 licence: this.licence,
477 licenceLabel,
478 language: this.language,
479 languageLabel,
480 nsfw: this.nsfw,
481 description: this.description,
482 podHost,
483 isLocal: this.isOwned(),
484 magnetUri: this.generateMagnetUri(),
485 author: this.Author.name,
486 duration: this.duration,
487 views: this.views,
488 likes: this.likes,
489 dislikes: this.dislikes,
490 tags: map(this.Tags, 'name'),
491 thumbnailPath: join(STATIC_PATHS.THUMBNAILS, this.getThumbnailName()),
492 createdAt: this.createdAt,
493 updatedAt: this.updatedAt
494 }
495
496 return json
497 }
498
499 toAddRemoteJSON = function (callback) {
500 // Get thumbnail data to send to the other pod
501 const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, this.getThumbnailName())
502 fs.readFile(thumbnailPath, (err, thumbnailData) => {
503 if (err) {
504 logger.error('Cannot read the thumbnail of the video')
505 return callback(err)
506 }
507
508 const remoteVideo = {
509 name: this.name,
510 category: this.category,
511 licence: this.licence,
512 language: this.language,
513 nsfw: this.nsfw,
514 description: this.description,
515 infoHash: this.infoHash,
516 remoteId: this.id,
517 author: this.Author.name,
518 duration: this.duration,
519 thumbnailData: thumbnailData.toString('binary'),
520 tags: map(this.Tags, 'name'),
521 createdAt: this.createdAt,
522 updatedAt: this.updatedAt,
523 extname: this.extname,
524 views: this.views,
525 likes: this.likes,
526 dislikes: this.dislikes
527 }
528
529 return callback(null, remoteVideo)
530 })
531 }
532
533 toUpdateRemoteJSON = function (callback) {
534 const json = {
535 name: this.name,
536 category: this.category,
537 licence: this.licence,
538 language: this.language,
539 nsfw: this.nsfw,
540 description: this.description,
541 infoHash: this.infoHash,
542 remoteId: this.id,
543 author: this.Author.name,
544 duration: this.duration,
545 tags: map(this.Tags, 'name'),
546 createdAt: this.createdAt,
547 updatedAt: this.updatedAt,
548 extname: this.extname,
549 views: this.views,
550 likes: this.likes,
551 dislikes: this.dislikes
552 }
553
554 return json
555 }
556
557 transcodeVideofile = function (finalCallback) {
558 const video = this
559
560 const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
561 const newExtname = '.mp4'
562 const videoInputPath = join(videosDirectory, video.getVideoFilename())
563 const videoOutputPath = join(videosDirectory, video.id + '-transcoded' + newExtname)
564
565 ffmpeg(videoInputPath)
566 .output(videoOutputPath)
567 .videoCodec('libx264')
568 .outputOption('-threads ' + CONFIG.TRANSCODING.THREADS)
569 .outputOption('-movflags faststart')
570 .on('error', finalCallback)
571 .on('end', function () {
572 series([
573 function removeOldFile (callback) {
574 fs.unlink(videoInputPath, callback)
575 },
576
577 function moveNewFile (callback) {
578 // Important to do this before getVideoFilename() to take in account the new file extension
579 video.set('extname', newExtname)
580
581 const newVideoPath = join(videosDirectory, video.getVideoFilename())
582 fs.rename(videoOutputPath, newVideoPath, callback)
583 },
584
585 function torrent (callback) {
586 const newVideoPath = join(videosDirectory, video.getVideoFilename())
587 createTorrentFromVideo(video, newVideoPath, callback)
588 },
589
590 function videoExtension (callback) {
591 video.save().asCallback(callback)
592 }
593
594 ], function (err) {
595 if (err) {
596 // Autodescruction...
597 video.destroy().asCallback(function (err) {
598 if (err) logger.error('Cannot destruct video after transcoding failure.', { error: err })
599 })
600
601 return finalCallback(err)
602 }
603
604 return finalCallback(null)
605 })
606 })
607 .run()
608 }
609
610 // ------------------------------ STATICS ------------------------------
611
612 generateThumbnailFromData = function (video, thumbnailData, callback) {
613 // Creating the thumbnail for a remote video
614
615 const thumbnailName = video.getThumbnailName()
616 const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, thumbnailName)
617 fs.writeFile(thumbnailPath, Buffer.from(thumbnailData, 'binary'), function (err) {
618 if (err) return callback(err)
619
620 return callback(null, thumbnailName)
621 })
622 }
623
624 getDurationFromFile = function (videoPath, callback) {
625 ffmpeg.ffprobe(videoPath, function (err, metadata) {
626 if (err) return callback(err)
627
628 return callback(null, Math.floor(metadata.format.duration))
629 })
630 }
631
632 list = function (callback) {
633 return Video.findAll().asCallback(callback)
634 }
635
636 listForApi = function (start, count, sort, callback) {
637 // Exclude Blakclisted videos from the list
638 const query = {
639 distinct: true,
640 offset: start,
641 limit: count,
642 order: [ getSort(sort), [ Video['sequelize'].models.Tag, 'name', 'ASC' ] ],
643 include: [
644 {
645 model: Video['sequelize'].models.Author,
646 include: [ { model: Video['sequelize'].models.Pod, required: false } ]
647 },
648
649 Video['sequelize'].models.Tag
650 ],
651 where: createBaseVideosWhere()
652 }
653
654 return Video.findAndCountAll(query).asCallback(function (err, result) {
655 if (err) return callback(err)
656
657 return callback(null, result.rows, result.count)
658 })
659 }
660
661 loadByHostAndRemoteId = function (fromHost, remoteId, callback) {
662 const query = {
663 where: {
664 remoteId: remoteId
665 },
666 include: [
667 {
668 model: Video['sequelize'].models.Author,
669 include: [
670 {
671 model: Video['sequelize'].models.Pod,
672 required: true,
673 where: {
674 host: fromHost
675 }
676 }
677 ]
678 }
679 ]
680 }
681
682 return Video.findOne(query).asCallback(callback)
683 }
684
685 listOwnedAndPopulateAuthorAndTags = function (callback) {
686 // If remoteId is null this is *our* video
687 const query = {
688 where: {
689 remoteId: null
690 },
691 include: [ Video['sequelize'].models.Author, Video['sequelize'].models.Tag ]
692 }
693
694 return Video.findAll(query).asCallback(callback)
695 }
696
697 listOwnedByAuthor = function (author, callback) {
698 const query = {
699 where: {
700 remoteId: null
701 },
702 include: [
703 {
704 model: Video['sequelize'].models.Author,
705 where: {
706 name: author
707 }
708 }
709 ]
710 }
711
712 return Video.findAll(query).asCallback(callback)
713 }
714
715 load = function (id, callback) {
716 return Video.findById(id).asCallback(callback)
717 }
718
719 loadAndPopulateAuthor = function (id, callback) {
720 const options = {
721 include: [ Video['sequelize'].models.Author ]
722 }
723
724 return Video.findById(id, options).asCallback(callback)
725 }
726
727 loadAndPopulateAuthorAndPodAndTags = function (id, callback) {
728 const options = {
729 include: [
730 {
731 model: Video['sequelize'].models.Author,
732 include: [ { model: Video['sequelize'].models.Pod, required: false } ]
733 },
734 Video['sequelize'].models.Tag
735 ]
736 }
737
738 return Video.findById(id, options).asCallback(callback)
739 }
740
741 searchAndPopulateAuthorAndPodAndTags = function (value, field, start, count, sort, callback) {
742 const podInclude: any = {
743 model: Video['sequelize'].models.Pod,
744 required: false
745 }
746
747 const authorInclude: any = {
748 model: Video['sequelize'].models.Author,
749 include: [
750 podInclude
751 ]
752 }
753
754 const tagInclude: any = {
755 model: Video['sequelize'].models.Tag
756 }
757
758 const query: any = {
759 distinct: true,
760 where: createBaseVideosWhere(),
761 offset: start,
762 limit: count,
763 order: [ getSort(sort), [ Video['sequelize'].models.Tag, 'name', 'ASC' ] ]
764 }
765
766 // Make an exact search with the magnet
767 if (field === 'magnetUri') {
768 const infoHash = magnetUtil.decode(value).infoHash
769 query.where.infoHash = infoHash
770 } else if (field === 'tags') {
771 const escapedValue = Video['sequelize'].escape('%' + value + '%')
772 query.where.id.$in = Video['sequelize'].literal(
773 '(SELECT "VideoTags"."videoId" FROM "Tags" INNER JOIN "VideoTags" ON "Tags"."id" = "VideoTags"."tagId" WHERE name LIKE ' + escapedValue + ')'
774 )
775 } else if (field === 'host') {
776 // FIXME: Include our pod? (not stored in the database)
777 podInclude.where = {
778 host: {
779 $like: '%' + value + '%'
780 }
781 }
782 podInclude.required = true
783 } else if (field === 'author') {
784 authorInclude.where = {
785 name: {
786 $like: '%' + value + '%'
787 }
788 }
789
790 // authorInclude.or = true
791 } else {
792 query.where[field] = {
793 $like: '%' + value + '%'
794 }
795 }
796
797 query.include = [
798 authorInclude, tagInclude
799 ]
800
801 if (tagInclude.where) {
802 // query.include.push([ Video['sequelize'].models.Tag ])
803 }
804
805 return Video.findAndCountAll(query).asCallback(function (err, result) {
806 if (err) return callback(err)
807
808 return callback(null, result.rows, result.count)
809 })
810 }
811
812 // ---------------------------------------------------------------------------
813
814 function createBaseVideosWhere () {
815 return {
816 id: {
817 $notIn: Video['sequelize'].literal(
818 '(SELECT "BlacklistedVideos"."videoId" FROM "BlacklistedVideos")'
819 )
820 }
821 }
822 }
823
824 function removeThumbnail (video, callback) {
825 const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, video.getThumbnailName())
826 fs.unlink(thumbnailPath, callback)
827 }
828
829 function removeFile (video, callback) {
830 const filePath = join(CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename())
831 fs.unlink(filePath, callback)
832 }
833
834 function removeTorrent (video, callback) {
835 const torrenPath = join(CONFIG.STORAGE.TORRENTS_DIR, video.getTorrentName())
836 fs.unlink(torrenPath, callback)
837 }
838
839 function removePreview (video, callback) {
840 // Same name than video thumnail
841 fs.unlink(CONFIG.STORAGE.PREVIEWS_DIR + video.getPreviewName(), callback)
842 }
843
844 function createTorrentFromVideo (video, videoPath, callback) {
845 const options = {
846 announceList: [
847 [ CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT + '/tracker/socket' ]
848 ],
849 urlList: [
850 CONFIG.WEBSERVER.URL + STATIC_PATHS.WEBSEED + video.getVideoFilename()
851 ]
852 }
853
854 createTorrent(videoPath, options, function (err, torrent) {
855 if (err) return callback(err)
856
857 const filePath = join(CONFIG.STORAGE.TORRENTS_DIR, video.getTorrentName())
858 fs.writeFile(filePath, torrent, function (err) {
859 if (err) return callback(err)
860
861 const parsedTorrent = parseTorrent(torrent)
862 video.set('infoHash', parsedTorrent.infoHash)
863 video.validate().asCallback(callback)
864 })
865 })
866 }
867
868 function createPreview (video, videoPath, callback) {
869 generateImage(video, videoPath, CONFIG.STORAGE.PREVIEWS_DIR, video.getPreviewName(), callback)
870 }
871
872 function createThumbnail (video, videoPath, callback) {
873 generateImage(video, videoPath, CONFIG.STORAGE.THUMBNAILS_DIR, video.getThumbnailName(), THUMBNAILS_SIZE, callback)
874 }
875
876 function generateImage (video, videoPath, folder, imageName, size, callback?) {
877 const options: any = {
878 filename: imageName,
879 count: 1,
880 folder
881 }
882
883 if (!callback) {
884 callback = size
885 } else {
886 options.size = size
887 }
888
889 ffmpeg(videoPath)
890 .on('error', callback)
891 .on('end', function () {
892 callback(null, imageName)
893 })
894 .thumbnail(options)
895 }
896
897 function removeFromBlacklist (video, callback) {
898 // Find the blacklisted video
899 db.BlacklistedVideo.loadByVideoId(video.id, function (err, video) {
900 // If an error occured, stop here
901 if (err) {
902 logger.error('Error when fetching video from blacklist.', { error: err })
903 return callback(err)
904 }
905
906 // If we found the video, remove it from the blacklist
907 if (video) {
908 video.destroy().asCallback(callback)
909 } else {
910 // If haven't found it, simply ignore it and do nothing
911 return callback()
912 }
913 })
914 }