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