From d28a5758dd7f80b90c87f113126e20e28c5858a0 Mon Sep 17 00:00:00 2001 From: bill bittner Date: Fri, 2 Mar 2018 17:24:18 -0800 Subject: [PATCH] moved thumbnail check to server side --- helpers/publishHelpers.js | 38 +++++++++++++++++++++++++++----- react/sagas/publish.js | 46 ++++----------------------------------- react/utils/publish.js | 6 ++++- routes/api-routes.js | 17 ++++++++++----- 4 files changed, 53 insertions(+), 54 deletions(-) diff --git a/helpers/publishHelpers.js b/helpers/publishHelpers.js index 390be4a3..88a93b1f 100644 --- a/helpers/publishHelpers.js +++ b/helpers/publishHelpers.js @@ -1,6 +1,6 @@ const logger = require('winston'); const fs = require('fs'); -const { site, wallet } = require('../config/speechConfig.js'); +const { site, wallet, publish } = require('../config/speechConfig.js'); module.exports = { parsePublishApiRequestBody ({name, nsfw, license, title, description, thumbnail}) { @@ -28,7 +28,7 @@ module.exports = { thumbnail, }; }, - parsePublishApiRequestFiles ({file}) { + parsePublishApiRequestFiles ({file, thumbnail}) { // make sure a file was provided if (!file) { throw new Error('no file with key of [file] found in request'); @@ -50,9 +50,12 @@ module.exports = { module.exports.validateFileTypeAndSize(file); // return results return { - fileName: file.name, - filePath: file.path, - fileType: file.type, + fileName : file.name, + filePath : file.path, + fileType : file.type, + thumbnailFileName: (thumbnail ? thumbnail.name : null), + thumbnailFilePath: (thumbnail ? thumbnail.path : null), + thumbnailFileType: (thumbnail ? thumbnail.type : null), }; }, parsePublishApiChannel ({channelName, channelPassword}, user) { @@ -138,7 +141,7 @@ module.exports = { claim_address: wallet.lbryClaimAddress, }; // add thumbnail to channel if video - if (thumbnail !== null) { + if (thumbnail) { publishParams['metadata']['thumbnail'] = thumbnail; } // add channel to params, if applicable @@ -147,6 +150,29 @@ module.exports = { } return publishParams; }, + createThumbnailPublishParams (thumbnailFilePath, claimName, license, nsfw) { + if (!thumbnailFilePath) { + return; + } + logger.debug(`Creating Thumbnail Publish Parameters`); + // create the publish params + return { + name : `${claimName}-thumb`, + file_path: thumbnailFilePath, + bid : 0.01, + metadata : { + title : `${claimName} thumbnail`, + description: `a thumbnail for ${claimName}`, + author : site.title, + language : 'en', + license, + nsfw, + }, + claim_address: wallet.lbryClaimAddress, + channel_name : publish.thumbnailChannel, + channel_id : publish.thumbnailChannelId, + }; + }, deleteTemporaryFile (filePath) { fs.unlink(filePath, err => { if (err) { diff --git a/react/sagas/publish.js b/react/sagas/publish.js index 073d9728..ae7d8fdd 100644 --- a/react/sagas/publish.js +++ b/react/sagas/publish.js @@ -34,20 +34,17 @@ function * publishFile (action) { publishMetadata['thumbnail'] = createThumbnailUrl(thumbnailChannel, thumbnailChannelId, claim, host); } // create form data for main publish - const publishFormData = createPublishFormData(file, publishMetadata); + const publishFormData = createPublishFormData(file, thumbnail, publishMetadata); // make the publish request const publishChannel = yield call(makePublishRequestChannel, publishFormData); - let publishInProgress = true; - while (publishInProgress) { + while (true) { const {loadStart, progress, load, success, error} = yield take(publishChannel); if (error) { - yield put(updatePublishStatus(publishStates.FAILED, error.message)); - publishInProgress = false; + return yield put(updatePublishStatus(publishStates.FAILED, error.message)); } if (success) { yield put(clearFile()); - history.push(`/${success.data.claimId}/${success.data.name}`); - publishInProgress = false; + return history.push(`/${success.data.claimId}/${success.data.name}`); } if (loadStart) { yield put(updatePublishStatus(publishStates.LOAD_START, null)); @@ -59,41 +56,6 @@ function * publishFile (action) { yield put(updatePublishStatus(publishStates.PUBLISHING, null)); } } - // publish thumbnail - if (thumbnail) { - // create form data for thumbnail publish - const thumbnailMetadata = { - name : `${claim}-thumb`, - title : `${claim} thumbnail`, - description : `a thumbnail for ${claim}`, - license : publishMetadata.license, - nsfw : publishMetadata.nsfw, - type : 'image/png', - channel_name: thumbnailChannel, - channel_id : thumbnailChannelId, - }; - const thumbnailFormData = createPublishFormData(thumbnail, thumbnailMetadata); - // make the publish reqeust - const thumbnailPublishChannel = yield call(makePublishRequestChannel, thumbnailFormData); - while (true) { - const {loadStart, progress, load, success, error} = yield take(thumbnailPublishChannel); - if (error) { - return console.log('thumbnail error:', error.message); - } - if (success) { - return console.log('thumbnail success:', success.data); - } - if (loadStart) { - console.log('thumbnail load started.'); - } - if (progress) { - console.log('thumbnail progress:', `${progress}%`); - } - if (load) { - console.log('thumbnail load completed.'); - } - } - } }; export function * watchPublishStart () { diff --git a/react/utils/publish.js b/react/utils/publish.js index c152ef5e..ecce371f 100644 --- a/react/utils/publish.js +++ b/react/utils/publish.js @@ -13,10 +13,14 @@ export const createPublishMetadata = (claim, { type }, { title, description, lic return metadata; }; -export const createPublishFormData = (file, metadata) => { +export const createPublishFormData = (file, thumbnail, metadata) => { let fd = new FormData(); // append file fd.append('file', file); + // append thumbnail + if (thumbnail) { + fd.append('thumbnail', thumbnail); + } // append metadata for (let key in metadata) { if (metadata.hasOwnProperty(key)) { diff --git a/routes/api-routes.js b/routes/api-routes.js index 9178834d..1ac79273 100644 --- a/routes/api-routes.js +++ b/routes/api-routes.js @@ -5,7 +5,7 @@ const multipartMiddleware = multipart({uploadDir: files.uploadDirectory}); const db = require('../models'); const { checkClaimNameAvailability, checkChannelAvailability, publish } = require('../controllers/publishController.js'); const { getClaimList, resolveUri, getClaim } = require('../helpers/lbryApi.js'); -const { createPublishParams, parsePublishApiRequestBody, parsePublishApiRequestFiles, parsePublishApiChannel, addGetResultsToFileData, createFileData } = require('../helpers/publishHelpers.js'); +const { addGetResultsToFileData, createFileData, createPublishParams, createThumbnailPublishParams, parsePublishApiRequestBody, parsePublishApiRequestFiles, parsePublishApiChannel } = require('../helpers/publishHelpers.js'); const errorHandlers = require('../helpers/errorHandlers.js'); const { sendGAAnonymousPublishTiming, sendGAChannelPublishTiming } = require('../helpers/googleAnalytics.js'); const { authenticateIfNoUserToken } = require('../auth/authentication.js'); @@ -135,14 +135,14 @@ module.exports = (app) => { logger.debug('api/claim-publish body:', body); logger.debug('api/claim-publish files:', files); // define variables - let name, fileName, filePath, fileType, nsfw, license, title, description, thumbnail, channelName, channelPassword; + let name, fileName, filePath, fileType, thumbnailFileName, thumbnailFilePath, thumbnailFileType, nsfw, license, title, description, thumbnail, channelName, channelPassword; // record the start time of the request const publishStartTime = Date.now(); // validate the body and files of the request try { // validateApiPublishRequest(body, files); ({name, nsfw, license, title, description, thumbnail} = parsePublishApiRequestBody(body)); - ({fileName, filePath, fileType} = parsePublishApiRequestFiles(files)); + ({fileName, filePath, fileType, thumbnailFileName, thumbnailFilePath, thumbnailFileType} = parsePublishApiRequestFiles(files)); ({channelName, channelPassword} = parsePublishApiChannel(body, user)); } catch (error) { return res.status(400).json({success: false, message: error.message}); @@ -161,9 +161,16 @@ module.exports = (app) => { throw new Error('That name is already claimed by another user.'); } // create publish parameters object - return createPublishParams(filePath, name, title, description, license, nsfw, thumbnail, channelName); + return Promise.all([ + createPublishParams(filePath, name, title, description, license, nsfw, thumbnail, channelName), + createThumbnailPublishParams(thumbnailFilePath, name, license, nsfw), + ]); }) - .then(publishParams => { + .then(([publishParams, thumbnailPublishParams]) => { + // publish the thumbnail + if (thumbnailPublishParams) { + publish(thumbnailPublishParams, thumbnailFileName, thumbnailFileType); + } // publish the asset return publish(publishParams, fileName, fileType); })