spee.ch/helpers/publishHelpers.js

204 lines
6.2 KiB
JavaScript
Raw Normal View History

const logger = require('winston');
const fs = require('fs');
2018-03-03 02:24:18 +01:00
const { site, wallet, publish } = require('../config/speechConfig.js');
module.exports = {
parsePublishApiRequestBody ({name, nsfw, license, title, description, thumbnail}) {
// validate name
if (!name) {
throw new Error('no name field found in request');
}
const invalidNameCharacters = /[^A-Za-z0-9,-]/.exec(name);
if (invalidNameCharacters) {
throw new Error('The claim name you provided is not allowed. Only the following characters are allowed: A-Z, a-z, 0-9, and "-"');
}
// optional parameters
nsfw = (nsfw === 'true');
license = license || null;
title = title || null;
description = description || null;
thumbnail = thumbnail || null;
// return results
return {
name,
nsfw,
license,
title,
description,
thumbnail,
};
},
2018-03-03 02:24:18 +01:00
parsePublishApiRequestFiles ({file, thumbnail}) {
// make sure a file was provided
if (!file) {
throw new Error('no file with key of [file] found in request');
}
if (!file.path) {
throw new Error('no file path found');
}
if (!file.type) {
throw new Error('no file type found');
}
if (!file.size) {
throw new Error('no file type found');
}
// validate the file name
if (/'/.test(file.name)) {
throw new Error('apostrophes are not allowed in the file name');
2017-07-08 01:08:35 +02:00
}
// validate the file
module.exports.validateFileTypeAndSize(file);
// return results
return {
2018-03-03 02:24:18 +01:00
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) {
logger.debug('publish api parser input:', {channelName, channelPassword, user});
// if anonymous or '' provided, publish will be anonymous (even if client is logged in)
// if a channel name is provided...
if (channelName) {
2017-12-15 20:02:04 +01:00
// make sure a password was provided if no user token is provided
if (!user && !channelPassword) {
throw new Error('Unauthenticated channel name provided without password');
}
2017-12-15 20:02:04 +01:00
// if request comes from the client with a token
// ensure this publish uses that channel name
if (user) {
channelName = user.channelName;
} ;
// add the @ if the channel name is missing it
if (channelName.indexOf('@') !== 0) {
channelName = `@${channelName}`;
}
}
return {
channelName,
channelPassword,
};
},
validateFileTypeAndSize (file) {
2017-07-08 01:08:35 +02:00
// check file type and size
switch (file.type) {
case 'image/jpeg':
case 'image/jpg':
2017-07-08 01:08:35 +02:00
case 'image/png':
if (file.size > 10000000) {
logger.debug('publish > file validation > .jpeg/.jpg/.png was too big');
throw new Error('Sorry, images are limited to 10 megabytes.');
}
break;
2017-07-08 01:08:35 +02:00
case 'image/gif':
2017-07-31 19:14:27 +02:00
if (file.size > 50000000) {
logger.debug('publish > file validation > .gif was too big');
throw new Error('Sorry, .gifs are limited to 50 megabytes.');
2017-07-08 01:08:35 +02:00
}
break;
case 'video/mp4':
if (file.size > 50000000) {
logger.debug('publish > file validation > .mp4 was too big');
throw new Error('Sorry, videos are limited to 50 megabytes.');
2017-07-08 01:08:35 +02:00
}
break;
default:
logger.debug('publish > file validation > unrecognized file type');
throw new Error('The ' + file.type + ' content type is not supported. Only, .jpeg, .png, .gif, and .mp4 files are currently supported.');
2017-07-08 01:08:35 +02:00
}
return file;
},
2017-10-10 03:29:40 +02:00
createPublishParams (filePath, name, title, description, license, nsfw, thumbnail, channelName) {
logger.debug(`Creating Publish Parameters`);
// provide defaults for title
if (title === null || title.trim() === '') {
2017-08-21 01:56:37 +02:00
title = name;
}
// provide default for description
2017-09-28 19:51:02 +02:00
if (description === null || description.trim() === '') {
description = '';
2017-08-21 01:56:37 +02:00
}
// provide default for license
if (license === null || license.trim() === '') {
2017-10-27 19:58:08 +02:00
license = ' '; // default to empty string
}
2017-08-21 01:56:37 +02:00
// create the publish params
const publishParams = {
name,
file_path: filePath,
bid : 0.01,
metadata : {
2017-08-21 01:56:37 +02:00
description,
title,
author : site.title,
language: 'en',
license,
nsfw,
},
claim_address: wallet.lbryClaimAddress,
};
2017-10-10 03:29:40 +02:00
// add thumbnail to channel if video
2018-03-03 02:24:18 +01:00
if (thumbnail) {
2017-10-10 03:29:40 +02:00
publishParams['metadata']['thumbnail'] = thumbnail;
}
// add channel to params, if applicable
if (channelName) {
publishParams['channel_name'] = channelName;
2017-09-22 01:03:45 +02:00
}
return publishParams;
},
2018-03-03 02:24:18 +01:00
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 => {
2017-10-12 22:10:44 +02:00
if (err) {
logger.error(`error deleting temporary file ${filePath}`);
throw err;
}
logger.debug(`successfully deleted ${filePath}`);
});
},
2018-01-23 21:08:53 +01:00
addGetResultsToFileData (fileInfo, getResult) {
fileInfo.fileName = getResult.file_name;
fileInfo.filePath = getResult.download_path;
return fileInfo;
},
createFileData ({ name, claimId, outpoint, height, address, nsfw, contentType }) {
return {
name,
claimId,
outpoint,
height,
address,
fileName: '',
filePath: '',
fileType: contentType,
nsfw,
};
},
};