spee.ch/helpers/publishHelpers.js

152 lines
4.4 KiB
JavaScript
Raw Normal View History

const logger = require('winston');
const fs = require('fs');
const { site, wallet } = 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}) {
2017-12-15 23:10:34 +01:00
logger.debug('file', 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,
};
},
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;
},
2018-03-06 00:38:26 +01:00
createBasicPublishParams (filePath, name, title, description, license, nsfw, thumbnail) {
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
if (thumbnail !== null) {
publishParams['metadata']['thumbnail'] = thumbnail;
}
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}`);
});
},
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,
};
},
};