2017-06-26 19:02:14 +02:00
|
|
|
const logger = require('winston');
|
2017-06-28 00:53:53 +02:00
|
|
|
const fs = require('fs');
|
2017-12-14 21:32:20 +01:00
|
|
|
const { site, wallet } = require('../config/speechConfig.js');
|
2017-06-26 19:02:14 +02:00
|
|
|
|
|
|
|
module.exports = {
|
2017-12-09 02:50:47 +01:00
|
|
|
parsePublishApiRequestBody ({name, nsfw, license, title, description, thumbnail}) {
|
|
|
|
// validate name
|
|
|
|
if (!name) {
|
2017-10-05 23:48:08 +02:00
|
|
|
throw new Error('no name field found in request');
|
|
|
|
}
|
2017-12-09 02:50:47 +01:00
|
|
|
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 "-"');
|
2017-10-05 23:48:08 +02:00
|
|
|
}
|
2017-12-09 02:50:47 +01:00
|
|
|
// 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);
|
2017-12-09 02:50:47 +01:00
|
|
|
// make sure a file was provided
|
|
|
|
if (!file) {
|
2017-10-05 23:48:08 +02:00
|
|
|
throw new Error('no file with key of [file] found in request');
|
|
|
|
}
|
2017-12-09 02:50:47 +01:00
|
|
|
if (!file.path) {
|
|
|
|
throw new Error('no file path found');
|
2017-10-05 23:48:08 +02:00
|
|
|
}
|
2017-12-09 02:50:47 +01:00
|
|
|
if (!file.type) {
|
|
|
|
throw new Error('no file type found');
|
|
|
|
}
|
|
|
|
if (!file.size) {
|
|
|
|
throw new Error('no file type found');
|
2017-10-05 23:48:08 +02:00
|
|
|
}
|
2017-12-09 02:50:47 +01:00
|
|
|
// validate the file name
|
2017-10-05 23:48:08 +02:00
|
|
|
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
|
|
|
}
|
2017-12-09 02:50:47 +01: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':
|
2017-10-05 23:48:08 +02:00
|
|
|
case 'image/jpg':
|
2017-07-08 01:08:35 +02:00
|
|
|
case 'image/png':
|
2017-10-05 23:48:08 +02:00
|
|
|
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) {
|
2017-10-05 23:48:08 +02:00
|
|
|
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) {
|
2017-10-05 23:48:08 +02:00
|
|
|
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:
|
2017-10-05 23:48:08 +02:00
|
|
|
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
|
|
|
}
|
2017-10-05 23:48:08 +02:00
|
|
|
return file;
|
|
|
|
},
|
2018-03-06 00:38:26 +01:00
|
|
|
createBasicPublishParams (filePath, name, title, description, license, nsfw, thumbnail) {
|
2017-10-05 23:48:08 +02:00
|
|
|
logger.debug(`Creating Publish Parameters`);
|
|
|
|
// provide defaults for title
|
|
|
|
if (title === null || title.trim() === '') {
|
2017-08-21 01:56:37 +02:00
|
|
|
title = name;
|
|
|
|
}
|
2017-10-05 23:48:08 +02:00
|
|
|
// provide default for description
|
2017-09-28 19:51:02 +02:00
|
|
|
if (description === null || description.trim() === '') {
|
2017-10-21 01:16:08 +02:00
|
|
|
description = '';
|
2017-08-21 01:56:37 +02:00
|
|
|
}
|
2017-10-05 23:48:08 +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-10-05 23:48:08 +02:00
|
|
|
}
|
2017-08-21 01:56:37 +02:00
|
|
|
// create the publish params
|
2017-06-26 19:02:14 +02:00
|
|
|
const publishParams = {
|
|
|
|
name,
|
|
|
|
file_path: filePath,
|
|
|
|
bid : 0.01,
|
|
|
|
metadata : {
|
2017-08-21 01:56:37 +02:00
|
|
|
description,
|
|
|
|
title,
|
2017-12-14 21:32:20 +01:00
|
|
|
author : site.title,
|
2017-09-21 21:05:04 +02:00
|
|
|
language: 'en',
|
2017-06-26 19:02:14 +02:00
|
|
|
license,
|
|
|
|
nsfw,
|
|
|
|
},
|
2017-12-14 21:32:20 +01:00
|
|
|
claim_address: wallet.lbryClaimAddress,
|
2017-06-26 19:02:14 +02:00
|
|
|
};
|
2017-10-10 03:29:40 +02:00
|
|
|
// add thumbnail to channel if video
|
|
|
|
if (thumbnail !== null) {
|
|
|
|
publishParams['metadata']['thumbnail'] = thumbnail;
|
|
|
|
}
|
2017-06-26 19:02:14 +02:00
|
|
|
return publishParams;
|
|
|
|
},
|
2017-07-04 03:27:35 +02:00
|
|
|
deleteTemporaryFile (filePath) {
|
2017-06-28 00:53:53 +02:00
|
|
|
fs.unlink(filePath, err => {
|
2017-10-12 22:10:44 +02:00
|
|
|
if (err) {
|
|
|
|
logger.error(`error deleting temporary file ${filePath}`);
|
|
|
|
throw err;
|
|
|
|
}
|
2017-06-28 00:53:53 +02:00
|
|
|
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,
|
|
|
|
};
|
|
|
|
},
|
2017-06-26 19:02:14 +02:00
|
|
|
};
|