spee.ch/helpers/publishHelpers.js

204 lines
6.1 KiB
JavaScript
Raw Normal View History

const logger = require('winston');
const fs = require('fs');
2017-08-16 20:00:17 +02:00
const db = require('../models');
const config = require('config');
module.exports = {
validateApiPublishRequest (body, files) {
if (!body) {
throw new Error('no body found in request');
}
if (!body.name) {
throw new Error('no name field found in request');
}
if (!body.nsfw) {
throw new Error('no nsfw field found in request');
}
if (!files) {
throw new Error('no files found in request');
}
if (!files.file) {
throw new Error('no file with key of [file] found in request');
}
},
validatePublishSubmission (file, claimName, nsfw) {
try {
module.exports.validateFile(file);
module.exports.validateClaimName(claimName);
module.exports.validateNSFW(nsfw);
} catch (error) {
throw error;
}
},
validateFile (file) {
2017-07-08 01:08:35 +02:00
if (!file) {
logger.debug('publish > file validation > no file found');
throw new Error('no file provided');
}
// check 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
}
// 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;
},
validateClaimName (claimName) {
const invalidCharacters = /[^A-Za-z0-9,-]/.exec(claimName);
2017-07-08 01:08:35 +02:00
if (invalidCharacters) {
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-07-08 01:08:35 +02:00
}
},
validateLicense (license) {
2017-09-22 01:03:45 +02:00
if ((license.indexOf('Public Domain') === -1) && (license.indexOf('Creative Commons') === -1)) {
throw new Error('Only posts with a "Public Domain" or "Creative Commons" license are eligible for publishing through spee.ch');
2017-07-08 01:08:35 +02:00
}
},
cleanseNSFW (nsfw) {
2017-07-08 01:08:35 +02:00
switch (nsfw) {
case true:
case 'on':
2017-07-08 01:08:35 +02:00
case 'true':
case 1:
case '1':
return true;
case false:
2017-07-08 01:08:35 +02:00
case 'false':
case 'off':
case 0:
case '0':
return false;
2017-07-08 01:08:35 +02:00
default:
return null;
2017-07-08 01:08:35 +02:00
}
},
cleanseChannelName (channelName) {
if (channelName) {
if (channelName.indexOf('@') !== 0) {
channelName = `@${channelName}`;
2017-09-28 19:51:02 +02:00
}
2017-06-27 00:01:41 +02:00
}
return channelName;
},
validateNSFW (nsfw) {
if (nsfw === true || nsfw === false) {
return;
}
throw new Error('NSFW must be set to either true or false');
},
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() === '') {
2017-08-21 01:56:37 +02:00
description = `${name} published via spee.ch`;
}
// provide default for license
if (license === null || license.trim() === '') {
license = 'All Rights Reserved';
}
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,
},
claim_address: config.get('WalletConfig.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}`);
});
},
2017-09-19 21:54:23 +02:00
checkClaimNameAvailability (name) {
2017-08-16 20:00:17 +02:00
return new Promise((resolve, reject) => {
// find any records where the name is used
db.File.findAll({ where: { name } })
.then(result => {
if (result.length >= 1) {
const claimAddress = config.get('WalletConfig.LbryClaimAddress');
// filter out any results that were not published from spee.ch's wallet address
const filteredResult = result.filter((claim) => {
return (claim.address === claimAddress);
2017-08-16 20:00:17 +02:00
});
// return based on whether any non-spee.ch claims were left
if (filteredResult.length >= 1) {
resolve(false);
} else {
resolve(true);
}
2017-08-16 20:00:17 +02:00
} else {
resolve(true);
}
})
.catch(error => {
reject(error);
});
});
},
2017-09-19 17:47:24 +02:00
checkChannelAvailability (name) {
return new Promise((resolve, reject) => {
// find any records where the name is used
2017-09-26 06:03:43 +02:00
db.Channel.findAll({ where: { channelName: name } })
2017-09-19 17:47:24 +02:00
.then(result => {
if (result.length >= 1) {
2017-09-19 21:54:23 +02:00
return resolve(false);
2017-09-19 17:47:24 +02:00
}
2017-09-19 21:54:23 +02:00
resolve(true);
2017-09-19 17:47:24 +02:00
})
.catch(error => {
reject(error);
});
});
},
};