]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/video.js
Server: use binary data instead of base64 to send thumbnails
[github/Chocobozzz/PeerTube.git] / server / models / video.js
index 3ebc48ad4d66ec5bc4faa8a370c5178e1abedf8c..0e84e89867f722d88e0c6d236986b4faa6646d15 100644 (file)
@@ -1,5 +1,6 @@
 'use strict'
 
+const Buffer = require('safe-buffer').Buffer
 const createTorrent = require('create-torrent')
 const ffmpeg = require('fluent-ffmpeg')
 const fs = require('fs')
@@ -12,6 +13,7 @@ const values = require('lodash/values')
 
 const constants = require('../initializers/constants')
 const logger = require('../helpers/logger')
+const friends = require('../lib/friends')
 const modelUtils = require('./utils')
 const customVideosValidators = require('../helpers/custom-validators').videos
 
@@ -82,10 +84,30 @@ module.exports = function (sequelize, DataTypes) {
       }
     },
     {
+      indexes: [
+        {
+          fields: [ 'authorId' ]
+        },
+        {
+          fields: [ 'remoteId' ]
+        },
+        {
+          fields: [ 'name' ]
+        },
+        {
+          fields: [ 'createdAt' ]
+        },
+        {
+          fields: [ 'duration' ]
+        },
+        {
+          fields: [ 'infoHash' ]
+        }
+      ],
       classMethods: {
         associate,
 
-        generateThumbnailFromBase64,
+        generateThumbnailFromData,
         getDurationFromFile,
         list,
         listForApi,
@@ -185,11 +207,24 @@ function afterDestroy (video, options, next) {
       function (callback) {
         removeFile(video, callback)
       },
+
       function (callback) {
         removeTorrent(video, callback)
       },
+
       function (callback) {
         removePreview(video, callback)
+      },
+
+      function (callback) {
+        const params = {
+          name: video.name,
+          remoteId: video.id
+        }
+
+        friends.removeVideoToFriends(params)
+
+        return callback()
       }
     )
   }
@@ -302,7 +337,7 @@ function toFormatedJSON () {
 function toRemoteJSON (callback) {
   const self = this
 
-  // Convert thumbnail to base64
+  // Get thumbnail data to send to the other pod
   const thumbnailPath = pathUtils.join(constants.CONFIG.STORAGE.THUMBNAILS_DIR, this.getThumbnailName())
   fs.readFile(thumbnailPath, function (err, thumbnailData) {
     if (err) {
@@ -317,7 +352,7 @@ function toRemoteJSON (callback) {
       remoteId: self.id,
       author: self.Author.name,
       duration: self.duration,
-      thumbnailBase64: new Buffer(thumbnailData).toString('base64'),
+      thumbnailData: thumbnailData.toString('binary'),
       tags: map(self.Tags, 'name'),
       createdAt: self.createdAt,
       extname: self.extname
@@ -329,12 +364,12 @@ function toRemoteJSON (callback) {
 
 // ------------------------------ STATICS ------------------------------
 
-function generateThumbnailFromBase64 (video, thumbnailData, callback) {
+function generateThumbnailFromData (video, thumbnailData, callback) {
   // Creating the thumbnail for a remote video
 
   const thumbnailName = video.getThumbnailName()
   const thumbnailPath = constants.CONFIG.STORAGE.THUMBNAILS_DIR + thumbnailName
-  fs.writeFile(thumbnailPath, thumbnailData, { encoding: 'base64' }, function (err) {
+  fs.writeFile(thumbnailPath, Buffer.from(thumbnailData, 'binary'), function (err) {
     if (err) return callback(err)
 
     return callback(null, thumbnailName)