Download params

This commit is contained in:
Aditya Kulkarni 2020-05-04 11:28:35 -07:00
parent 23b9b048e0
commit 453396b00f
3 changed files with 102 additions and 4 deletions

View File

@ -2,9 +2,15 @@
/* eslint-disable max-classes-per-file */
import React, { Component } from 'react';
import { Redirect, withRouter } from 'react-router';
import { ipcRenderer } from 'electron';
import { remote, ipcRenderer } from 'electron';
import TextareaAutosize from 'react-textarea-autosize';
import Store from 'electron-store';
import request from 'request';
import progress from 'progress-stream';
import path from 'path';
import os from 'os';
import fs from 'fs';
import { promisify } from 'util';
import native from '../../native/index.node';
import routes from '../constants/routes.json';
import { RPCConfig, Info } from './AppState';
@ -13,6 +19,18 @@ import cstyles from './Common.module.css';
import styles from './LoadingScreen.module.css';
import Logo from '../assets/img/logobig.png';
const locateZcashParamsDir = () => {
if (os.platform() === 'darwin') {
return path.join(remote.app.getPath('appData'), 'ZcashParams');
}
if (os.platform() === 'linux') {
return path.join(remote.app.getPath('home'), '.zcash-params');
}
return path.join(remote.app.getPath('appData'), 'ZcashParams');
};
type Props = {
setRPCConfig: (rpcConfig: RPCConfig) => void,
rescanning: boolean,
@ -70,11 +88,89 @@ class LoadingScreen extends Component<Props, LoadingScreenState> {
if (rescanning) {
this.runSyncStatusPoller();
} else {
// Do it in a timeout, so the window has a chance to load.
setTimeout(() => this.doFirstTimeSetup(), 100);
(async () => {
const success = await this.ensureZcashParams();
if (success) {
// Do it in a timeout, so the window has a chance to load.
setTimeout(() => this.doFirstTimeSetup(), 100);
}
})();
}
}
download = (url, dest, name, cb) => {
const file = fs.createWriteStream(dest);
const sendReq = request.get(url);
// verify response code
sendReq.on('response', response => {
if (response.statusCode !== 200) {
return cb(`Response status was ${response.statusCode}`);
}
const totalSize = (parseInt(response.headers['content-length'], 10) / 1024 / 1024).toFixed(0);
const str = progress({ time: 1000 }, pgrs => {
this.setState({
currentStatus: `Downloading ${name}... (${(pgrs.transferred / 1024 / 1024).toFixed(0)} MB / ${totalSize} MB)`
});
});
sendReq.pipe(str).pipe(file);
});
// close() is async, call cb after close completes
file.on('finish', () => file.close(cb));
// check for request errors
sendReq.on('error', err => {
fs.unlink(dest);
return cb(err.message);
});
file.on('error', err => {
// Handle errors
fs.unlink(dest); // Delete the file async. (But we don't check the result)
return cb(err.message);
});
};
ensureZcashParams = async () => {
// Check if the zcash params dir exists and if the params files are present
const dir = locateZcashParamsDir();
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
// Check for the params
const params = [
{ name: 'sapling-output.params', url: 'https://z.cash/downloads/sapling-output.params' },
{ name: 'sapling-spend.params', url: 'https://z.cash/downloads/sapling-spend.params' }
];
// eslint-disable-next-line no-plusplus
for (let i = 0; i < params.length; i++) {
const p = params[i];
const fileName = path.join(dir, p.name);
if (!fs.existsSync(fileName)) {
// Download and save this file
this.setState({ currentStatus: `Downloading ${p.name}...` });
try {
// eslint-disable-next-line no-await-in-loop
await promisify(this.download)(p.url, fileName, p.name);
} catch (err) {
console.log(`error: ${err}`);
this.setState({ currentStatus: `Error downloading ${p.name}. The error was: ${err}` });
return false;
}
}
}
return true;
};
loadServerURI = () => {
// Try to read the default server
const store = new Store();

View File

@ -289,6 +289,7 @@
"electron-store": "^5.1.1",
"history": "^4.10.1",
"libsodium-wrappers-sumo": "^0.7.6",
"progress": "^2.0.3",
"qrcode.react": "^1.0.0",
"react": "^16.12.0",
"react-accessible-accordion": "^3.0.1",
@ -299,6 +300,7 @@
"react-router-dom": "^5.1.2",
"react-tabs": "^3.1.0",
"react-textarea-autosize": "^7.1.2",
"request": "^2.88.2",
"source-map-support": "^0.5.16",
"typeface-roboto": "^0.0.75",
"ws": "^7.2.1"

View File

@ -11157,7 +11157,7 @@ request-promise-native@^1.0.5:
stealthy-require "^1.1.1"
tough-cookie "^2.3.3"
request@^2.45.0, request@^2.83.0, request@^2.87.0, request@^2.88.0:
request@^2.45.0, request@^2.83.0, request@^2.87.0, request@^2.88.0, request@^2.88.2:
version "2.88.2"
resolved "https://registry.yarnpkg.com/request/-/request-2.88.2.tgz#d73c918731cb5a87da047e207234146f664d12b3"
integrity sha512-MsvtOrfG9ZcrOwAW+Qi+F6HbD0CWXEh9ou77uOb7FM2WPhwT7smM833PzanhJLsgXjN89Ir6V2PczXNnMpwKhw==