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