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-08-16 20:00:17 +02:00
|
|
|
const db = require('../models');
|
2017-10-05 23:48:08 +02:00
|
|
|
const config = require('config');
|
2017-06-26 19:02:14 +02:00
|
|
|
|
|
|
|
module.exports = {
|
2017-10-05 23:48:08 +02:00
|
|
|
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) {
|
2017-10-05 23:48:08 +02:00
|
|
|
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':
|
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;
|
|
|
|
},
|
|
|
|
validateClaimName (claimName) {
|
|
|
|
const invalidCharacters = /[^A-Za-z0-9,-]/.exec(claimName);
|
2017-07-08 01:08:35 +02:00
|
|
|
if (invalidCharacters) {
|
2017-10-05 23:48:08 +02:00
|
|
|
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
|
|
|
}
|
2017-10-05 23:48:08 +02:00
|
|
|
},
|
|
|
|
validateLicense (license) {
|
2017-09-22 01:03:45 +02:00
|
|
|
if ((license.indexOf('Public Domain') === -1) && (license.indexOf('Creative Commons') === -1)) {
|
2017-10-05 23:48:08 +02:00
|
|
|
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
|
|
|
}
|
2017-10-05 23:48:08 +02:00
|
|
|
},
|
|
|
|
cleanseNSFW (nsfw) {
|
2017-07-08 01:08:35 +02:00
|
|
|
switch (nsfw) {
|
|
|
|
case true:
|
2017-10-05 23:48:08 +02:00
|
|
|
case 'on':
|
2017-07-08 01:08:35 +02:00
|
|
|
case 'true':
|
2017-10-05 23:48:08 +02:00
|
|
|
case 1:
|
|
|
|
case '1':
|
|
|
|
return true;
|
|
|
|
case false:
|
2017-07-08 01:08:35 +02:00
|
|
|
case 'false':
|
|
|
|
case 'off':
|
|
|
|
case 0:
|
|
|
|
case '0':
|
2017-10-05 23:48:08 +02:00
|
|
|
return false;
|
2017-07-08 01:08:35 +02:00
|
|
|
default:
|
2017-10-05 23:48:08 +02:00
|
|
|
return null;
|
2017-07-08 01:08:35 +02:00
|
|
|
}
|
|
|
|
},
|
2017-10-05 23:48:08 +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
|
|
|
}
|
2017-10-05 23:48:08 +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) {
|
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-08-21 01:56:37 +02:00
|
|
|
description = `${name} published via spee.ch`;
|
|
|
|
}
|
2017-10-05 23:48:08 +02:00
|
|
|
// 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
|
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-09-21 21:05:04 +02:00
|
|
|
author : 'spee.ch',
|
|
|
|
language: 'en',
|
2017-06-26 19:02:14 +02:00
|
|
|
license,
|
|
|
|
nsfw,
|
|
|
|
},
|
2017-10-05 23:48:08 +02:00
|
|
|
claim_address: config.get('WalletConfig.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-10-05 23:48:08 +02:00
|
|
|
// add channel to params, if applicable
|
|
|
|
if (channelName) {
|
|
|
|
publishParams['channel_name'] = channelName;
|
2017-09-22 01:03:45 +02:00
|
|
|
}
|
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 => {
|
|
|
|
if (err) 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) {
|
2017-09-27 21:12:42 +02:00
|
|
|
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
|
|
|
});
|
2017-09-27 21:12:42 +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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
2017-06-26 19:02:14 +02:00
|
|
|
};
|