From: Chocobozzz Date: Thu, 29 Dec 2016 11:13:19 +0000 (+0100) Subject: Server: use binary data instead of base64 to send thumbnails X-Git-Tag: v0.0.1-alpha~574^2~25 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=4d32448895ad29ef694bcf790d59253249ad5939;p=oweals%2Fpeertube.git Server: use binary data instead of base64 to send thumbnails --- diff --git a/package.json b/package.json index bff21082f..5eadcc363 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/server/controllers/api/remote.js b/server/controllers/api/remote.js index 94d6e740e..ac850c2d2 100644 --- a/server/controllers/api/remote.js +++ b/server/controllers/api/remote.js @@ -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) } diff --git a/server/controllers/api/users.js b/server/controllers/api/users.js index e4423680c..53bf56790 100644 --- a/server/controllers/api/users.js +++ b/server/controllers/api/users.js @@ -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 diff --git a/server/helpers/custom-validators/videos.js b/server/helpers/custom-validators/videos.js index da857ba5f..4aaa6aaa9 100644 --- a/server/helpers/custom-validators/videos.js +++ b/server/helpers/custom-validators/videos.js @@ -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) { diff --git a/server/initializers/constants.js b/server/initializers/constants.js index 0af7aca3c..474a37277 100644 --- a/server/initializers/constants.js +++ b/server/initializers/constants.js @@ -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 } } diff --git a/server/models/video.js b/server/models/video.js index 564e362fd..0e84e8986 100644 --- a/server/models/video.js +++ b/server/models/video.js @@ -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)