Merge branch 'master' into undefined-og-description

This commit is contained in:
Bill Bittner 2017-11-13 13:57:09 -08:00 committed by GitHub
commit 1aa05b2b1b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
43 changed files with 713 additions and 795 deletions

View file

@ -14,13 +14,10 @@ spee.ch is a single-serving site that reads and publishes images and videos to a
* start spee.ch
* clone this repo
* run `npm install`
* to start the server, from your command line run `node speech.js` while passing three environmental variables:
* (1) your lbry wallet address (`LBRY_CLAIM_ADDRESS`),
* (2) your mysql username (`MYSQL_USERNAME`),
* (2) your mysql password (`MYSQL_PASSWORD`),
* (3) the environment to run (`NODE_ENV`).
* i.e. `LBRY_CLAIM_ADDRESS=<your wallet address here> MYSQL_USERNAME=<username here> MYSQL_PASSWORD=<password here> NODE_ENV=development node speech.js`
* e.g. `LBRY_CLAIM_ADDRESS=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX MYSQL_USERNAME="lbry" MYSQL_PASSWORD="xxxxxx" NODE_ENV=development node speech.js`
* create your `speechConfig.js` file
* copy `speechConfig.js.example` and name it `speechConfig.js`
* replace the `null` values in the config file with the appropriate values for your environement
* to start the server, from your command line run `node speech.js`
* To run hot, use `nodemon` instead of `node`
* visit [localhost:3000](http://localhost:3000)
@ -36,17 +33,17 @@ spee.ch is a single-serving site that reads and publishes images and videos to a
#### POST
* /api/publish
* example: `curl -X POST -F 'name=MyPictureName' -F 'nsfw=false' -F 'file=@/path/to/my/picture.jpeg' https://spee.ch/api/publish`
* example: `curl -X POST -F 'name=MyPictureName' -F 'file=@/path/to/myPicture.jpeg' https://spee.ch/api/publish`
* Parameters:
* name (string)
* nsfw (boolean)
* file (.mp4, .jpeg, .jpg, .gif, or .png)
* license (string, optional)
* title (string, optional)
* description (string, optional)
* thumbnail (string, optional) (for .mp4 uploads only)
* channelName(string, optional)
* channelPassword (string, optional)
* `name`
* `file` (.mp4, .jpeg, .jpg, .gif, or .png)
* `nsfw` (optional)
* `license` (optional)
* `title` (optional)
* `description` (optional)
* `thumbnail` url to thumbnail image, for .mp4 uploads only (optional)
* `channelName`(optional)
* `channelPassword` (optional,; required if `channelName` is provided)
## bugs
If you find a bug or experience a problem, please report your issue here on github and find us in the lbry slack!

View file

@ -4,10 +4,6 @@ const logger = require('winston');
module.exports = {
authenticateChannelCredentials (channelName, userPassword) {
return new Promise((resolve, reject) => {
if (!channelName) {
resolve(true);
return;
}
const userName = channelName.substring(1);
logger.debug(`authenticateChannelCredentials > channelName: ${channelName} username: ${userName} pass: ${userPassword}`);
db.User
@ -18,18 +14,32 @@ module.exports = {
resolve(false);
return;
}
if (!user.validPassword(userPassword, user.password)) {
logger.debug('incorrect password');
resolve(false);
return;
}
logger.debug('user found:', user.dataValues);
resolve(true);
return user.comparePassword(userPassword, (passwordErr, isMatch) => {
if (passwordErr) {
logger.error('comparePassword error:', passwordErr);
resolve(false);
return;
}
if (!isMatch) {
logger.debug('incorrect password');
resolve(false);
return;
}
logger.debug('...password was a match...');
resolve(true);
});
})
.catch(error => {
logger.error(error);
reject();
reject(error);
});
});
},
authenticateOrSkip (skipAuth, channelName, channelPassword) {
return new Promise((resolve, reject) => {
if (skipAuth) {
return resolve(true);
}
return resolve(module.exports.authenticateChannelCredentials(channelName, channelPassword));
});
},
};

View file

@ -1,15 +0,0 @@
{
"WalletConfig": {
"LbryClaimAddress": "LBRY_CLAIM_ADDRESS"
},
"Database": {
"Username": "MYSQL_USERNAME",
"Password": "MYSQL_PASSWORD"
},
"Logging": {
"SlackWebHook": "SLACK_WEB_HOOK"
},
"Session": {
"SessionKey": "SESSION_KEY"
}
}

View file

@ -1 +0,0 @@
{}

View file

@ -1,23 +0,0 @@
{
"WalletConfig": {
"LbryClaimAddress": null,
"DefaultChannel": null
},
"AnalyticsConfig":{
"GoogleId": null
},
"Database": {
"Database": "lbry",
"Username": null,
"Password": null
},
"Logging": {
"LogLevel": null,
"SlackWebHook": null,
"SlackErrorChannel": null,
"SlackInfoChannel": null
},
"Session": {
"SessionKey": null
}
}

View file

@ -1,13 +0,0 @@
{
"WalletConfig": {
"DefaultChannel": "@speechDev"
},
"AnalyticsConfig":{
"GoogleId": "UA-100747990-1"
},
"Logging": {
"LogLevel": "silly",
"SlackErrorChannel": "#staging_speech-errors",
"SlackInfoChannel": "none"
}
}

View file

@ -1,13 +0,0 @@
{
"WalletConfig": {
"DefaultChannel": "@speech"
},
"AnalyticsConfig":{
"GoogleId": "UA-60403362-3"
},
"Logging": {
"LogLevel": "verbose",
"SlackErrorChannel": "#speech-errors",
"SlackInfoChannel": "#speech-logs"
}
}

30
config/slackConfig.js Normal file
View file

@ -0,0 +1,30 @@
const config = require('./speechConfig.js');
const winstonSlackWebHook = require('winston-slack-webhook').SlackWebHook;
module.exports = (winston) => {
if (config.logging.slackWebHook) {
// add a transport for errors to slack
winston.add(winstonSlackWebHook, {
name : 'slack-errors-transport',
level : 'warn',
webhookUrl: config.logging.slackWebHook,
channel : config.logging.slackErrorChannel,
username : 'spee.ch',
iconEmoji : ':face_with_head_bandage:',
});
winston.add(winstonSlackWebHook, {
name : 'slack-info-transport',
level : 'info',
webhookUrl: config.logging.slackWebHook,
channel : config.logging.slackInfoChannel,
username : 'spee.ch',
iconEmoji : ':nerd_face:',
});
// send test message
winston.error('Slack "error" logging is online.');
winston.warn('Slack "warning" logging is online.');
winston.info('Slack "info" logging is online.');
} else {
winston.warn('Slack logging is not enabled because no SLACK_WEB_HOOK env var provided.');
}
};

View file

@ -1,32 +0,0 @@
const config = require('config');
const SLACK_WEB_HOOK = config.get('Logging.SlackWebHook');
const SLACK_ERROR_CHANNEL = config.get('Logging.SlackErrorChannel');
const SLACK_INFO_CHANNEL = config.get('Logging.SlackInfoChannel');
const winstonSlackWebHook = require('winston-slack-webhook').SlackWebHook;
module.exports = (winston) => {
if (SLACK_WEB_HOOK) {
// add a transport for errors to slack
winston.add(winstonSlackWebHook, {
name : 'slack-errors-transport',
level : 'error',
webhookUrl: SLACK_WEB_HOOK,
channel : SLACK_ERROR_CHANNEL,
username : 'spee.ch',
iconEmoji : ':face_with_head_bandage:',
});
winston.add(winstonSlackWebHook, {
name : 'slack-info-transport',
level : 'info',
webhookUrl: SLACK_WEB_HOOK,
channel : SLACK_INFO_CHANNEL,
username : 'spee.ch',
iconEmoji : ':nerd_face:',
});
// send test message
winston.error('Slack error logging is online.');
winston.info('Slack info logging is online.');
} else {
winston.error('Slack logging is not enabled because no SLACK_WEB_HOOK env var provided.');
}
};

View file

@ -0,0 +1,22 @@
module.exports = {
wallet: {
lbryClaimAddress: null, // choose an address from your lbry wallet
},
analytics: {
googleId: null, // google id for analytics tracking; leave `null` if not applicable
},
sql: {
database: null, // name of mysql database
username: null, // username for mysql
password: null, // password for mysql
},
logging: {
logLevel : null, // options: silly, debug, verbose, info
slackWebHook : null, // enter a webhook if you wish to push logs to slack; otherwise leave as `null`
slackErrorChannel: null, // enter a slack channel (#example) for errors to be sent to; otherwise leave null
slackInfoChannel : null, // enter a slack channel (#info) for info level logs to be sent to otherwise leave null
},
session: {
sessionKey: null, // enter a secret key to be used for session encryption
},
};

View file

@ -6,24 +6,33 @@ const publishHelpers = require('../helpers/publishHelpers.js');
module.exports = {
publish (publishParams, fileName, fileType) {
return new Promise((resolve, reject) => {
let publishResults = {};
// 1. publish the file
let publishResults, certificateId, channelName;
// publish the file
return lbryApi.publishClaim(publishParams)
// 2. upsert File record (update is in case the claim has been published before by this daemon)
.then(tx => {
logger.info(`Successfully published ${fileName}`, tx);
publishResults = tx;
return db.Channel.findOne({where: {channelName: publishParams.channel_name}}); // note: should this be db.User ??
// get the channel information
if (publishParams.channel_name) {
logger.debug(`this claim was published in channel: ${publishParams.channel_name}`);
return db.Channel.findOne({where: {channelName: publishParams.channel_name}});
} else {
logger.debug('this claim was published in channel: n/a');
return null;
}
})
.then(channel => {
let certificateId;
// set channel information
certificateId = null;
channelName = null;
if (channel) {
certificateId = channel.channelClaimId;
logger.debug('successfully found channel in Channel table');
} else {
certificateId = null;
logger.debug('channel for publish not found in Channel table');
};
channelName = channel.channelName;
}
logger.debug(`certificateId: ${certificateId}`);
})
.then(() => {
// create the File record
const fileRecord = {
name : publishParams.name,
claimId : publishResults.claim_id,
@ -37,6 +46,7 @@ module.exports = {
fileType,
nsfw : publishParams.metadata.nsfw,
};
// create the Claim record
const claimRecord = {
name : publishParams.name,
claimId : publishResults.claim_id,
@ -48,14 +58,16 @@ module.exports = {
height : 0,
contentType: fileType,
nsfw : publishParams.metadata.nsfw,
certificateId,
amount : publishParams.bid,
certificateId,
channelName,
};
// upsert criteria
const upsertCriteria = {
name : publishParams.name,
claimId: publishResults.claim_id,
};
// create the records
// upsert the records
return Promise.all([db.upsert(db.File, fileRecord, upsertCriteria, 'File'), db.upsert(db.Claim, claimRecord, upsertCriteria, 'Claim')]);
})
.then(([file, claim]) => {
@ -67,7 +79,6 @@ module.exports = {
resolve(publishResults); // resolve the promise with the result from lbryApi.publishClaim;
})
.catch(error => {
logger.error('caught a publishController.publish error');
publishHelpers.deleteTemporaryFile(publishParams.file_path); // delete the local file
reject(error);
});

View file

@ -67,12 +67,12 @@ function getAssetByLongClaimId (fullClaimId, name) {
db.Claim
.resolveClaim(name, fullClaimId)
.then(resolveResult => {
logger.debug('resolve result >> ', resolveResult.dataValues);
// if no result, return early (claim doesn't exist or isn't free)
if (!resolveResult) {
resolve(NO_CLAIM);
return;
}
logger.debug('resolve result >> ', resolveResult.dataValues);
let fileRecord = {};
// get the claim
lbryApi.getClaim(`${name}#${fullClaimId}`)

View file

@ -1,8 +1,8 @@
const logger = require('winston');
const ua = require('universal-analytics');
const config = require('config');
const config = require('../config/speechConfig.js');
const db = require('../models');
const googleApiKey = config.get('AnalyticsConfig.GoogleId');
const googleApiKey = config.analytics.googleId;
module.exports = {
postToStats (action, url, ipAddress, name, claimId, result) {

View file

@ -1,15 +1,13 @@
const config = require('config');
const config = require('../config/speechConfig.js');
const logger = require('winston');
const fs = require('fs');
module.exports = function () {
// get the config file
const defaultConfigFile = JSON.parse(fs.readFileSync('./config/default.json'));
for (let configCategoryKey in defaultConfigFile) {
if (defaultConfigFile.hasOwnProperty(configCategoryKey)) {
for (let configCategoryKey in config) {
if (config.hasOwnProperty(configCategoryKey)) {
// get the final variables for each config category
const configVariables = config.get(configCategoryKey);
const configVariables = config[configCategoryKey];
for (let configVarKey in configVariables) {
if (configVariables.hasOwnProperty(configVarKey)) {
// print each variable

View file

@ -2,38 +2,54 @@ const logger = require('winston');
const { postToStats } = require('../controllers/statsController.js');
module.exports = {
handleRequestError (action, originalUrl, ip, error, res) {
logger.error(`Request Error: ${originalUrl}`, module.exports.useObjectPropertiesIfNoKeys(error));
postToStats(action, originalUrl, ip, null, null, error);
if (error.response) {
res.status(error.response.status).render('requestError', {message: error.response.data.error.message, status: error.response.status});
} else if (error.code === 'ECONNREFUSED') {
res.status(503).render('requestError', {message: 'Connection refused. The daemon may not be running.', status: 503});
} else if (error.message) {
res.status(500).render('requestError', {message: error.message, status: 500});
} else {
res.status(400).render('requestError', {message: error, status: 400});
}
},
handlePublishError (error) {
logger.error('Publish Error:', module.exports.useObjectPropertiesIfNoKeys(error));
returnErrorMessageAndStatus: function (error) {
let status, message;
// check for daemon being turned off
if (error.code === 'ECONNREFUSED') {
return 'Connection refused. The daemon may not be running.';
status = 503;
message = 'Connection refused. The daemon may not be running.';
// check for errors from the deamon
} else if (error.response) {
status = error.response.status || 500;
if (error.response.data) {
if (error.response.data.message) {
return error.response.data.message;
message = error.response.data.message;
} else if (error.response.data.error) {
return error.response.data.error.message;
message = error.response.data.error.message;
} else {
message = error.response.data;
}
return error.response.data;
} else {
message = error.response;
}
return error.response;
// check for spee.ch thrown errors
} else if (error.message) {
status = 400;
message = error.message;
// fallback for everything else
} else {
return error;
status = 400;
message = error;
}
return [status, message];
},
useObjectPropertiesIfNoKeys (err) {
handleRequestError: function (action, originalUrl, ip, error, res) {
logger.error(`Request Error on ${originalUrl}`, module.exports.useObjectPropertiesIfNoKeys(error));
postToStats(action, originalUrl, ip, null, null, error);
const [status, message] = this.returnErrorMessageAndStatus(error);
res
.status(status)
.render('requestError', this.createErrorResponsePayload(status, message));
},
handleApiError: function (action, originalUrl, ip, error, res) {
logger.error(`Api ${action} Error on ${originalUrl}`, module.exports.useObjectPropertiesIfNoKeys(error));
postToStats(action, originalUrl, ip, null, null, error);
const [status, message] = this.returnErrorMessageAndStatus(error);
res
.status(status)
.json(this.createErrorResponsePayload(status, message));
},
useObjectPropertiesIfNoKeys: function (err) {
if (Object.keys(err).length === 0) {
let newErrorObject = {};
Object.getOwnPropertyNames(err).forEach((key) => {
@ -43,4 +59,11 @@ module.exports = {
}
return err;
},
createErrorResponsePayload (status, message) {
return {
status,
success: false,
message,
};
},
};

View file

@ -1,10 +1,10 @@
const Handlebars = require('handlebars');
const config = require('config');
const config = require('../config/speechConfig.js');
module.exports = {
// define any extra helpers you may need
googleAnalytics () {
const googleApiKey = config.get('AnalyticsConfig.GoogleId');
const googleApiKey = config.analytics.googleId;
return new Handlebars.SafeString(
`<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){

View file

@ -1,7 +1,7 @@
const logger = require('winston');
const fs = require('fs');
const db = require('../models');
const config = require('config');
const config = require('../config/speechConfig.js');
module.exports = {
validateApiPublishRequest (body, files) {
@ -11,9 +11,6 @@ module.exports = {
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');
}
@ -21,11 +18,10 @@ module.exports = {
throw new Error('no file with key of [file] found in request');
}
},
validatePublishSubmission (file, claimName, nsfw) {
validatePublishSubmission (file, claimName) {
try {
module.exports.validateFile(file);
module.exports.validateClaimName(claimName);
module.exports.validateNSFW(nsfw);
} catch (error) {
throw error;
}
@ -79,24 +75,6 @@ module.exports = {
throw new Error('Only posts with a "Public Domain" or "Creative Commons" license are eligible for publishing through spee.ch');
}
},
cleanseNSFW (nsfw) {
switch (nsfw) {
case true:
case 'on':
case 'true':
case 1:
case '1':
return true;
case false:
case 'false':
case 'off':
case 0:
case '0':
return false;
default:
return null;
}
},
cleanseChannelName (channelName) {
if (channelName) {
if (channelName.indexOf('@') !== 0) {
@ -105,12 +83,6 @@ module.exports = {
}
return channelName;
},
validateNSFW (nsfw) {
if (nsfw === true || nsfw === false) {
return;
}
throw new Error('NSFW must be set to either true or false');
},
createPublishParams (filePath, name, title, description, license, nsfw, thumbnail, channelName) {
logger.debug(`Creating Publish Parameters`);
// provide defaults for title
@ -138,7 +110,7 @@ module.exports = {
license,
nsfw,
},
claim_address: config.get('WalletConfig.LbryClaimAddress'),
claim_address: config.wallet.lbryClaimAddress,
};
// add thumbnail to channel if video
if (thumbnail !== null) {
@ -165,7 +137,7 @@ module.exports = {
db.File.findAll({ where: { name } })
.then(result => {
if (result.length >= 1) {
const claimAddress = config.get('WalletConfig.LbryClaimAddress');
const claimAddress = config.wallet.lbryClaimAddress;
// filter out any results that were not published from spee.ch's wallet address
const filteredResult = result.filter((claim) => {
return (claim.address === claimAddress);

View file

@ -298,7 +298,7 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, ARRAY, DECIMAL, D
case 1:
return resolve(result[0]);
default:
logger.error('more than one entry matches that name and claimID');
logger.warn(`more than one entry matches that name (${name}) and claimID (${claimId})`);
return resolve(result[0]);
}
})

View file

@ -2,13 +2,13 @@ const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const basename = path.basename(module.filename);
const config = require('config');
const config = require('../config/speechConfig.js');
const db = {};
const logger = require('winston');
const database = config.get('Database.Database');
const username = config.get('Database.Username');
const password = config.get('Database.Password');
const database = config.sql.database;
const username = config.sql.username;
const password = config.sql.password;
const sequelize = new Sequelize(database, username, password, {
host : 'localhost',
@ -66,7 +66,7 @@ db.upsert = (Model, values, condition, tableName) => {
}
})
.catch(function (error) {
logger.error('Sequelize findOne error', error);
logger.error(`${tableName}.upsert error`, error);
});
};

View file

@ -25,7 +25,6 @@ module.exports = (sequelize, { STRING }) => {
};
User.prototype.comparePassword = function (password, callback) {
logger.debug(`User.prototype.comparePassword ${password} ${this.password}`);
bcrypt.compare(password, this.password, callback);
};

View file

@ -34,7 +34,6 @@
"cookie-session": "^2.0.0-beta.3",
"express": "^4.15.2",
"express-handlebars": "^3.0.0",
"express-session": "^1.15.5",
"form-data": "^2.3.1",
"helmet": "^3.8.1",
"mysql2": "^1.3.5",
@ -44,8 +43,6 @@
"sequelize": "^4.1.0",
"sequelize-cli": "^3.0.0-3",
"sleep": "^5.1.1",
"socket.io": "^2.0.1",
"socketio-file-upload": "^0.6.0",
"universal-analytics": "^0.4.13",
"winston": "^2.3.1",
"winston-slack-webhook": "billbitt/winston-slack-webhook"

View file

@ -24,7 +24,7 @@ function publishNewChannel (event) {
// prevent default so this script can handle submission
event.preventDefault();
// validate submission
validateNewChannelSubmission(userName, password)
validationFunctions.validateNewChannelSubmission(userName, password)
.then(() => {
showChannelCreateInProgressDisplay();
return sendAuthRequest(userName, password, '/signup') // post the request

View file

@ -1,6 +1,3 @@
function triggerFileChooser(fileInputId, event) {
document.getElementById(fileInputId).click();
}
function drop_handler(event) {
event.preventDefault();
@ -9,7 +6,7 @@ function drop_handler(event) {
if (dt.items) {
if (dt.items[0].kind == 'file') {
var droppedFile = dt.items[0].getAsFile();
previewAndStageFile(droppedFile);
publishFileFunctions.previewAndStageFile(droppedFile);
}
}
}

View file

@ -8,7 +8,7 @@ function getRequest (url) {
if ( xhttp.status == 200) {
resolve(xhttp.response);
} else if (xhttp.status == 401) {
reject('Wrong username or password');
reject('Wrong channel name or password');
} else {
reject('request failed with status:' + xhttp.status);
};
@ -29,7 +29,7 @@ function postRequest (url, params) {
if ( xhttp.status == 200) {
resolve(xhttp.response);
} else if (xhttp.status == 401) {
reject( new AuthenticationError('Wrong username or password'));
reject( new AuthenticationError('Wrong channel name or password'));
} else {
reject('request failed with status:' + xhttp.status);
};

View file

@ -47,7 +47,7 @@ function loginToChannel (event) {
const password = document.getElementById('channel-login-password-input').value;
// prevent default
event.preventDefault()
validateNewChannelLogin(userName, password)
validationFunctions.validateNewChannelLogin(userName, password)
.then(() => {
// send request
return sendAuthRequest(userName, password, '/login')
@ -71,9 +71,9 @@ function loginToChannel (event) {
.catch(error => {
const loginErrorDisplayElement = document.getElementById('login-error-display-element');
if (error.name){
showError(loginErrorDisplayElement, error.message);
validationFunctions.showError(loginErrorDisplayElement, error.message);
} else {
showError(loginErrorDisplayElement, 'There was an error logging into your channel');
validationFunctions.showError(loginErrorDisplayElement, 'There was an error logging into your channel');
}
})
}

View file

@ -1,99 +1,231 @@
/* publish functions */
var stagedFiles = null;
function cancelPublish () {
window.location.href = '/';
}
// When a file is selected for publish, validate that file and
// stage it so it will be ready when the publish button is clicked.
function previewAndStageFile(selectedFile){
const publishForm = document.getElementById('publish-form');
const assetPreview = document.getElementById('asset-preview-target');
const primaryDropzone = document.getElementById('primary-dropzone');
const previewReader = new FileReader();
const nameInput = document.getElementById('claim-name-input');
const fileSelectionInputError = document.getElementById('input-error-file-selection');
const thumbnailSelectionTool = document.getElementById('publish-thumbnail');
const thumbnailSelectionInput = document.getElementById('claim-thumbnail-input');
// validate the file's name, type, and size
try {
validateFile(selectedFile);
} catch (error) {
showError(fileSelectionInputError, error.message);
return;
}
// set the image preview, if an image was provided
if (selectedFile.type !== 'video/mp4') {
if (selectedFile.type === 'image/gif') {
assetPreview.innerHTML = `<p>loading preview...</p>`
}
previewReader.readAsDataURL(selectedFile);
previewReader.onloadend = function () {
assetPreview.innerHTML = '<img id="asset-preview" src="' + previewReader.result + '" alt="image preview"/>';
};
// clear & hide the thumbnail selection input
thumbnailSelectionInput.value = '';
thumbnailSelectionTool.hidden = true;
} else {
assetPreview.innerHTML = `<img id="asset-preview" src="/assets/img/video_thumb_default.png"/>`;
// clear & show the thumbnail selection input
thumbnailSelectionInput.value = '';
thumbnailSelectionTool.hidden = false;
}
// hide the drop zone
primaryDropzone.setAttribute('class', 'hidden');
publishForm.setAttribute('class', 'row')
// set the name input value to the image name if none is set yet
if (nameInput.value === "") {
var filename = selectedFile.name.substring(0, selectedFile.name.indexOf('.'))
nameInput.value = cleanseClaimName(filename);
checkClaimName(nameInput.value);
}
// store the selected file for upload
stagedFiles = [selectedFile];
}
// Validate the publish submission and then trigger upload
function publishStagedFile(event) {
// prevent default so this script can handle submission
event.preventDefault();
// declare variables
const claimName = document.getElementById('claim-name-input').value;
let channelName = document.getElementById('channel-name-select').value;
const fileSelectionInputError = document.getElementById('input-error-file-selection');
const claimNameError = document.getElementById('input-error-claim-name');
const channelSelectError = document.getElementById('input-error-channel-select');
const publishSubmitError = document.getElementById('input-error-publish-submit');
let anonymousOrInChannel;
// replace channelName with 'anonymous' if appropriate
const radios = document.getElementsByName('anonymous-or-channel');
for (let i = 0; i < radios.length; i++) {
if (radios[i].checked) {
// do whatever you want with the checked radio
anonymousOrInChannel = radios[i].value;
// only one radio can be logically checked, don't check the rest
break;
const publishFileFunctions = {
triggerFileChooser: function (fileInputId) {
document.getElementById(fileInputId).click();
},
cancelPublish: function () {
window.location.href = '/';
},
previewAndStageFile: function (selectedFile) {
const fileSelectionInputError = document.getElementById('input-error-file-selection');
// When a file is selected for publish, validate that file and
// stage it so it will be ready when the publish button is clicked
try {
validationFunctions.validateFile(selectedFile); // validate the file's name, type, and size
} catch (error) {
validationFunctions.showError(fileSelectionInputError, error.message);
return;
}
}
if (anonymousOrInChannel === 'anonymous') {
channelName = null;
};
// validate, submit, and handle response
validateFilePublishSubmission(stagedFiles, claimName, channelName)
.then(() => {
uploader.submitFiles(stagedFiles);
})
.catch(error => {
if (error.name === 'FileError') {
showError(fileSelectionInputError, error.message);
} else if (error.name === 'NameError') {
showError(claimNameError, error.message);
} else if (error.name === 'ChannelNameError'){
console.log(error);
showError(channelSelectError, error.message);
} else {
showError(publishSubmitError, error.message);
}
return;
})
};
// set image preview, if an image was provided
this.setImagePreview(selectedFile);
// hide the primary drop zone
this.hidePrimaryDropzone();
// set the name input value to the image name if none is set yet
this.updateClaimNameInputWithFileName(selectedFile);
// store the selected file for upload
stagedFiles = [selectedFile];
},
hidePrimaryDropzone: function () {
const primaryDropzone = document.getElementById('primary-dropzone');
const publishForm = document.getElementById('publish-form');
primaryDropzone.setAttribute('class', 'hidden');
publishForm.setAttribute('class', 'row')
},
updateClaimNameInputWithFileName: function (selectedFile) {
const nameInput = document.getElementById('claim-name-input');
if (nameInput.value === "") {
var filename = selectedFile.name.substring(0, selectedFile.name.indexOf('.'))
nameInput.value = validationFunctions.cleanseClaimName(filename);
validationFunctions.checkClaimName(nameInput.value);
}
},
setImagePreview: function (selectedFile) {
const assetPreview = document.getElementById('asset-preview-target');
const thumbnailInput = document.getElementById('claim-thumbnail-input');
const thumbnailInputTool = document.getElementById('publish-thumbnail');
if (selectedFile.type !== 'video/mp4') {
const previewReader = new FileReader();
if (selectedFile.type === 'image/gif') {
assetPreview.innerHTML = `<p>loading preview...</p>`
}
previewReader.readAsDataURL(selectedFile);
previewReader.onloadend = function () {
assetPreview.innerHTML = '<img id="asset-preview" src="' + previewReader.result + '" alt="image preview"/>';
};
// clear & hide the thumbnail selection input
thumbnailInput.value = '';
thumbnailInputTool.hidden = true;
} else {
assetPreview.innerHTML = `<img id="asset-preview" src="/assets/img/video_thumb_default.png"/>`;
// clear & show the thumbnail selection input
thumbnailInput.value = '';
thumbnailInputTool.hidden = false;
}
},
returnNullOrChannel: function () {
const channelRadio = document.getElementById('channel-radio');
if (channelRadio.checked) {
const channelInput = document.getElementById('channel-name-select');
return channelInput.value.trim();
}
return null;
},
createMetadata: function() {
const nameInput = document.getElementById('claim-name-input');
const titleInput = document.getElementById('publish-title');
const descriptionInput = document.getElementById('publish-description');
const licenseInput = document.getElementById('publish-license');
const nsfwInput = document.getElementById('publish-nsfw');
const thumbnailInput = document.getElementById('claim-thumbnail-input');
return {
name: nameInput.value.trim(),
channelName: this.returnNullOrChannel(),
title: titleInput.value.trim(),
description: descriptionInput.value.trim(),
license: licenseInput.value.trim(),
nsfw: nsfwInput.checked,
type: stagedFiles[0].type,
thumbnail: thumbnailInput.value.trim(),
}
},
appendDataToFormData: function (file, metadata) {
var fd = new FormData();
fd.append('file', file)
for (var key in metadata) {
if (metadata.hasOwnProperty(key)) {
console.log(key, metadata[key]);
fd.append(key, metadata[key]);
}
}
return fd;
},
publishFile: function (file, metadata) {
var uri = "/api/publish";
var xhr = new XMLHttpRequest();
var fd = this.appendDataToFormData(file, metadata);
var that = this;
xhr.upload.addEventListener("loadstart", function(e) {
that.showUploadStartedMessage();
})
xhr.upload.addEventListener("progress", function(e) {
if (e.lengthComputable) {
var percentage = Math.round((e.loaded * 100) / e.total);
console.log('progress:', percentage);
that.showUploadProgressMessage(percentage);
}
}, false);
xhr.upload.addEventListener("load", function(e){
console.log('loaded 100%');
that.showFilePublishUpdate("Your file has been loaded, and is now being published to the blockchain. Sit tight...")
}, false);
xhr.open("POST", uri, true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
console.log('publish complete!');
that.showFilePublishComplete(JSON.parse(xhr.response).message);
} else {
console.log(xhr.response);
that.showFilePublishFailure(JSON.parse(xhr.response).message);
}
} else {
console.log('xhr.readyState', xhr.readyState, 'xhr.status', xhr.status);
}
};
// Initiate a multipart/form-data upload
xhr.send(fd);
},
// Validate the publish submission and then trigger upload
publishStagedFile: function (event) {
event.preventDefault(); // prevent default so this script can handle submission
const metadata = this.createMetadata();
const that = this; // note: necessary ?
const fileSelectionInputError = document.getElementById('input-error-file-selection');
const claimNameError = document.getElementById('input-error-claim-name');
const channelSelectError = document.getElementById('input-error-channel-select');
const publishSubmitError = document.getElementById('input-error-publish-submit');
// validate, submit, and handle response
validationFunctions.validateFilePublishSubmission(stagedFiles, metadata)
.then( function() {
that.publishFile(stagedFiles[0], metadata);
})
.catch(error => {
if (error.name === 'FileError') {
validationFunctions.showError(fileSelectionInputError, error.message);
} else if (error.name === 'NameError') {
validationFunctions.showError(claimNameError, error.message);
} else if (error.name === 'ChannelNameError'){
console.log(error);
validationFunctions.showError(channelSelectError, error.message);
} else {
validationFunctions.showError(publishSubmitError, error.message);
}
return;
})
},
showUploadStartedMessage: function (){
console.log('starting upload');
// hide the publish tool
this.hidePublishTools();
// show the progress status and animation
this.showPublishStatus();
this.showPublishProgressBar();
},
showUploadProgressMessage: function (percentage){
this.updatePublishStatus('<p>File is loading to server</p>');
this.updateUploadPercent('<p class="blue">' + percentage + '% </p>');
},
showFilePublishUpdate: function (msg) {
this.updatePublishStatus('<p>' + msg + '</p>');
this.updateUploadPercent('<p>Curious what magic is happening here? <a class="link--primary" target="blank" href="https://lbry.io/faq/what-is-lbry">Learn more.</a></p>');
},
showFilePublishFailure: function (msg){
this.updatePublishStatus('<p>Something went wrong...</p><p><strong>' + msg + '</strong></p><p>For help, post the above error text in the #speech channel on the <a class="link--primary" href="https://discord.gg/YjYbwhS" target="_blank">lbry discord</a>');
this.hidePublishProgressBar();
this.hideUploadPercent();
},
showFilePublishComplete: function (msg) {
console.log('Publish complete!');
const showUrl = msg.lbryTx.claim_id + "/" + msg.name;
// update status
this.updatePublishStatus('<p>Your publish is complete! You are being redirected to it now.</p>');
this.updateUploadPercent('<p><a class="link--primary" target="_blank" href="' + showUrl + '">If you do not get redirected, click here.</a></p>')
// redirect the user
window.location.href = showUrl;
},
hidePublishTools: function () {
const publishFormWrapper = document.getElementById('publish-form');
publishFormWrapper.setAttribute('class', 'hidden');
},
// publish status functions
showPublishStatus: function () {
const publishStatus = document.getElementById('publish-status');
publishStatus.setAttribute('class', 'row row--tall flex-container--column flex-container--center-center');
},
updatePublishStatus: function (msg){
const publishUpdate = document.getElementById('publish-update');
publishUpdate.innerHTML = msg;
},
// progress bar functions
showPublishProgressBar: function (){
const publishProgressBar = document.getElementById('publish-progress-bar');
createProgressBar(publishProgressBar, 12);
},
hidePublishProgressBar: function (){
const publishProgressBar = document.getElementById('publish-progress-bar');
publishProgressBar.hidden = true;
},
// upload percent functions
updateUploadPercent: function (msg){
const uploadPercent = document.getElementById('upload-percent');
uploadPercent.innerHTML = msg;
},
hideUploadPercent: function (){
const uploadPercent = document.getElementById('upload-percent');
uploadPercent.hidden = true;
},
}

View file

@ -1,236 +1,231 @@
// validation function which checks the proposed file's type, size, and name
function validateFile(file) {
if (!file) {
console.log('no file found');
throw new Error('no file provided');
}
if (/'/.test(file.name)) {
console.log('file name had apostrophe in it');
throw new Error('apostrophes are not allowed in the file name');
}
// validate size and type
switch (file.type) {
case 'image/jpeg':
case 'image/jpg':
case 'image/png':
if (file.size > 10000000){
console.log('file was too big');
throw new Error('Sorry, images are limited to 10 megabytes.');
}
break;
case 'image/gif':
if (file.size > 50000000){
console.log('file was too big');
throw new Error('Sorry, .gifs are limited to 50 megabytes.');
}
break;
case 'video/mp4':
if (file.size > 50000000){
console.log('file was too big');
throw new Error('Sorry, videos are limited to 50 megabytes.');
}
break;
default:
console.log('file type is not supported');
throw new Error(file.type + ' is not a supported file type. Only, .jpeg, .png, .gif, and .mp4 files are currently supported.')
}
}
// validation function that checks to make sure the claim name is valid
function validateClaimName (name) {
// ensure a name was entered
if (name.length < 1) {
throw new NameError("You must enter a name for your url");
}
// validate the characters in the 'name' field
const invalidCharacters = /[^A-Za-z0-9,-]/g.exec(name);
if (invalidCharacters) {
throw new NameError('"' + invalidCharacters + '" characters are not allowed');
}
}
function validateChannelName (name) {
name = name.substring(name.indexOf('@') + 1);
// ensure a name was entered
if (name.length < 1) {
throw new ChannelNameError("You must enter a name for your channel");
}
// validate the characters in the 'name' field
const invalidCharacters = /[^A-Za-z0-9,-,@]/g.exec(name);
if (invalidCharacters) {
throw new ChannelNameError('"' + invalidCharacters + '" characters are not allowed');
}
}
function validatePassword (password) {
if (password.length < 1) {
throw new ChannelPasswordError("You must enter a password for you channel");
}
}
function cleanseClaimName(name) {
name = name.replace(/\s+/g, '-'); // replace spaces with dashes
name = name.replace(/[^A-Za-z0-9-]/g, ''); // remove all characters that are not A-Z, a-z, 0-9, or '-'
return name;
}
// validation functions to check claim & channel name eligibility as the inputs change
function isNameAvailable (name, apiUrl) {
const url = apiUrl + name;
return getRequest(url)
}
function showError(errorDisplay, errorMsg) {
errorDisplay.hidden = false;
errorDisplay.innerText = errorMsg;
}
function hideError(errorDisplay) {
errorDisplay.hidden = true;
errorDisplay.innerText = '';
}
function showSuccess (successElement) {
successElement.hidden = false;
successElement.innerHTML = "&#x2714";
}
function hideSuccess (successElement) {
successElement.hidden = true;
successElement.innerHTML = "";
}
function checkAvailability(name, successDisplayElement, errorDisplayElement, validateName, isNameAvailable, errorMessage, apiUrl) {
try {
// check to make sure the characters are valid
validateName(name);
// check to make sure it is available
isNameAvailable(name, apiUrl)
.then(result => {
if (result === true) {
hideError(errorDisplayElement);
showSuccess(successDisplayElement)
} else {
hideSuccess(successDisplayElement);
showError(errorDisplayElement, errorMessage);
const validationFunctions = {
validateFile: function (file) {
if (!file) {
console.log('no file found');
throw new Error('no file provided');
}
if (/'/.test(file.name)) {
console.log('file name had apostrophe in it');
throw new Error('apostrophes are not allowed in the file name');
}
// validate size and type
switch (file.type) {
case 'image/jpeg':
case 'image/jpg':
case 'image/png':
if (file.size > 10000000) {
console.log('file was too big');
throw new Error('Sorry, images are limited to 10 megabytes.');
}
})
.catch(error => {
hideSuccess(successDisplayElement);
showError(errorDisplayElement, error.message);
});
} catch (error) {
hideSuccess(successDisplayElement);
showError(errorDisplayElement, error.message);
}
}
function checkClaimName(name){
const successDisplayElement = document.getElementById('input-success-claim-name');
const errorDisplayElement = document.getElementById('input-error-claim-name');
checkAvailability(name, successDisplayElement, errorDisplayElement, validateClaimName, isNameAvailable, 'Sorry, that ending is already taken', '/api/isClaimAvailable/');
}
function checkChannelName(name){
const successDisplayElement = document.getElementById('input-success-channel-name');
const errorDisplayElement = document.getElementById('input-error-channel-name');
name = `@${name}`;
checkAvailability(name, successDisplayElement, errorDisplayElement, validateChannelName, isNameAvailable, 'Sorry, that name is already taken', '/api/isChannelAvailable/');
}
// validation function which checks all aspects of the publish submission
function validateFilePublishSubmission(stagedFiles, claimName, channelName){
return new Promise(function (resolve, reject) {
// 1. make sure 1 file was staged
if (!stagedFiles) {
reject(new FileError("Please select a file"));
return;
} else if (stagedFiles.length > 1) {
reject(new FileError("Only one file is allowed at a time"));
return;
}
// 2. validate the file's name, type, and size
try {
validateFile(stagedFiles[0]);
} catch (error) {
reject(error);
return;
}
// 3. validate that a channel was chosen
if (channelName === 'new' || channelName === 'login') {
reject(new ChannelNameError("Please log in to a channel"));
return;
};
// 4. validate the claim name
try {
validateClaimName(claimName);
} catch (error) {
reject(error);
return;
}
// if all validation passes, check availability of the name (note: do we need to re-validate channel name vs. credentials as well?)
return isNameAvailable(claimName, '/api/isClaimAvailable/')
.then(result => {
if (result) {
resolve();
} else {
reject(new NameError('Sorry, that ending is already taken'));
break;
case 'image/gif':
if (file.size > 50000000) {
console.log('file was too big');
throw new Error('Sorry, .gifs are limited to 50 megabytes.');
}
})
.catch(error => {
break;
case 'video/mp4':
if (file.size > 50000000) {
console.log('file was too big');
throw new Error('Sorry, videos are limited to 50 megabytes.');
}
break;
default:
console.log('file type is not supported');
throw new Error(file.type + ' is not a supported file type. Only, .jpeg, .png, .gif, and .mp4 files are currently supported.')
}
},
// validation function that checks to make sure the claim name is valid
validateClaimName: function (name) {
console.log('validating the claim name');
// ensure a name was entered
if (name.length < 1) {
throw new NameError("You must enter a name for your url");
}
// validate the characters in the 'name' field
const invalidCharacters = /[^A-Za-z0-9,-]/g.exec(name);
if (invalidCharacters) {
throw new NameError('"' + invalidCharacters + '" characters are not allowed');
}
},
validateChannelName: function (name) {
name = name.substring(name.indexOf('@') + 1);
// ensure a name was entered
if (name.length < 1) {
throw new ChannelNameError("You must enter a name for your channel");
}
// validate the characters in the 'name' field
const invalidCharacters = /[^A-Za-z0-9,-,@]/g.exec(name);
if (invalidCharacters) {
throw new ChannelNameError('"' + invalidCharacters + '" characters are not allowed');
}
},
validatePassword: function (password) {
if (password.length < 1) {
throw new ChannelPasswordError("You must enter a password for you channel");
}
},
cleanseClaimName: function (name) {
name = name.replace(/\s+/g, '-'); // replace spaces with dashes
name = name.replace(/[^A-Za-z0-9-]/g, ''); // remove all characters that are not A-Z, a-z, 0-9, or '-'
return name;
},
// validation functions to check claim & channel name eligibility as the inputs change
isNameAvailable: function (name, apiUrl) {
const url = apiUrl + name;
return getRequest(url)
},
showError: function (errorDisplay, errorMsg) {
errorDisplay.hidden = false;
errorDisplay.innerText = errorMsg;
},
hideError: function (errorDisplay) {
errorDisplay.hidden = true;
errorDisplay.innerText = '';
},
showSuccess: function (successElement) {
successElement.hidden = false;
successElement.innerHTML = "&#x2714";
},
hideSuccess: function (successElement) {
successElement.hidden = true;
successElement.innerHTML = "";
},
checkAvailability: function (name, successDisplayElement, errorDisplayElement, validateName, errorMessage, apiUrl) {
var that = this;
try {
// check to make sure the characters are valid
validateName(name);
// check to make sure it is available
that.isNameAvailable(name, apiUrl)
.then(function (result) {
if (result === true) {
that.hideError(errorDisplayElement);
that.showSuccess(successDisplayElement)
} else {
that.hideSuccess(successDisplayElement);
that.showError(errorDisplayElement, errorMessage);
}
})
.catch(error => {
that.hideSuccess(successDisplayElement);
that.showError(errorDisplayElement, error.message);
});
} catch (error) {
that.hideSuccess(successDisplayElement);
that.showError(errorDisplayElement, error.message);
}
},
checkClaimName: function (name) {
const successDisplayElement = document.getElementById('input-success-claim-name');
const errorDisplayElement = document.getElementById('input-error-claim-name');
this.checkAvailability(name, successDisplayElement, errorDisplayElement, this.validateClaimName, 'Sorry, that ending is already taken', '/api/isClaimAvailable/');
},
checkChannelName: function (name) {
const successDisplayElement = document.getElementById('input-success-channel-name');
const errorDisplayElement = document.getElementById('input-error-channel-name');
name = `@${name}`;
this.checkAvailability(name, successDisplayElement, errorDisplayElement, this.validateChannelName, 'Sorry, that name is already taken', '/api/isChannelAvailable/');
},
// validation function which checks all aspects of the publish submission
validateFilePublishSubmission: function (stagedFiles, metadata) {
const channelName = metadata.channelName;
const claimName = metadata.name;
var that = this;
return new Promise(function (resolve, reject) {
// 1. make sure 1 file was staged
if (!stagedFiles) {
reject(new FileError("Please select a file"));
return;
} else if (stagedFiles.length > 1) {
reject(new FileError("Only one file is allowed at a time"));
return;
}
// 2. validate the file's name, type, and size
try {
that.validateFile(stagedFiles[0]);
} catch (error) {
reject(error);
});
});
}
// validation function which checks all aspects of a new channel submission
function validateNewChannelSubmission(userName, password){
const channelName = `@${userName}`;
return new Promise(function (resolve, reject) {
// 1. validate name
try {
validateChannelName(channelName);
} catch (error) {
return reject(error);
}
// 2. validate password
try {
validatePassword(password);
} catch (error) {
return reject(error);
}
// 3. if all validation passes, check availability of the name
isNameAvailable(channelName, '/api/isChannelAvailable/') // validate the availability
.then(result => {
if (result) {
resolve();
} else {
reject(new ChannelNameError('Sorry, that name is already taken'));
}
})
.catch( error => {
console.log('error evaluating channel name availability', error);
return;
}
// 3. validate that a channel was chosen
if (channelName === 'new' || channelName === 'login') {
reject(new ChannelNameError("Please log in to a channel"));
return;
}
;
// 4. validate the claim name
try {
that.validateClaimName(claimName);
} catch (error) {
reject(error);
});
});
}
// validation function which checks all aspects of a new channel login
function validateNewChannelLogin(userName, password){
const channelName = `@${userName}`;
return new Promise(function (resolve, reject) {
// 1. validate name
try {
validateChannelName(channelName);
} catch (error) {
return reject(error);
}
// 2. validate password
try {
validatePassword(password);
} catch (error) {
return reject(error);
}
resolve();
});
}
return;
}
// if all validation passes, check availability of the name (note: do we need to re-validate channel name vs. credentials as well?)
return that.isNameAvailable(claimName, '/api/isClaimAvailable/')
.then(result => {
if (result) {
resolve();
} else {
reject(new NameError('Sorry, that ending is already taken'));
}
})
.catch(error => {
reject(error);
});
});
},
// validation function which checks all aspects of a new channel submission
validateNewChannelSubmission: function (userName, password) {
const channelName = `@${userName}`;
var that = this;
return new Promise(function (resolve, reject) {
// 1. validate name
try {
that.validateChannelName(channelName);
} catch (error) {
return reject(error);
}
// 2. validate password
try {
that.validatePassword(password);
} catch (error) {
return reject(error);
}
// 3. if all validation passes, check availability of the name
that.isNameAvailable(channelName, '/api/isChannelAvailable/') // validate the availability
.then(function(result) {
if (result) {
resolve();
} else {
reject(new ChannelNameError('Sorry, that name is already taken'));
}
})
.catch(function(error) {
console.log('error evaluating channel name availability', error);
reject(error);
});
});
},
// validation function which checks all aspects of a new channel login
validateNewChannelLogin: function (userName, password) {
const channelName = `@${userName}`;
var that = this;
return new Promise(function (resolve, reject) {
// 1. validate name
try {
that.validateChannelName(channelName);
} catch (error) {
return reject(error);
}
// 2. validate password
try {
that.validatePassword(password);
} catch (error) {
return reject(error);
}
resolve();
});
}
};

View file

@ -1,13 +1,13 @@
const logger = require('winston');
const multipart = require('connect-multiparty');
const multipartMiddleware = multipart();
const multipartMiddleware = multipart({uploadDir: '/home/lbry/test/'});
const db = require('../models');
const { publish } = require('../controllers/publishController.js');
const { getClaimList, resolveUri } = require('../helpers/lbryApi.js');
const { createPublishParams, validateApiPublishRequest, validatePublishSubmission, cleanseNSFW, cleanseChannelName, checkClaimNameAvailability, checkChannelAvailability } = require('../helpers/publishHelpers.js');
const { createPublishParams, validateApiPublishRequest, validatePublishSubmission, cleanseChannelName, checkClaimNameAvailability, checkChannelAvailability } = require('../helpers/publishHelpers.js');
const errorHandlers = require('../helpers/errorHandlers.js');
const { postToStats, sendGoogleAnalytics } = require('../controllers/statsController.js');
const { authenticateChannelCredentials } = require('../auth/authentication.js');
const { authenticateOrSkip } = require('../auth/authentication.js');
module.exports = (app) => {
// route to run a claim_list request on the daemon
@ -21,7 +21,7 @@ module.exports = (app) => {
res.status(200).json(claimsList);
})
.catch(error => {
errorHandlers.handleRequestError('publish', originalUrl, ip, error, res);
errorHandlers.handleApiError('claim_list', originalUrl, ip, error, res);
});
});
// route to check whether spee.ch has published to a claim
@ -67,15 +67,13 @@ module.exports = (app) => {
res.status(200).json(resolvedUri);
})
.catch(error => {
errorHandlers.handleRequestError('publish', originalUrl, ip, error, res);
errorHandlers.handleApiError('resolve', originalUrl, ip, error, res);
});
});
// route to run a publish request on the daemon
app.post('/api/publish', multipartMiddleware, (req, res) => {
logger.debug('req:', req);
app.post('/api/publish', multipartMiddleware, ({ body, files, ip, originalUrl, user }, res) => {
let file, fileName, filePath, fileType, name, nsfw, license, title, description, thumbnail, anonymous, skipAuth, channelName, channelPassword;
// validate that mandatory parts of the request are present
const body = req.body;
const files = req.files;
try {
validateApiPublishRequest(body, files);
} catch (error) {
@ -84,13 +82,12 @@ module.exports = (app) => {
return;
}
// validate file, name, license, and nsfw
const file = files.file;
const fileName = file.name;
const filePath = file.path;
const fileType = file.type;
const name = body.name;
let nsfw = body.nsfw;
nsfw = cleanseNSFW(nsfw); // cleanse nsfw
file = files.file;
fileName = file.path.substring(file.path.lastIndexOf('/') + 1);
filePath = file.path;
fileType = file.type;
name = body.name;
nsfw = (body.nsfw === 'true');
try {
validatePublishSubmission(file, name, nsfw);
} catch (error) {
@ -98,20 +95,38 @@ module.exports = (app) => {
res.status(400).json({success: false, message: error.message});
return;
}
logger.debug(`name: ${name}, nsfw: ${nsfw}`);
// optional inputs
const license = body.license || null;
const title = body.title || null;
const description = body.description || null;
const thumbnail = body.thumbnail || null;
let channelName = body.channelName || null;
license = body.license || null;
title = body.title || null;
description = body.description || null;
thumbnail = body.thumbnail || null;
anonymous = (body.channelName === 'null') || (body.channelName === undefined);
if (user) {
channelName = user.channelName || null;
} else {
channelName = body.channelName || null;
}
channelPassword = body.channelPassword || null;
skipAuth = false;
// case 1: publish from spee.ch, client logged in
if (user) {
skipAuth = true;
if (anonymous) {
channelName = null;
}
// case 2: publish from api or spee.ch, client not logged in
} else {
if (anonymous) {
skipAuth = true;
channelName = null;
}
}
channelName = cleanseChannelName(channelName);
const channelPassword = body.channelPassword || null;
logger.debug(`license: ${license} title: "${title}" description: "${description}" channelName: "${channelName}" channelPassword: "${channelPassword}"`);
logger.debug(`/api/publish > name: ${name}, license: ${license} title: "${title}" description: "${description}" channelName: "${channelName}" channelPassword: "${channelPassword}" nsfw: "${nsfw}"`);
// check channel authorization
authenticateChannelCredentials(channelName, channelPassword)
.then(result => {
if (!result) {
authenticateOrSkip(skipAuth, channelName, channelPassword)
.then(authenticated => {
if (!authenticated) {
throw new Error('Authentication failed, you do not have access to that channel');
}
// make sure the claim name is available
@ -133,14 +148,14 @@ module.exports = (app) => {
res.status(200).json({
success: true,
message: {
name,
url : `spee.ch/${result.claim_id}/${name}`,
lbryTx: result,
},
});
})
.catch(error => {
logger.error('publish api error', error);
res.status(400).json({success: false, message: error.message});
errorHandlers.handleApiError('publish', originalUrl, ip, error, res);
});
});
@ -157,7 +172,7 @@ module.exports = (app) => {
});
});
// route to get a short channel id from long channel Id
app.get('/api/shortChannelId/:longId/:name', ({ params }, res) => {
app.get('/api/shortChannelId/:longId/:name', ({ ip, originalUrl, params }, res) => {
// serve content
db.Certificate.getShortChannelIdFromLongChannelId(params.longId, params.name)
.then(shortId => {
@ -166,7 +181,7 @@ module.exports = (app) => {
})
.catch(error => {
logger.error('api error getting short channel id', error);
res.status(400).json(error.message);
errorHandlers.handleApiError('short channel id', originalUrl, ip, error, res);
});
});
};

View file

@ -24,7 +24,7 @@ module.exports = (app) => {
app.get('/trending', (req, res) => {
res.status(301).redirect('/popular');
});
app.get('/popular', (req, res) => {
app.get('/popular', ({ ip, originalUrl }, res) => {
const startDate = new Date();
startDate.setDate(startDate.getDate() - 1);
const dateTime = startDate.toISOString().slice(0, 19).replace('T', ' ');
@ -36,20 +36,18 @@ module.exports = (app) => {
});
})
.catch(error => {
errorHandlers.handleRequestError(null, null, null, error, res);
errorHandlers.handleRequestError('popular', originalUrl, ip, error, res);
});
});
// route to display a list of the trending images
app.get('/new', (req, res) => {
app.get('/new', ({ ip, originalUrl }, res) => {
getRecentClaims()
.then(result => {
// logger.debug(result);
res.status(200).render('new', {
newClaims: result,
});
res.status(200).render('new', { newClaims: result });
})
.catch(error => {
errorHandlers.handleRequestError(null, null, null, error, res);
errorHandlers.handleRequestError('new', originalUrl, ip, error, res);
});
});
// route to send embedable video player (for twitter)

View file

@ -49,7 +49,6 @@ function extractPageFromClaims (claims, pageNumber) {
const claimStartIndex = (pageNumber - 1) * CLAIMS_PER_PAGE;
const claimEndIndex = claimStartIndex + 10;
const pageOfClaims = claims.slice(claimStartIndex, claimEndIndex);
logger.debug('page of claims:', pageOfClaims);
return pageOfClaims;
}

View file

@ -1,79 +0,0 @@
const logger = require('winston');
const { publish } = require('../controllers/publishController.js');
const { createPublishParams } = require('../helpers/publishHelpers.js');
const { useObjectPropertiesIfNoKeys } = require('../helpers/errorHandlers.js');
const { postToStats } = require('../controllers/statsController.js');
module.exports = (app, siofu, hostedContentPath) => {
const http = require('http');
const server = http.Server(app);
const io = require('socket.io')(server);
io.on('connection', socket => {
logger.silly('a user connected via sockets');
// attach upload listeners
const uploader = new siofu();
uploader.dir = hostedContentPath;
uploader.listen(socket);
// listener for when file upload starts
uploader.on('start', ({ file }) => {
// log
logger.info('client started an upload:', file.name);
// server side test to make sure file is not a bad file type
if (/\.exe$/.test(file.name)) {
uploader.abort(file.id, socket);
}
});
// listener for when file upload encounters an error
uploader.on('error', ({ error }) => {
logger.error('an error occured while uploading', error);
postToStats('PUBLISH', '/', null, null, null, error);
socket.emit('publish-status', error);
});
// listener for when file has been uploaded
uploader.on('saved', ({ file }) => {
if (file.success) {
logger.debug(`Client successfully uploaded ${file.name}`);
socket.emit('publish-update', 'File upload successfully completed. Your image is being published to LBRY (this might take a second)...');
// /*
// NOTE: need to validate that client has the credentials to the channel they chose
// otherwise they could circumvent security.
// */
let thumbnail;
if (file.meta.thumbnail) {
thumbnail = file.meta.thumbnail;
} else {
thumbnail = null;
}
let channelName;
if (file.meta.channel) {
channelName = file.meta.channel;
} else {
channelName = null;
}
// prepare the publish parameters
const publishParams = createPublishParams(file.pathName, file.meta.name, file.meta.title, file.meta.description, file.meta.license, file.meta.nsfw, thumbnail, channelName);
logger.debug('publish parameters:', publishParams);
// publish the file
publish(publishParams, file.name, file.meta.type)
.then(result => {
socket.emit('publish-complete', { name: publishParams.name, result });
})
.catch(error => {
logger.error('Publish Error:', useObjectPropertiesIfNoKeys(error));
socket.emit('publish-failure', error.message);
});
} else {
socket.emit('publish-failure', 'File uploaded, but with errors');
logger.error(`An error occurred in uploading the client's file`);
// to-do: remove the file, if not done automatically
}
});
// handle disconnect
socket.on('disconnect', () => {
logger.silly('a user disconnected via sockets');
});
});
return server;
};

View file

@ -6,7 +6,7 @@ const expressHandlebars = require('express-handlebars');
const Handlebars = require('handlebars');
const handlebarsHelpers = require('./helpers/handlebarsHelpers.js');
const { populateLocalsDotUser, serializeSpeechUser, deserializeSpeechUser } = require('./helpers/authHelpers.js');
const config = require('config');
const config = require('./config/speechConfig.js');
const logger = require('winston');
const { getDownloadDirectory } = require('./helpers/lbryApi');
const helmet = require('helmet');
@ -17,9 +17,9 @@ const passport = require('passport');
const cookieSession = require('cookie-session');
// configure logging
const logLevel = config.get('Logging.LogLevel');
const logLevel = config.logging.logLevel;
require('./config/loggerConfig.js')(logger, logLevel);
require('./config/slackLoggerConfig.js')(logger);
require('./config/slackConfig.js')(logger);
// check for global config variables
require('./helpers/configVarCheck.js')();
@ -42,7 +42,7 @@ app.use((req, res, next) => { // custom logging middleware to log all incoming
// initialize passport
app.use(cookieSession({
name : 'session',
keys : [config.get('Session.SessionKey')],
keys : [config.session.sessionKey],
maxAge: 24 * 60 * 60 * 1000, // 24 hours
}));
app.use(passport.initialize());
@ -82,7 +82,8 @@ db.sequelize
require('./routes/page-routes.js')(app);
require('./routes/serve-routes.js')(app);
require('./routes/home-routes.js')(app);
return require('./routes/sockets-routes.js')(app, siofu, hostedContentPath);
const http = require('http');
return http.Server(app);
})
.then(server => { // start the server
server.listen(PORT, () => {

View file

@ -1,4 +1,4 @@
<div class="row">
<div class="row row--padded">
<h3>404: Not Found</h3>
<p>That page does not exist. Return <a class="link--primary" href="/">home</a>.</p>
</div>

View file

@ -1,8 +1,8 @@
<div class="row row--tall flex-container--column">
<form>
<input class="input-file" type="file" id="siofu_input" name="siofu_input" accept="video/*,image/*" onchange="previewAndStageFile(event.target.files[0])" enctype="multipart/form-data"/>
<input class="input-file" type="file" id="siofu_input" name="siofu_input" accept="video/*,image/*" onchange="publishFileFunctions.previewAndStageFile(event.target.files[0])" enctype="multipart/form-data"/>
</form>
<div id="primary-dropzone" class="dropzone row row--margined row--padded row--tall flex-container--column flex-container--center-center" ondrop="drop_handler(event);" ondragover="dragover_handler(event);" ondragend="dragend_handler(event)" ondragenter="dragenter_handler(event)" ondragleave="dragexit_handler(event)" onclick="triggerFileChooser('siofu_input', event)">
<div id="primary-dropzone" class="dropzone row row--margined row--padded row--tall flex-container--column flex-container--center-center" ondrop="drop_handler(event);" ondragover="dragover_handler(event);" ondragend="dragend_handler(event)" ondragenter="dragenter_handler(event)" ondragleave="dragexit_handler(event)" onclick="publishFileFunctions.triggerFileChooser('siofu_input', event)">
<div id="primary-dropzone-instructions">
<p class="info-message-placeholder info-message--failure" id="input-error-file-selection" hidden="true"></p>
<p>Drag & drop image or video here to publish</p>
@ -22,7 +22,7 @@
<div class="column column--5 column--sml-10" >
<!-- preview -->
<div class="row row--padded">
<div id="asset-preview-holder" class="dropzone" ondrop="drop_handler(event);" ondragover="dragover_handler(event);" ondragend="dragend_handler(event)" ondragenter="preview_onmouseenter_handler()" ondragleave="preview_onmouseleave_handler()" onmouseenter="preview_onmouseenter_handler()" onmouseleave="preview_onmouseleave_handler()" onclick="triggerFileChooser('siofu_input', event)">
<div id="asset-preview-holder" class="dropzone" ondrop="drop_handler(event);" ondragover="dragover_handler(event);" ondragend="dragend_handler(event)" ondragenter="preview_onmouseenter_handler()" ondragleave="preview_onmouseleave_handler()" onmouseenter="preview_onmouseenter_handler()" onmouseleave="preview_onmouseleave_handler()" onclick="publishFileFunctions.triggerFileChooser('siofu_input', event)">
<div id="asset-preview-dropzone-instructions" class="hidden">
<p>Drag & drop image or video here</p>
<p class="fine-print">OR</p>
@ -50,105 +50,3 @@
</div>
</div>
</div>
<script src="/socket.io/socket.io.js"></script>
<script src="/siofu/client.js"></script>
<script typ="text/javascript">
checkCookie();
const socket = io();
const uploader = new SocketIOFileUpload(socket);
let stagedFiles = null;
const publishFormWrapper = document.getElementById('publish-form');
const publishStatus = document.getElementById('publish-status');
const publishUpdate = document.getElementById('publish-update');
const publishProgressBar = document.getElementById('publish-progress-bar');
const uploadPercent = document.getElementById('upload-percent');
/* socketio-file-upload listeners */
uploader.addEventListener('start', function(event){
console.log('starting upload');
addInputValuesToFileMetaData(event)
// hide the publish tool
hidePublishTools();
// show the progress status and animation
showPublishStatus();
showPublishProgressBar();
});
uploader.addEventListener('progress', function(event){
var percent = event.bytesLoaded / event.file.size * 100;
updatePublishStatus('<p>File is loading to server</p>')
updateUploadPercent(`<p class="blue">${percent.toFixed(2)}%</p>`)
});
/* socket.io message listeners */
socket.on('publish-update', function(msg){
updatePublishStatus(`<p>${msg}</p>`);
updateUploadPercent(`<p>Curious what magic is happening here? <a class="link--primary" target="blank" href="https://lbry.io/faq/what-is-lbry">Learn more.</a></p>`);
});
socket.on('publish-failure', function(msg){
updatePublishStatus('<p> --(✖╭╮✖)→ </p><p>' + JSON.stringify(msg) + '</p><strong>For help, post the above error text in the #speech channel on the <a class="link--primary" href="https://discord.gg/YjYbwhS" target="_blank">lbry discord</a></strong>');
hidePublishProgressBar();
hideUploadPercent();
});
socket.on('publish-complete', function(msg){
const showUrl = msg.result.claim_id + "/" + msg.name;
// update status
updatePublishStatus('<p>Your publish is complete! You are being redirected to it now.</p>');
updateUploadPercent('<p><a class="link--primary" target="_blank" href="\' + showUrl + \'">If you do not get redirected, click here.</a></p>')
// redirect the user
window.location.href = showUrl;
});
function hidePublishTools() {
publishFormWrapper.setAttribute('class', 'hidden');
}
// publish status functions
function showPublishStatus() {
publishStatus.setAttribute('class', 'row row--tall flex-container--column flex-container--center-center');
}
function updatePublishStatus(msg){
publishUpdate.innerHTML = msg;
}
// progress bar functions
function showPublishProgressBar(){
createProgressBar(publishProgressBar, 12);
}
function hidePublishProgressBar(){
publishProgressBar.hidden = true;
}
// upload percent functions
function updateUploadPercent(msg){
uploadPercent.innerHTML = msg;
}
function hideUploadPercent(){
uploadPercent.hidden = true;
}
function addInputValuesToFileMetaData(event) {
// get values from inputs
const name = document.getElementById('claim-name-input').value.trim();
const title = document.getElementById('publish-title').value.trim();
const description = document.getElementById('publish-description').value.trim();
const license = document.getElementById('publish-license').value.trim();
const nsfw = document.getElementById('publish-nsfw').checked;
const anonymous = document.getElementById('anonymous-select').checked;
const channel = document.getElementById('channel-name-select').value.trim();
const thumbnail = document.getElementById('claim-thumbnail-input').value.trim();
// set values on file meta data
event.file.meta.name = name;
event.file.meta.title = title;
event.file.meta.description = description;
event.file.meta.license = license;
event.file.meta.nsfw = nsfw;
event.file.meta.type = stagedFiles[0].type;
if (!anonymous) {
event.file.meta.channel = channel;
}
if (thumbnail && (thumbnail.trim !== '')){
event.file.meta.thumbnail = thumbnail;
}
}
</script>

View file

@ -21,7 +21,7 @@
<!-- google analytics -->
{{ googleAnalytics }}
</head>
<body>
<body id="channel-body">
<script src="/assets/js/generalFunctions.js"></script>
<script src="/assets/js/navBarFunctions.js"></script>
{{> navBar}}

View file

@ -1,6 +1,6 @@
<!DOCTYPE html>
<html>
<body>
<body id="embed-video-player">
<style type="text/css">
video {

View file

@ -21,7 +21,7 @@
<!-- google analytics -->
{{ googleAnalytics }}
</head>
<body>
<body id="main-body">
<script src="/assets/js/generalFunctions.js"></script>
<script src="/assets/js/validationFunctions.js"></script>
<script src="/assets/js/publishFileFunctions.js"></script>

View file

@ -18,7 +18,7 @@
<!-- google analytics -->
{{ googleAnalytics }}
</head>
<body>
<body id="show-body">
<script src="/assets/js/generalFunctions.js"></script>
<script src="/assets/js/navBarFunctions.js"></script>
{{> navBar}}

View file

@ -16,7 +16,7 @@
<!-- google analytics -->
{{ googleAnalytics }}
</head>
<body>
<body id="showlite-body">
{{{ body }}}
<script src="/assets/js/showFunctions.js"></script>
</body>

View file

@ -6,7 +6,7 @@
</div><div class="column column--6 column--sml-10">
<div class="input-text--primary flex-container--row flex-container--left-bottom">
<span>@</span>
<input type="text" name="new-channel-name" id="new-channel-name" class="input-text" placeholder="exampleChannelName" value="" oninput="checkChannelName(event.target.value)">
<input type="text" name="new-channel-name" id="new-channel-name" class="input-text" placeholder="exampleChannelName" value="" oninput="validationFunctions.checkChannelName(event.target.value)">
<span id="input-success-channel-name" class="info-message--success"></span>
</div>
</div>

View file

@ -3,11 +3,11 @@
<div class="column column--10">
<form>
<div class="column column--3 column--med-10">
<input type="radio" name="anonymous-or-channel" id="anonymous-select" class="input-radio" value="anonymous" {{#unless user}}checked {{/unless}} onchange="toggleChannel(event.target.value)"/>
<label class="label label--pointer" for="anonymous-select">Anonymous</label>
<input type="radio" name="anonymous-or-channel" id="anonymous-radio" class="input-radio" value="anonymous" {{#unless user}}checked {{/unless}} onchange="toggleChannel(event.target.value)"/>
<label class="label label--pointer" for="anonymous-radio">Anonymous</label>
</div><div class="column column--7 column--med-10">
<input type="radio" name="anonymous-or-channel" id="in-a-channel-select" class="input-radio" value="in a channel" {{#if user}}checked {{/if}} onchange="toggleChannel(event.target.value)"/>
<label class="label label--pointer" for="in-a-channel-select">In a channel</label>
<input type="radio" name="anonymous-or-channel" id="channel-radio" class="input-radio" value="in a channel" {{#if user}}checked {{/if}} onchange="toggleChannel(event.target.value)"/>
<label class="label label--pointer" for="channel-radio">In a channel</label>
</div>
</form>
</div>

View file

@ -1,9 +1,9 @@
<div class="row row--padded row--wide">
<div class="input-error" id="input-error-publish-submit" hidden="true"></div>
<button id="publish-submit" class="button--primary button--large" onclick="publishStagedFile(event)">Upload</button>
<button id="publish-submit" class="button--primary button--large" onclick="publishFileFunctions.publishStagedFile(event)">Upload</button>
</div>
<div class="row row--short align-content-center">
<button class="button--cancel" onclick="cancelPublish()">Cancel</button>
<button class="button--cancel" onclick="publishFileFunctions.cancelPublish()">Cancel</button>
</div>
<div class="row row--short align-content-center">
<p class="fine-print">By clicking 'Upload', you affirm that you have the rights to publish this content to the LBRY network, and that you understand the properties of publishing it to a decentralized, user-controlled network. <a class="link--primary" target="_blank" href="https://lbry.io/learn">Read more.</a></p>

View file

@ -9,7 +9,7 @@
<span id="url-channel" class="url-text--secondary" {{#if user}}{{else}}hidden="true"{{/if}}>{{user.channelName}}:{{user.shortChannelId}}</span>
<span id="url-no-channel-placeholder" class="url-text--secondary tooltip" {{#if user}}hidden="true"{{else}}{{/if}}>xyz<span class="tooltip-text">This will be a random id</span></span>
<span id="url-channel-placeholder" class="url-text--secondary tooltip" hidden="true">@channel<span class="tooltip-text">Select a channel above</span></span> /
<input type="text" id="claim-name-input" class="input-text" placeholder="your-url-here" oninput="checkClaimName(event.target.value)">
<input type="text" id="claim-name-input" class="input-text" placeholder="your-url-here" oninput="validationFunctions.checkClaimName(event.target.value)">
<span id="input-success-claim-name" class="info-message--success span--absolute"></span>
</div>