spee.ch/helpers/publishHelpers.js

162 lines
4.9 KiB
JavaScript
Raw Normal View History

const logger = require('winston');
const fs = require('fs');
2017-11-07 00:18:45 +01:00
const config = 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,
};
},
parsePublishApiRequestFiles ({file}) {
// 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)) {
logger.debug('publish > file validation > file name had apostrophe in it');
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 {
fileName: file.name,
filePath: file.path,
fileType: file.type,
};
},
parsePublishApiChannel ({channelName, channelPassword}, user) {
// if no channel name provided, publish will be anonymous
// if a channel name is provided...
if (channelName) {
// make sure a password was provided
if (!channelPassword) {
throw new Error('Channel name provided without password');
}
// if request comes from the client and is logged in
// ensure it is the same channel and get the password
if (user) {
channelName = user.channelName;
channelPassword = user.channelPassword;
} ;
// 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 : 'spee.ch',
language: 'en',
license,
nsfw,
},
2017-11-07 00:18:45 +01:00
claim_address: config.wallet.lbryClaimAddress,
};
2017-10-10 03:29:40 +02:00
// add thumbnail to channel if video
if (thumbnail !== null) {
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;
},
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}`);
});
},
};