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