diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-12-29 12:13:19 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-12-29 12:13:19 +0100 |
commit | 4d32448895ad29ef694bcf790d59253249ad5939 (patch) | |
tree | c81643455db459dd90f004a7d2669e22a092fa4f /server | |
parent | 98ac898a03ed7bbb4edec74fe823b3f2d6d4904a (diff) | |
download | PeerTube-4d32448895ad29ef694bcf790d59253249ad5939.tar.gz PeerTube-4d32448895ad29ef694bcf790d59253249ad5939.tar.zst PeerTube-4d32448895ad29ef694bcf790d59253249ad5939.zip |
Server: use binary data instead of base64 to send thumbnails
Diffstat (limited to 'server')
-rw-r--r-- | server/controllers/api/remote.js | 4 | ||||
-rw-r--r-- | server/controllers/api/users.js | 2 | ||||
-rw-r--r-- | server/helpers/custom-validators/videos.js | 9 | ||||
-rw-r--r-- | server/initializers/constants.js | 2 | ||||
-rw-r--r-- | server/models/video.js | 11 |
5 files changed, 13 insertions, 15 deletions
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) { | |||
147 | }, | 147 | }, |
148 | 148 | ||
149 | function generateThumbnail (t, tagInstances, video, callback) { | 149 | function generateThumbnail (t, tagInstances, video, callback) { |
150 | db.Video.generateThumbnailFromBase64(video, videoToCreateData.thumbnailBase64, function (err) { | 150 | db.Video.generateThumbnailFromData(video, videoToCreateData.thumbnailData, function (err) { |
151 | if (err) { | 151 | if (err) { |
152 | logger.error('Cannot generate thumbnail from base 64 data.', { error: err }) | 152 | logger.error('Cannot generate thumbnail from data.', { error: err }) |
153 | return callback(err) | 153 | return callback(err) |
154 | } | 154 | } |
155 | 155 | ||
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 @@ | |||
1 | 'use strict' | 1 | 'use strict' |
2 | 2 | ||
3 | const each = require('async/each') | ||
4 | const express = require('express') | 3 | const express = require('express') |
5 | const waterfall = require('async/waterfall') | 4 | const waterfall = require('async/waterfall') |
6 | 5 | ||
7 | const constants = require('../../initializers/constants') | 6 | const constants = require('../../initializers/constants') |
8 | const db = require('../../initializers/database') | 7 | const db = require('../../initializers/database') |
9 | const friends = require('../../lib/friends') | ||
10 | const logger = require('../../helpers/logger') | 8 | const logger = require('../../helpers/logger') |
11 | const middlewares = require('../../middlewares') | 9 | const middlewares = require('../../middlewares') |
12 | const admin = middlewares.admin | 10 | 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 = { | |||
17 | isVideoNameValid, | 17 | isVideoNameValid, |
18 | isVideoTagsValid, | 18 | isVideoTagsValid, |
19 | isVideoThumbnailValid, | 19 | isVideoThumbnailValid, |
20 | isVideoThumbnail64Valid | 20 | isVideoThumbnailDataValid |
21 | } | 21 | } |
22 | 22 | ||
23 | function isEachRemoteVideosValid (requests) { | 23 | function isEachRemoteVideosValid (requests) { |
@@ -33,7 +33,7 @@ function isEachRemoteVideosValid (requests) { | |||
33 | isVideoInfoHashValid(video.infoHash) && | 33 | isVideoInfoHashValid(video.infoHash) && |
34 | isVideoNameValid(video.name) && | 34 | isVideoNameValid(video.name) && |
35 | isVideoTagsValid(video.tags) && | 35 | isVideoTagsValid(video.tags) && |
36 | isVideoThumbnail64Valid(video.thumbnailBase64) && | 36 | isVideoThumbnailDataValid(video.thumbnailData) && |
37 | isVideoRemoteIdValid(video.remoteId) && | 37 | isVideoRemoteIdValid(video.remoteId) && |
38 | isVideoExtnameValid(video.extname) | 38 | isVideoExtnameValid(video.extname) |
39 | ) || | 39 | ) || |
@@ -86,9 +86,8 @@ function isVideoThumbnailValid (value) { | |||
86 | return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL) | 86 | return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL) |
87 | } | 87 | } |
88 | 88 | ||
89 | function isVideoThumbnail64Valid (value) { | 89 | function isVideoThumbnailDataValid (value) { |
90 | return validator.isBase64(value) && | 90 | return validator.isByteLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL_DATA) |
91 | validator.isByteLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL64) | ||
92 | } | 91 | } |
93 | 92 | ||
94 | function isVideoRemoteIdValid (value) { | 93 | 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 = { | |||
74 | TAGS: { min: 1, max: 3 }, // Number of total tags | 74 | TAGS: { min: 1, max: 3 }, // Number of total tags |
75 | TAG: { min: 2, max: 10 }, // Length | 75 | TAG: { min: 2, max: 10 }, // Length |
76 | THUMBNAIL: { min: 2, max: 30 }, | 76 | THUMBNAIL: { min: 2, max: 30 }, |
77 | THUMBNAIL64: { min: 0, max: 20000 } // Bytes | 77 | THUMBNAIL_DATA: { min: 0, max: 20000 } // Bytes |
78 | } | 78 | } |
79 | } | 79 | } |
80 | 80 | ||
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 @@ | |||
1 | 'use strict' | 1 | 'use strict' |
2 | 2 | ||
3 | const Buffer = require('safe-buffer').Buffer | ||
3 | const createTorrent = require('create-torrent') | 4 | const createTorrent = require('create-torrent') |
4 | const ffmpeg = require('fluent-ffmpeg') | 5 | const ffmpeg = require('fluent-ffmpeg') |
5 | const fs = require('fs') | 6 | const fs = require('fs') |
@@ -106,7 +107,7 @@ module.exports = function (sequelize, DataTypes) { | |||
106 | classMethods: { | 107 | classMethods: { |
107 | associate, | 108 | associate, |
108 | 109 | ||
109 | generateThumbnailFromBase64, | 110 | generateThumbnailFromData, |
110 | getDurationFromFile, | 111 | getDurationFromFile, |
111 | list, | 112 | list, |
112 | listForApi, | 113 | listForApi, |
@@ -336,7 +337,7 @@ function toFormatedJSON () { | |||
336 | function toRemoteJSON (callback) { | 337 | function toRemoteJSON (callback) { |
337 | const self = this | 338 | const self = this |
338 | 339 | ||
339 | // Convert thumbnail to base64 | 340 | // Get thumbnail data to send to the other pod |
340 | const thumbnailPath = pathUtils.join(constants.CONFIG.STORAGE.THUMBNAILS_DIR, this.getThumbnailName()) | 341 | const thumbnailPath = pathUtils.join(constants.CONFIG.STORAGE.THUMBNAILS_DIR, this.getThumbnailName()) |
341 | fs.readFile(thumbnailPath, function (err, thumbnailData) { | 342 | fs.readFile(thumbnailPath, function (err, thumbnailData) { |
342 | if (err) { | 343 | if (err) { |
@@ -351,7 +352,7 @@ function toRemoteJSON (callback) { | |||
351 | remoteId: self.id, | 352 | remoteId: self.id, |
352 | author: self.Author.name, | 353 | author: self.Author.name, |
353 | duration: self.duration, | 354 | duration: self.duration, |
354 | thumbnailBase64: new Buffer(thumbnailData).toString('base64'), | 355 | thumbnailData: thumbnailData.toString('binary'), |
355 | tags: map(self.Tags, 'name'), | 356 | tags: map(self.Tags, 'name'), |
356 | createdAt: self.createdAt, | 357 | createdAt: self.createdAt, |
357 | extname: self.extname | 358 | extname: self.extname |
@@ -363,12 +364,12 @@ function toRemoteJSON (callback) { | |||
363 | 364 | ||
364 | // ------------------------------ STATICS ------------------------------ | 365 | // ------------------------------ STATICS ------------------------------ |
365 | 366 | ||
366 | function generateThumbnailFromBase64 (video, thumbnailData, callback) { | 367 | function generateThumbnailFromData (video, thumbnailData, callback) { |
367 | // Creating the thumbnail for a remote video | 368 | // Creating the thumbnail for a remote video |
368 | 369 | ||
369 | const thumbnailName = video.getThumbnailName() | 370 | const thumbnailName = video.getThumbnailName() |
370 | const thumbnailPath = constants.CONFIG.STORAGE.THUMBNAILS_DIR + thumbnailName | 371 | const thumbnailPath = constants.CONFIG.STORAGE.THUMBNAILS_DIR + thumbnailName |
371 | fs.writeFile(thumbnailPath, thumbnailData, { encoding: 'base64' }, function (err) { | 372 | fs.writeFile(thumbnailPath, Buffer.from(thumbnailData, 'binary'), function (err) { |
372 | if (err) return callback(err) | 373 | if (err) return callback(err) |
373 | 374 | ||
374 | return callback(null, thumbnailName) | 375 | return callback(null, thumbnailName) |