2017-12-28 20:51:03 +01:00
|
|
|
import React from 'react';
|
2018-01-17 18:46:16 +01:00
|
|
|
import {connect} from 'react-redux';
|
|
|
|
import {selectFile, clearFile, updateLoggedInChannel, updatePublishStatus, updateError} from '../actions';
|
|
|
|
import {getCookie} from '../utils/cookies.js';
|
2018-01-17 18:24:17 +01:00
|
|
|
import Dropzone from './Dropzone.jsx';
|
2018-01-08 18:39:59 +01:00
|
|
|
import PublishTitleInput from './PublishTitleInput.jsx';
|
|
|
|
import PublishUrlInput from './PublishUrlInput.jsx';
|
2018-01-06 03:26:57 +01:00
|
|
|
import PublishThumbnailInput from './PublishThumbnailInput.jsx';
|
|
|
|
import PublishMetadataInputs from './PublishMetadataInputs.jsx';
|
2018-01-17 18:46:16 +01:00
|
|
|
import ChannelSelect from './ChannelSelect.jsx';
|
2018-01-17 00:55:29 +01:00
|
|
|
import * as publishStates from '../constants/publishing_states';
|
2018-01-12 00:37:32 +01:00
|
|
|
|
2018-01-05 01:10:25 +01:00
|
|
|
class PublishForm extends React.Component {
|
|
|
|
constructor (props) {
|
|
|
|
super(props);
|
2018-01-11 21:51:38 +01:00
|
|
|
this.validatePublishRequest = this.validatePublishRequest.bind(this);
|
|
|
|
this.makePublishRequest = this.makePublishRequest.bind(this);
|
2018-01-04 23:00:02 +01:00
|
|
|
this.publish = this.publish.bind(this);
|
2018-01-03 02:12:57 +01:00
|
|
|
}
|
2018-01-10 03:25:38 +01:00
|
|
|
componentWillMount () {
|
2018-01-10 20:26:01 +01:00
|
|
|
// check for whether a channel is already logged in
|
2018-01-10 03:25:38 +01:00
|
|
|
const loggedInChannelName = getCookie('channel_name');
|
|
|
|
const loggedInChannelShortId = getCookie('short_channel_id');
|
|
|
|
const loggedInChannelLongId = getCookie('long_channel_id');
|
2018-01-10 20:26:01 +01:00
|
|
|
console.log(`channel cookies found: ${loggedInChannelName} ${loggedInChannelShortId} ${loggedInChannelLongId}`);
|
|
|
|
this.props.onChannelLogin(loggedInChannelName, loggedInChannelShortId, loggedInChannelLongId);
|
2018-01-10 03:25:38 +01:00
|
|
|
}
|
2018-01-11 21:51:38 +01:00
|
|
|
validatePublishRequest () {
|
|
|
|
// make sure all required data is provided
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
// is there a file?
|
|
|
|
if (!this.props.file) {
|
|
|
|
return reject(new Error('Please choose a file'));
|
|
|
|
}
|
|
|
|
// is there a claim chosen?
|
|
|
|
if (!this.props.claim) {
|
2018-01-12 19:21:40 +01:00
|
|
|
return reject(new Error('Please enter a URL'));
|
2018-01-11 21:51:38 +01:00
|
|
|
}
|
2018-01-12 23:27:34 +01:00
|
|
|
if (this.props.urlError) {
|
|
|
|
return reject(new Error('Fix the url'));
|
|
|
|
}
|
2018-01-11 21:51:38 +01:00
|
|
|
// if publishInChannel is true, is a channel logged in (or selected)
|
|
|
|
if (this.props.publishInChannel && !this.props.loggedInChannel.name) {
|
2018-01-12 19:21:40 +01:00
|
|
|
return reject(new Error('Select "Anonymous" or log in to a channel'));
|
2018-01-11 21:51:38 +01:00
|
|
|
}
|
2018-01-12 23:27:34 +01:00
|
|
|
// is the claim available?
|
2018-01-11 21:51:38 +01:00
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
makePublishRequest (file, metadata) {
|
|
|
|
const uri = '/api/claim-publish';
|
|
|
|
const xhr = new XMLHttpRequest();
|
|
|
|
const fd = this.appendDataToFormData(file, metadata);
|
|
|
|
const that = this;
|
|
|
|
xhr.upload.addEventListener('loadstart', function () {
|
2018-01-17 00:55:29 +01:00
|
|
|
that.props.onPublishStatusChange(publishStates.LOAD_START, 'upload started');
|
2018-01-11 21:51:38 +01:00
|
|
|
});
|
|
|
|
xhr.upload.addEventListener('progress', function (e) {
|
|
|
|
if (e.lengthComputable) {
|
|
|
|
const percentage = Math.round((e.loaded * 100) / e.total);
|
|
|
|
console.log('progress:', percentage);
|
2018-01-17 00:55:29 +01:00
|
|
|
that.props.onPublishStatusChange(publishStates.LOADING, `${percentage}%`);
|
2018-01-11 21:51:38 +01:00
|
|
|
}
|
|
|
|
}, false);
|
|
|
|
xhr.upload.addEventListener('load', function () {
|
|
|
|
console.log('loaded 100%');
|
2018-01-17 00:55:29 +01:00
|
|
|
that.props.onPublishStatusChange(publishStates.PUBLISHING, null);
|
2018-01-11 21:51:38 +01:00
|
|
|
}, false);
|
|
|
|
xhr.open('POST', uri, true);
|
|
|
|
xhr.onreadystatechange = function () {
|
|
|
|
if (xhr.readyState === 4) {
|
|
|
|
console.log('publish response:', xhr.response);
|
|
|
|
if (xhr.status === 200) {
|
|
|
|
console.log('publish complete!');
|
2018-01-12 19:21:40 +01:00
|
|
|
const url = JSON.parse(xhr.response).message.url;
|
2018-01-17 00:55:29 +01:00
|
|
|
that.props.onPublishStatusChange(publishStates.SUCCESS, url);
|
2018-01-12 19:21:40 +01:00
|
|
|
window.location = url;
|
2018-01-11 21:51:38 +01:00
|
|
|
} else if (xhr.status === 502) {
|
2018-01-17 00:55:29 +01:00
|
|
|
that.props.onPublishStatusChange(publishStates.FAILED, 'Spee.ch was not able to get a response from the LBRY network.');
|
2018-01-11 21:51:38 +01:00
|
|
|
} else {
|
2018-01-17 00:55:29 +01:00
|
|
|
that.props.onPublishStatusChange(publishStates.FAILED, JSON.parse(xhr.response).message);
|
2018-01-11 21:51:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// Initiate a multipart/form-data upload
|
|
|
|
xhr.send(fd);
|
|
|
|
}
|
|
|
|
createMetadata () {
|
|
|
|
let metadata = {
|
|
|
|
name : this.props.claim,
|
|
|
|
title : this.props.title,
|
|
|
|
description: this.props.description,
|
|
|
|
license : this.props.license,
|
|
|
|
nsfw : this.props.nsfw,
|
|
|
|
type : this.props.file.type,
|
|
|
|
thumbnail : this.props.thumbnail,
|
|
|
|
};
|
|
|
|
if (this.props.publishInChannel) {
|
|
|
|
metadata['channelName'] = this.props.loggedInChannel.name;
|
|
|
|
}
|
|
|
|
return metadata;
|
|
|
|
}
|
|
|
|
appendDataToFormData (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;
|
|
|
|
}
|
2018-01-03 02:12:57 +01:00
|
|
|
publish () {
|
|
|
|
// publish the asset
|
2018-01-11 21:51:38 +01:00
|
|
|
const that = this;
|
|
|
|
this.validatePublishRequest()
|
|
|
|
.then(() => {
|
|
|
|
const metadata = that.createMetadata();
|
|
|
|
// publish the claim
|
2018-01-15 18:59:23 +01:00
|
|
|
return that.makePublishRequest(that.props.file, metadata);
|
2018-01-11 21:51:38 +01:00
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
that.props.onPublishStatusChange('publish request made');
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
2018-01-15 18:59:23 +01:00
|
|
|
that.props.onPublishSubmitError(error.message);
|
2018-01-11 21:51:38 +01:00
|
|
|
});
|
2018-01-03 02:12:57 +01:00
|
|
|
}
|
2017-12-28 20:51:03 +01:00
|
|
|
render () {
|
|
|
|
return (
|
2018-01-04 20:05:16 +01:00
|
|
|
<div className="row row--no-bottom">
|
2018-01-03 02:12:57 +01:00
|
|
|
<div className="column column--10">
|
2018-01-09 02:46:17 +01:00
|
|
|
<PublishTitleInput />
|
2018-01-03 02:12:57 +01:00
|
|
|
</div>
|
|
|
|
<div className="column column--5 column--sml-10" >
|
2018-01-11 02:41:17 +01:00
|
|
|
<div className="row row--padded">
|
2018-01-17 18:24:17 +01:00
|
|
|
<Dropzone />
|
2017-12-28 20:51:03 +01:00
|
|
|
</div>
|
2018-01-03 02:12:57 +01:00
|
|
|
</div>
|
|
|
|
<div className="column column--5 column--sml-10 align-content-top">
|
|
|
|
<div id="publish-active-area" className="row row--padded">
|
2018-01-10 20:26:01 +01:00
|
|
|
<div className="row row--padded row--no-top row--wide">
|
|
|
|
<PublishUrlInput />
|
|
|
|
</div>
|
|
|
|
<div className="row row--padded row--no-top row--wide">
|
2018-01-17 18:46:16 +01:00
|
|
|
<ChannelSelect />
|
2018-01-10 20:26:01 +01:00
|
|
|
</div>
|
2018-01-11 21:51:38 +01:00
|
|
|
{ (this.props.file.type === 'video/mp4') && (
|
2018-01-17 18:46:16 +01:00
|
|
|
<div className="row row--padded row--no-top row--wide ">
|
2018-01-11 21:51:38 +01:00
|
|
|
<PublishThumbnailInput />
|
|
|
|
</div>
|
|
|
|
)}
|
2018-01-10 22:10:08 +01:00
|
|
|
<div className="row row--padded row--no-top row--no-bottom row--wide">
|
|
|
|
<PublishMetadataInputs />
|
|
|
|
</div>
|
2018-01-17 18:46:16 +01:00
|
|
|
{ this.props.publishSubmitError && (
|
|
|
|
<div className="row align-content-center">
|
|
|
|
<p className="info-message-placeholder info-message--failure">{this.props.publishSubmitError}</p>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
<div className="row row--wide align-content-center">
|
2018-01-06 01:47:55 +01:00
|
|
|
<button id="publish-submit" className="button--primary button--large" onClick={this.publish}>Publish</button>
|
2017-12-28 20:51:03 +01:00
|
|
|
</div>
|
2018-01-17 18:46:16 +01:00
|
|
|
<div className="row row--padded row--no-bottom align-content-center">
|
2018-01-09 02:06:31 +01:00
|
|
|
<button className="button--cancel" onClick={this.props.onFileClear}>Cancel</button>
|
2018-01-03 02:12:57 +01:00
|
|
|
</div>
|
|
|
|
<div className="row row--short align-content-center">
|
|
|
|
<p className="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 className="link--primary" target="_blank" href="https://lbry.io/learn">Read more.</a></p>
|
2017-12-28 20:51:03 +01:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-01-09 02:06:31 +01:00
|
|
|
const mapStateToProps = state => {
|
|
|
|
return {
|
2018-01-15 18:59:23 +01:00
|
|
|
file : state.file,
|
|
|
|
claim : state.claim,
|
|
|
|
title : state.metadata.title,
|
|
|
|
thumbnail : state.metadata.thumbnail,
|
|
|
|
description : state.metadata.description,
|
|
|
|
license : state.metadata.license,
|
|
|
|
nsfw : state.metadata.nsfw,
|
|
|
|
loggedInChannel : state.loggedInChannel,
|
|
|
|
publishInChannel : state.publishInChannel,
|
|
|
|
fileError : state.error.file,
|
|
|
|
urlError : state.error.url,
|
|
|
|
publishSubmitError: state.error.publishSubmit,
|
2018-01-09 02:06:31 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const mapDispatchToProps = dispatch => {
|
|
|
|
return {
|
|
|
|
onFileSelect: (file) => {
|
|
|
|
dispatch(selectFile(file));
|
|
|
|
},
|
|
|
|
onFileClear: () => {
|
|
|
|
dispatch(clearFile());
|
|
|
|
},
|
2018-01-10 20:26:01 +01:00
|
|
|
onChannelLogin: (name, shortId, longId) => {
|
2018-01-10 03:25:38 +01:00
|
|
|
dispatch(updateLoggedInChannel(name, shortId, longId));
|
|
|
|
},
|
2018-01-12 00:37:32 +01:00
|
|
|
onPublishStatusChange: (status, message) => {
|
|
|
|
dispatch(updatePublishStatus(status, message));
|
2018-01-11 21:51:38 +01:00
|
|
|
},
|
2018-01-15 18:59:23 +01:00
|
|
|
onPublishSubmitError: (value) => {
|
|
|
|
dispatch(updateError('publishSubmit', value));
|
2018-01-12 23:27:34 +01:00
|
|
|
},
|
2018-01-09 02:06:31 +01:00
|
|
|
};
|
2018-01-10 03:25:38 +01:00
|
|
|
};
|
2018-01-09 02:06:31 +01:00
|
|
|
|
|
|
|
export default connect(mapStateToProps, mapDispatchToProps)(PublishForm);
|