2017-06-26 19:02:14 +02:00
|
|
|
const logger = require('winston');
|
|
|
|
const config = require('config');
|
2017-06-28 00:53:53 +02:00
|
|
|
const fs = require('fs');
|
2017-08-16 20:00:17 +02:00
|
|
|
const db = require('../models');
|
|
|
|
const { getWalletList } = require('./lbryApi.js');
|
2017-06-26 19:02:14 +02:00
|
|
|
|
|
|
|
module.exports = {
|
2017-07-08 01:08:35 +02:00
|
|
|
validateFile (file, name, license, nsfw) {
|
|
|
|
if (!file) {
|
|
|
|
throw new Error('No file was submitted or the key used was incorrect. Files posted through this route must use a key of "speech" or null');
|
|
|
|
}
|
|
|
|
// check file type and size
|
|
|
|
switch (file.type) {
|
|
|
|
case 'image/jpeg':
|
|
|
|
case 'image/png':
|
|
|
|
case 'image/gif':
|
2017-07-31 19:14:27 +02:00
|
|
|
if (file.size > 50000000) {
|
|
|
|
throw new Error('Your image exceeds the 50 megabyte limit.');
|
2017-07-08 01:08:35 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'video/mp4':
|
|
|
|
if (file.size > 50000000) {
|
|
|
|
throw new Error('Your video exceeds the 50 megabyte limit.');
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Error('The ' + file.Type + ' content type is not supported. Only, .jpeg, .png, .gif, and .mp4 files are currently supported.');
|
|
|
|
}
|
2017-07-23 00:09:58 +02:00
|
|
|
// validate claim name
|
|
|
|
const invalidCharacters = /[^A-Za-z0-9,-]/.exec(name);
|
2017-07-08 01:08:35 +02:00
|
|
|
if (invalidCharacters) {
|
2017-07-23 00:09:58 +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
|
|
|
}
|
|
|
|
// validate license
|
2017-07-24 01:02:59 +02:00
|
|
|
if ((license.indexOf('Public Domain') === -1) && (license.indexOf('Creative Commons') === -1) && (license.indecOf('CC Attribution-NonCommercial 4.0 International') === -1)) {
|
|
|
|
throw new Error('Only posts with a "Public Domain" license, or one of the Creative Commons licenses are eligible for publishing through spee.ch');
|
2017-07-08 01:08:35 +02:00
|
|
|
}
|
|
|
|
switch (nsfw) {
|
|
|
|
case true:
|
|
|
|
case false:
|
|
|
|
case 'true':
|
|
|
|
case 'false':
|
|
|
|
case 'on':
|
|
|
|
case 'off':
|
|
|
|
case 0:
|
|
|
|
case '0':
|
|
|
|
case 1:
|
|
|
|
case '1':
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Error('NSFW value was not accepted. NSFW must be set to either true, false, "on", or "off"');
|
|
|
|
}
|
|
|
|
},
|
2017-08-21 01:56:37 +02:00
|
|
|
createPublishParams (name, filePath, title, description, license, nsfw) {
|
2017-06-26 19:02:14 +02:00
|
|
|
logger.debug(`Creating Publish Parameters for "${name}"`);
|
2017-06-28 21:55:17 +02:00
|
|
|
const claimAddress = config.get('WalletConfig.LbryClaimAddress');
|
2017-06-29 21:35:36 +02:00
|
|
|
// filter nsfw and ensure it is a boolean
|
2017-06-27 04:39:12 +02:00
|
|
|
if (nsfw === false) {
|
|
|
|
nsfw = false;
|
|
|
|
} else if (nsfw.toLowerCase === 'false') {
|
2017-06-27 04:26:37 +02:00
|
|
|
nsfw = false;
|
|
|
|
} else if (nsfw.toLowerCase === 'off') {
|
2017-06-27 00:01:41 +02:00
|
|
|
nsfw = false;
|
2017-06-27 04:26:37 +02:00
|
|
|
} else if (nsfw === 0) {
|
|
|
|
nsfw = false;
|
|
|
|
} else if (nsfw === '0') {
|
|
|
|
nsfw = false;
|
|
|
|
} else {
|
|
|
|
nsfw = true;
|
2017-06-27 00:01:41 +02:00
|
|
|
}
|
2017-08-21 01:56:37 +02:00
|
|
|
// provide defaults for title & description
|
|
|
|
if (title === '' || title === null) {
|
|
|
|
title = name;
|
|
|
|
}
|
|
|
|
if (description === '' || title === null) {
|
|
|
|
description = `${name} published via spee.ch`;
|
|
|
|
}
|
|
|
|
// 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,
|
|
|
|
author : 'spee.ch',
|
|
|
|
language: 'en',
|
2017-06-26 19:02:14 +02:00
|
|
|
license,
|
|
|
|
nsfw,
|
|
|
|
},
|
2017-06-29 19:51:23 +02:00
|
|
|
claim_address: claimAddress,
|
2017-06-29 21:35:36 +02:00
|
|
|
// change_address: changeAddress,
|
2017-06-26 19:02:14 +02:00
|
|
|
};
|
|
|
|
logger.debug('publishParams:', publishParams);
|
|
|
|
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-08-16 20:00:17 +02:00
|
|
|
checkNameAvailability (name) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
// find any records where the name is used
|
|
|
|
db.File.findAll({ where: { name } })
|
|
|
|
.then(result => {
|
|
|
|
if (result.length >= 1) {
|
|
|
|
// filter out any results that were not published from a spee.ch wallet address
|
|
|
|
getWalletList()
|
|
|
|
.then((walletList) => {
|
|
|
|
const filteredResult = result.filter((claim) => {
|
|
|
|
return walletList.includes(claim.address);
|
|
|
|
});
|
|
|
|
if (filteredResult.length >= 1) {
|
|
|
|
resolve(false);
|
|
|
|
} else {
|
|
|
|
resolve(true);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
resolve(true);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
2017-06-26 19:02:14 +02:00
|
|
|
};
|