mirror of
https://github.com/ascribe/onion.git
synced 2024-12-22 09:23:13 +01:00
Merge pull request #174 from ascribe/fix-digital-work-file-name
Use setTimeout instead of setInterval to refresh signed download urls
This commit is contained in:
commit
55f53247ea
@ -30,15 +30,12 @@ const S3DownloadButton = React.createClass({
|
|||||||
},
|
},
|
||||||
|
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
/**
|
// Initially, we request a signed url from the backend
|
||||||
* Initially, we request a signed url from
|
|
||||||
* the backend
|
|
||||||
*/
|
|
||||||
this.signUrl();
|
this.signUrl();
|
||||||
},
|
},
|
||||||
|
|
||||||
componentWillUnmount() {
|
componentWillUnmount() {
|
||||||
window.clearInterval(this.state.signatureExpiryTimerId);
|
window.clearTimeout(this.state.signatureExpiryTimerId);
|
||||||
},
|
},
|
||||||
|
|
||||||
transformS3UrlToS3Key(url) {
|
transformS3UrlToS3Key(url) {
|
||||||
@ -52,29 +49,28 @@ const S3DownloadButton = React.createClass({
|
|||||||
.signUrl(this.transformS3UrlToS3Key(url), title, artistName)
|
.signUrl(this.transformS3UrlToS3Key(url), title, artistName)
|
||||||
.then(({ signed_url: downloadUrl }) => {
|
.then(({ signed_url: downloadUrl }) => {
|
||||||
const { signatureExpiryTimerId } = this.state;
|
const { signatureExpiryTimerId } = this.state;
|
||||||
let newState = { downloadUrl };
|
|
||||||
|
|
||||||
if(!signatureExpiryTimerId) {
|
// The signed url, however can expire, which is why we need to set up a timer to
|
||||||
/**
|
// renew it when it expires.
|
||||||
* The signed url, however can expire, which is why
|
const expires = parseInt(queryParamsToArgs(downloadUrl.split('?')[1]).expires, 10);
|
||||||
* we need to renew it when it expires.
|
const now = new Date().getTime() / 1000;
|
||||||
*/
|
|
||||||
const expires = parseInt(queryParamsToArgs(downloadUrl.split('?')[1]).expires, 10);
|
|
||||||
const now = new Date().getTime() / 1000;
|
|
||||||
|
|
||||||
/**
|
// Amazon uses seconds as their signature unix timestamp while `setTimeout` uses
|
||||||
* Amazon uses seconds as their signature unix timestamp
|
// milliseconds. Therefore we need to multiply with 1000.
|
||||||
* while `setInterval` uses milliseconds. Therefore we need to
|
const interval = (expires - now) * 1000;
|
||||||
* multiply with 1000.
|
|
||||||
*/
|
|
||||||
const interval = (expires - now) * 1000;
|
|
||||||
|
|
||||||
Object.assign(newState, {
|
// Make sure to clear the previous timeout just in case it was not the one that
|
||||||
// Substract 5s to make sure there is a big enough window to sign again before expiration
|
// invoked the refetching
|
||||||
signatureExpiryTimerId: window.setInterval(this.signUrl, interval - 5000)
|
if (signatureExpiryTimerId) {
|
||||||
});
|
window.clearTimeout(signatureExpiryTimerId);
|
||||||
}
|
}
|
||||||
this.setState(newState);
|
|
||||||
|
this.setState({
|
||||||
|
downloadUrl,
|
||||||
|
// Substract 5s to make sure there is a big enough window to sign again before
|
||||||
|
// expiration
|
||||||
|
signatureExpiryTimerId: window.setTimeout(this.signUrl, interval - 5000)
|
||||||
|
});
|
||||||
})
|
})
|
||||||
.catch(console.logGlobal);
|
.catch(console.logGlobal);
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user