2018-01-30 18:00:02 +01:00
|
|
|
import React from 'react';
|
2018-02-05 01:40:28 +01:00
|
|
|
import PropTypes from 'prop-types';
|
2018-02-01 06:08:28 +01:00
|
|
|
import ProgressBar from 'components/ProgressBar';
|
|
|
|
import Request from 'utils/request';
|
2018-02-01 23:29:33 +01:00
|
|
|
import { LOCAL_CHECK, SEARCHING, UNAVAILABLE, AVAILABLE } from 'constants/asset_display_states';
|
2018-02-01 06:08:28 +01:00
|
|
|
|
|
|
|
class AssetDisplay extends React.Component {
|
|
|
|
constructor (props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
2018-02-01 23:29:33 +01:00
|
|
|
error : null,
|
|
|
|
status: LOCAL_CHECK,
|
2018-02-01 06:08:28 +01:00
|
|
|
};
|
2018-02-01 23:29:33 +01:00
|
|
|
this.isLocalFileAvailableOnServer = this.isLocalFileAvailableOnServer.bind(this);
|
|
|
|
this.triggerGetAssetOnServer = this.triggerGetAssetOnServer.bind(this);
|
2018-02-01 06:08:28 +01:00
|
|
|
}
|
|
|
|
componentDidMount () {
|
|
|
|
const that = this;
|
2018-02-01 23:29:33 +01:00
|
|
|
this.isLocalFileAvailableOnServer()
|
2018-02-01 06:08:28 +01:00
|
|
|
.then(isAvailable => {
|
|
|
|
if (!isAvailable) {
|
|
|
|
console.log('file is not yet available');
|
|
|
|
that.setState({status: SEARCHING});
|
2018-02-01 23:29:33 +01:00
|
|
|
return that.triggerGetAssetOnServer();
|
2018-02-01 06:08:28 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
that.setState({status: AVAILABLE});
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
that.setState({
|
|
|
|
status: UNAVAILABLE,
|
|
|
|
error : error.message,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2018-02-01 23:29:33 +01:00
|
|
|
isLocalFileAvailableOnServer () {
|
2018-02-01 06:08:28 +01:00
|
|
|
console.log(`checking if file is available for ${this.props.name}#${this.props.claimId}`);
|
|
|
|
const url = `/api/file-is-available/${this.props.name}/${this.props.claimId}`;
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
Request(url)
|
2018-02-02 21:46:18 +01:00
|
|
|
.then(({success, message, data: isAvailable}) => {
|
|
|
|
if (success) {
|
|
|
|
console.log('/api/file-is-available response:', isAvailable);
|
|
|
|
return resolve(isAvailable);
|
|
|
|
}
|
|
|
|
reject(new Error(message));
|
2018-02-01 06:08:28 +01:00
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2018-02-01 23:29:33 +01:00
|
|
|
triggerGetAssetOnServer () {
|
|
|
|
console.log(`getting claim for ${this.props.name}#${this.props.claimId}`);
|
2018-02-01 06:08:28 +01:00
|
|
|
const url = `/api/claim-get/${this.props.name}/${this.props.claimId}`;
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
Request(url)
|
2018-02-02 21:46:18 +01:00
|
|
|
.then(({success, message}) => {
|
|
|
|
console.log('/api/claim-get response:', success, message);
|
|
|
|
if (success) {
|
|
|
|
return resolve(true);
|
|
|
|
}
|
|
|
|
reject(new Error(message));
|
2018-02-01 06:08:28 +01:00
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
render () {
|
|
|
|
return (
|
|
|
|
<div id="asset-display-component">
|
2018-02-02 21:46:18 +01:00
|
|
|
{(this.state.status === LOCAL_CHECK) &&
|
|
|
|
<div>
|
|
|
|
<p>Checking to see if Spee.ch has your asset locally...</p>
|
|
|
|
</div>
|
|
|
|
}
|
2018-02-01 06:08:28 +01:00
|
|
|
{(this.state.status === SEARCHING) &&
|
|
|
|
<div>
|
|
|
|
<p>Sit tight, we're searching the LBRY blockchain for your asset!</p>
|
|
|
|
<ProgressBar size={12}/>
|
|
|
|
<p>Curious what magic is happening here? <a className="link--primary" target="blank" href="https://lbry.io/faq/what-is-lbry">Learn more.</a></p>
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
{(this.state.status === UNAVAILABLE) &&
|
|
|
|
<div>
|
|
|
|
<p>Unfortunately, we couldn't download your asset from LBRY. You can help us out by sharing the below error message in the <a className="link--primary" href="https://discord.gg/YjYbwhS" target="_blank">LBRY discord</a>.</p>
|
|
|
|
<i><p id="error-message">{this.state.error}</p></i>
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
{(this.state.status === AVAILABLE) &&
|
|
|
|
(() => {
|
2018-02-01 23:29:33 +01:00
|
|
|
switch (this.props.contentType) {
|
2018-02-01 06:08:28 +01:00
|
|
|
case 'image/jpeg':
|
|
|
|
case 'image/jpg':
|
|
|
|
case 'image/png':
|
|
|
|
return (
|
2018-02-01 23:29:33 +01:00
|
|
|
<img className="asset" src={this.props.src} alt={this.props.name}/>
|
2018-02-01 06:08:28 +01:00
|
|
|
);
|
|
|
|
case 'image/gif':
|
|
|
|
return (
|
2018-02-01 23:29:33 +01:00
|
|
|
<img className="asset" src={this.props.src} alt={this.props.name}/>
|
2018-02-01 06:08:28 +01:00
|
|
|
);
|
|
|
|
case 'video/mp4':
|
|
|
|
return (
|
2018-02-01 23:29:33 +01:00
|
|
|
<video id="video" className="asset" controls poster={this.props.thumbnail}>
|
|
|
|
<source src={this.props.src}/>
|
2018-02-01 06:08:28 +01:00
|
|
|
<p>Your browser does not support the <code>video</code> element.</p>
|
|
|
|
</video>
|
|
|
|
);
|
|
|
|
default:
|
|
|
|
return (
|
2018-02-01 23:29:33 +01:00
|
|
|
<p>Unsupported file type</p>
|
2018-02-01 06:08:28 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
})()
|
|
|
|
}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2018-01-30 18:00:02 +01:00
|
|
|
};
|
|
|
|
|
2018-02-01 23:29:33 +01:00
|
|
|
AssetDisplay.propTypes = {
|
|
|
|
name : PropTypes.string.isRequired,
|
|
|
|
claimId : PropTypes.string.isRequired,
|
|
|
|
src : PropTypes.string.isRequired,
|
|
|
|
contentType: PropTypes.string.isRequired,
|
|
|
|
fileExt : PropTypes.string.isRequired,
|
2018-02-03 01:06:21 +01:00
|
|
|
thumbnail : PropTypes.string,
|
2018-02-01 23:29:33 +01:00
|
|
|
};
|
2018-02-01 06:08:28 +01:00
|
|
|
|
2018-01-30 18:00:02 +01:00
|
|
|
export default AssetDisplay;
|