Server: use binary data instead of base64 to send thumbnails
This commit is contained in:
parent
98ac898a03
commit
4d32448895
|
@ -66,6 +66,7 @@
|
||||||
"request": "^2.57.0",
|
"request": "^2.57.0",
|
||||||
"request-replay": "^1.0.2",
|
"request-replay": "^1.0.2",
|
||||||
"rimraf": "^2.5.4",
|
"rimraf": "^2.5.4",
|
||||||
|
"safe-buffer": "^5.0.1",
|
||||||
"scripty": "^1.5.0",
|
"scripty": "^1.5.0",
|
||||||
"sequelize": "^3.27.0",
|
"sequelize": "^3.27.0",
|
||||||
"ursa": "^0.9.1",
|
"ursa": "^0.9.1",
|
||||||
|
|
|
@ -147,9 +147,9 @@ function addRemoteVideo (videoToCreateData, fromHost, finalCallback) {
|
||||||
},
|
},
|
||||||
|
|
||||||
function generateThumbnail (t, tagInstances, video, callback) {
|
function generateThumbnail (t, tagInstances, video, callback) {
|
||||||
db.Video.generateThumbnailFromBase64(video, videoToCreateData.thumbnailBase64, function (err) {
|
db.Video.generateThumbnailFromData(video, videoToCreateData.thumbnailData, function (err) {
|
||||||
if (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)
|
return callback(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,12 +1,10 @@
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const each = require('async/each')
|
|
||||||
const express = require('express')
|
const express = require('express')
|
||||||
const waterfall = require('async/waterfall')
|
const waterfall = require('async/waterfall')
|
||||||
|
|
||||||
const constants = require('../../initializers/constants')
|
const constants = require('../../initializers/constants')
|
||||||
const db = require('../../initializers/database')
|
const db = require('../../initializers/database')
|
||||||
const friends = require('../../lib/friends')
|
|
||||||
const logger = require('../../helpers/logger')
|
const logger = require('../../helpers/logger')
|
||||||
const middlewares = require('../../middlewares')
|
const middlewares = require('../../middlewares')
|
||||||
const admin = middlewares.admin
|
const admin = middlewares.admin
|
||||||
|
|
|
@ -17,7 +17,7 @@ const videosValidators = {
|
||||||
isVideoNameValid,
|
isVideoNameValid,
|
||||||
isVideoTagsValid,
|
isVideoTagsValid,
|
||||||
isVideoThumbnailValid,
|
isVideoThumbnailValid,
|
||||||
isVideoThumbnail64Valid
|
isVideoThumbnailDataValid
|
||||||
}
|
}
|
||||||
|
|
||||||
function isEachRemoteVideosValid (requests) {
|
function isEachRemoteVideosValid (requests) {
|
||||||
|
@ -33,7 +33,7 @@ function isEachRemoteVideosValid (requests) {
|
||||||
isVideoInfoHashValid(video.infoHash) &&
|
isVideoInfoHashValid(video.infoHash) &&
|
||||||
isVideoNameValid(video.name) &&
|
isVideoNameValid(video.name) &&
|
||||||
isVideoTagsValid(video.tags) &&
|
isVideoTagsValid(video.tags) &&
|
||||||
isVideoThumbnail64Valid(video.thumbnailBase64) &&
|
isVideoThumbnailDataValid(video.thumbnailData) &&
|
||||||
isVideoRemoteIdValid(video.remoteId) &&
|
isVideoRemoteIdValid(video.remoteId) &&
|
||||||
isVideoExtnameValid(video.extname)
|
isVideoExtnameValid(video.extname)
|
||||||
) ||
|
) ||
|
||||||
|
@ -86,9 +86,8 @@ function isVideoThumbnailValid (value) {
|
||||||
return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL)
|
return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL)
|
||||||
}
|
}
|
||||||
|
|
||||||
function isVideoThumbnail64Valid (value) {
|
function isVideoThumbnailDataValid (value) {
|
||||||
return validator.isBase64(value) &&
|
return validator.isByteLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL_DATA)
|
||||||
validator.isByteLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL64)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function isVideoRemoteIdValid (value) {
|
function isVideoRemoteIdValid (value) {
|
||||||
|
|
|
@ -74,7 +74,7 @@ const CONSTRAINTS_FIELDS = {
|
||||||
TAGS: { min: 1, max: 3 }, // Number of total tags
|
TAGS: { min: 1, max: 3 }, // Number of total tags
|
||||||
TAG: { min: 2, max: 10 }, // Length
|
TAG: { min: 2, max: 10 }, // Length
|
||||||
THUMBNAIL: { min: 2, max: 30 },
|
THUMBNAIL: { min: 2, max: 30 },
|
||||||
THUMBNAIL64: { min: 0, max: 20000 } // Bytes
|
THUMBNAIL_DATA: { min: 0, max: 20000 } // Bytes
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
|
const Buffer = require('safe-buffer').Buffer
|
||||||
const createTorrent = require('create-torrent')
|
const createTorrent = require('create-torrent')
|
||||||
const ffmpeg = require('fluent-ffmpeg')
|
const ffmpeg = require('fluent-ffmpeg')
|
||||||
const fs = require('fs')
|
const fs = require('fs')
|
||||||
|
@ -106,7 +107,7 @@ module.exports = function (sequelize, DataTypes) {
|
||||||
classMethods: {
|
classMethods: {
|
||||||
associate,
|
associate,
|
||||||
|
|
||||||
generateThumbnailFromBase64,
|
generateThumbnailFromData,
|
||||||
getDurationFromFile,
|
getDurationFromFile,
|
||||||
list,
|
list,
|
||||||
listForApi,
|
listForApi,
|
||||||
|
@ -336,7 +337,7 @@ function toFormatedJSON () {
|
||||||
function toRemoteJSON (callback) {
|
function toRemoteJSON (callback) {
|
||||||
const self = this
|
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())
|
const thumbnailPath = pathUtils.join(constants.CONFIG.STORAGE.THUMBNAILS_DIR, this.getThumbnailName())
|
||||||
fs.readFile(thumbnailPath, function (err, thumbnailData) {
|
fs.readFile(thumbnailPath, function (err, thumbnailData) {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
@ -351,7 +352,7 @@ function toRemoteJSON (callback) {
|
||||||
remoteId: self.id,
|
remoteId: self.id,
|
||||||
author: self.Author.name,
|
author: self.Author.name,
|
||||||
duration: self.duration,
|
duration: self.duration,
|
||||||
thumbnailBase64: new Buffer(thumbnailData).toString('base64'),
|
thumbnailData: thumbnailData.toString('binary'),
|
||||||
tags: map(self.Tags, 'name'),
|
tags: map(self.Tags, 'name'),
|
||||||
createdAt: self.createdAt,
|
createdAt: self.createdAt,
|
||||||
extname: self.extname
|
extname: self.extname
|
||||||
|
@ -363,12 +364,12 @@ function toRemoteJSON (callback) {
|
||||||
|
|
||||||
// ------------------------------ STATICS ------------------------------
|
// ------------------------------ STATICS ------------------------------
|
||||||
|
|
||||||
function generateThumbnailFromBase64 (video, thumbnailData, callback) {
|
function generateThumbnailFromData (video, thumbnailData, callback) {
|
||||||
// Creating the thumbnail for a remote video
|
// Creating the thumbnail for a remote video
|
||||||
|
|
||||||
const thumbnailName = video.getThumbnailName()
|
const thumbnailName = video.getThumbnailName()
|
||||||
const thumbnailPath = constants.CONFIG.STORAGE.THUMBNAILS_DIR + thumbnailName
|
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)
|
if (err) return callback(err)
|
||||||
|
|
||||||
return callback(null, thumbnailName)
|
return callback(null, thumbnailName)
|
||||||
|
|
Loading…
Reference in New Issue