Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #336 Disable search during database download #346

Merged
merged 5 commits into from
Oct 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions www/js/desktop_scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ const progress = require('request-progress');
const { remote } = electron;
const ipc = electron.ipcRenderer;
const userDataPath = remote.app.getPath('userData');
const dbPath = path.resolve(userDataPath, 'sttmdesktop.realm');
const dbName = 'sttmdesktop.realm';
const dbCompressedName = `${dbName}.zip`;
const dbPath = path.resolve(userDataPath, dbName);
const newDBFolder = path.resolve(userDataPath, 'new-db');
const newDBPath = path.resolve(newDBFolder, dbName);

const { store } = remote.require('./app');

Expand Down Expand Up @@ -82,24 +86,29 @@ module.exports = {
if (!error && response.statusCode === 200) {
const curDBHash = store.get('curDBHash');
if (force || curDBHash !== newestDBHash) {
const dbCompressed = path.resolve(userDataPath, 'sttmdesktop.realm.zip');
const dbCompressed = path.resolve(userDataPath, dbCompressedName);
global.core.search.$search.placeholder = 'Downloading database...';
progress(request('https://banidb.com/databases/sttmdesktop.realm.zip'))
.on('progress', (state) => {
const win = remote.getCurrentWindow();
win.setProgressBar(state.percent);
global.core.search.updateDLProgress(state);
})
.on('end', () => {
extract(dbCompressed, { dir: userDataPath }, (err0) => {
extract(dbCompressed, { dir: newDBFolder }, (err0) => {
if (err0) {
// ToDo: Log errors
// eslint-disable-next-line no-console
console.log(err0);
}
fs.chmodSync(dbPath, '755');
fs.chmodSync(newDBPath, '755');
module.exports.initDB();
// Save the hash for comparison next time
store.set('curDBHash', newestDBHash);
// Delete compressed database
fs.unlinkSync(dbCompressed);

// Replace current DB file with new version
fs.renameSync(newDBPath, dbPath);
// Delete pre-realm DB
const oldDBs = ['data.db', 'sttmdesktop.db'];
oldDBs.forEach((oldDB) => {
Expand Down
2 changes: 2 additions & 0 deletions www/js/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const searchInputs = h('div#search-container', [
h('span', 'Ang'),
h('input#ang-input.gurmukhi', {
type: 'number',
disabled: 'disabled',
placeholder: '123',
min: 1,
max: 1430,
Expand Down Expand Up @@ -315,6 +316,7 @@ module.exports = {
initSearch() {
this.$dbDownloadProgress.style.height = 0;
this.$search.disabled = false;
this.$angSearch.disabled = false;
this.$search.focus();
this.changeSearchType(this.searchType);
this.changeSearchSource(this.searchSource);
Expand Down