Server: use binary data instead of base64 to send thumbnails

This commit is contained in:
Chocobozzz 2016-12-29 12:13:19 +01:00
parent 98ac898a03
commit 4d32448895
6 changed files with 14 additions and 15 deletions

View File

@ -66,6 +66,7 @@
"request": "^2.57.0",
"request-replay": "^1.0.2",
"rimraf": "^2.5.4",
"safe-buffer": "^5.0.1",
"scripty": "^1.5.0",
"sequelize": "^3.27.0",
"ursa": "^0.9.1",

View File

@ -147,9 +147,9 @@ function addRemoteVideo (videoToCreateData, fromHost, finalCallback) {
},
function generateThumbnail (t, tagInstances, video, callback) {
db.Video.generateThumbnailFromBase64(video, videoToCreateData.thumbnailBase64, function (err) {
db.Video.generateThumbnailFromData(video, videoToCreateData.thumbnailData, function (err) {
if (err) {
logger.error('Cannot generate thumbnail from base 64 data.', { error: err })
logger.error('Cannot generate thumbnail from data.', { error: err })
return callback(err)
}

View File

@ -1,12 +1,10 @@
'use strict'
const each = require('async/each')
const express = require('express')
const waterfall = require('async/waterfall')
const constants = require('../../initializers/constants')
const db = require('../../initializers/database')
const friends = require('../../lib/friends')
const logger = require('../../helpers/logger')
const middlewares = require('../../middlewares')
const admin = middlewares.admin

View File

@ -17,7 +17,7 @@ const videosValidators = {
isVideoNameValid,
isVideoTagsValid,
isVideoThumbnailValid,
isVideoThumbnail64Valid
isVideoThumbnailDataValid
}
function isEachRemoteVideosValid (requests) {
@ -33,7 +33,7 @@ function isEachRemoteVideosValid (requests) {
isVideoInfoHashValid(video.infoHash) &&
isVideoNameValid(video.name) &&
isVideoTagsValid(video.tags) &&
isVideoThumbnail64Valid(video.thumbnailBase64) &&
isVideoThumbnailDataValid(video.thumbnailData) &&
isVideoRemoteIdValid(video.remoteId) &&
isVideoExtnameValid(video.extname)
) ||
@ -86,9 +86,8 @@ function isVideoThumbnailValid (value) {
return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL)
}
function isVideoThumbnail64Valid (value) {
return validator.isBase64(value) &&
validator.isByteLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL64)
function isVideoThumbnailDataValid (value) {
return validator.isByteLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL_DATA)
}
function isVideoRemoteIdValid (value) {

View File

@ -74,7 +74,7 @@ const CONSTRAINTS_FIELDS = {
TAGS: { min: 1, max: 3 }, // Number of total tags
TAG: { min: 2, max: 10 }, // Length
THUMBNAIL: { min: 2, max: 30 },
THUMBNAIL64: { min: 0, max: 20000 } // Bytes
THUMBNAIL_DATA: { min: 0, max: 20000 } // Bytes
}
}

View 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')
@ -106,7 +107,7 @@ module.exports = function (sequelize, DataTypes) {
classMethods: {
associate,
generateThumbnailFromBase64,
generateThumbnailFromData,
getDurationFromFile,
list,
listForApi,
@ -336,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) {
@ -351,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
@ -363,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)