Skip to content
This repository has been archived by the owner on Jun 20, 2022. It is now read-only.

Commit

Permalink
fix: add decrypt script to backups
Browse files Browse the repository at this point in the history
  • Loading branch information
arielsvg committed Nov 18, 2020
1 parent 5ff4dad commit 1984a31
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 31 deletions.
6 changes: 3 additions & 3 deletions app/application.ts
Expand Up @@ -14,12 +14,12 @@ import {
} from './javascripts/main/keychain';
import { IpcMessages } from './javascripts/shared/ipcMessages';
import { isDev } from './javascripts/main/utils';
import { indexHtml } from './javascripts/main/paths';
import { indexUrl } from './javascripts/main/paths';
import { action, makeObservable, observable } from 'mobx';

export class AppState {
readonly store: Store;
readonly startUrl = indexHtml;
readonly startUrl = indexUrl;
readonly isPrimaryInstance: boolean;
public willQuitApp = false;
public lastBackupDate: number | null = null;
Expand All @@ -34,7 +34,7 @@ export class AppState {
});
}

setBackupCreationDate(date: number | null) {
setBackupCreationDate(date: number | null): void {
this.lastBackupDate = date;
}
}
Expand Down
65 changes: 43 additions & 22 deletions app/javascripts/main/backupsManager.ts
Expand Up @@ -4,11 +4,16 @@ import path from 'path';
import { AppMessageType, MessageType } from '../../../test/TestIpcMessage';
import { AppState } from '../../application';
import { IpcMessages } from '../shared/ipcMessages';
import { deleteDir, ensureDirectoryExists, moveFiles } from './fileUtils';
import { Store, StoreKeys } from './store';
import {
deleteDir,
ensureDirectoryExists,
FileDoesNotExist,
moveFiles,
} from './fileUtils';
import { decryptScriptPath } from './paths';
import { StoreKeys } from './store';
import { backups as str } from './strings';
import { handle, send } from './testing';
import { UpdateManager } from './updateManager';
import { isTesting, last } from './utils';

function log(...message: any) {
Expand Down Expand Up @@ -60,10 +65,23 @@ export function createBackupsManager(
let backupsDisabled = appState.store.get(StoreKeys.BackupsDisabled);
let needsBackup = false;

ensureDirectoryExists(backupsLocation)
.then(() =>
fs.copyFile(
decryptScriptPath,
path.join(backupsLocation, path.basename(decryptScriptPath))
)
)
.catch(console.error);

determineLastBackupDate(backupsLocation)
.then((date) => (appState.lastBackupDate = date))
.then((date) => appState.setBackupCreationDate(date))
.catch(console.error);

ipcMain.on(IpcMessages.DataArchive, (_event, data) => {
archiveData(data);
});

async function setBackupsLocation(location: string) {
const previousLocation = backupsLocation;
if (previousLocation === location) {
Expand All @@ -86,10 +104,6 @@ export function createBackupsManager(
appState.store.set(StoreKeys.BackupsLocation, backupsLocation);
}

ipcMain.on(IpcMessages.DataArchive, (_event, data) => {
archiveData(data);
});

async function archiveData(data: any) {
if (backupsDisabled) return;
let success: boolean;
Expand Down Expand Up @@ -193,20 +207,27 @@ export function createBackupsManager(
async function determineLastBackupDate(
backupsLocation: string
): Promise<number | null> {
const files = (await fs.readdir(backupsLocation))
.filter(
(filename) =>
filename.endsWith(BackupFileExtension) &&
!Number.isNaN(backupFileNameToDate(filename))
)
.sort();
const lastBackupFileName = last(files);
if (!lastBackupFileName) {
return null;
}
const backupDate = backupFileNameToDate(lastBackupFileName);
if (Number.isNaN(backupDate)) {
try {
const files = (await fs.readdir(backupsLocation))
.filter(
(filename) =>
filename.endsWith(BackupFileExtension) &&
!Number.isNaN(backupFileNameToDate(filename))
)
.sort();
const lastBackupFileName = last(files);
if (!lastBackupFileName) {
return null;
}
const backupDate = backupFileNameToDate(lastBackupFileName);
if (Number.isNaN(backupDate)) {
return null;
}
return backupDate;
} catch (error) {
if (error.code !== FileDoesNotExist) {
console.error(error);
}
return null;
}
return backupDate;
}
4 changes: 2 additions & 2 deletions app/javascripts/main/keychain.ts
Expand Up @@ -4,7 +4,7 @@ import { isLinux } from './platforms';
import { AppName } from './strings';
import { isDev, isTesting } from './utils';
import { IpcMessages } from '../shared/ipcMessages';
import { grantKeyringAccessJsPath, grantKeyringAccessHtml } from './paths';
import { grantKeyringAccessJsPath, grantKeyringAccessUrl } from './paths';
import { Store, StoreKeys } from './store';

const ServiceName = isTesting()
Expand Down Expand Up @@ -41,7 +41,7 @@ export async function ensureKeychainAccess(
},
});
window.on('ready-to-show', window.show);
window.loadURL(grantKeyringAccessHtml);
window.loadURL(grantKeyringAccessUrl);

const quitListener = () => {
app.quit();
Expand Down
19 changes: 16 additions & 3 deletions app/javascripts/main/paths.ts
@@ -1,9 +1,11 @@
import path from 'path';
import index from '../../index.html';
import grantKeyringAccess from '../../grantKeyringAccess.html';
import decryptScript from 'decrypt/dist/decrypt.html';

export const indexHtml = htmlPath(index);
export const grantKeyringAccessHtml = htmlPath(grantKeyringAccess);
export const indexUrl = url(index);
export const grantKeyringAccessUrl = url(grantKeyringAccess);
export const decryptScriptPath = filePath(decryptScript);
export const preloadJsPath = path.join(
__dirname,
'javascripts/renderer/preload.js'
Expand All @@ -13,7 +15,7 @@ export const grantKeyringAccessJsPath = path.join(
'javascripts/renderer/grantKeyringAccess.js'
);

function htmlPath(fileName: string): string {
function url(fileName: string): string {
if ('APP_RELATIVE_PATH' in process.env) {
return path.join(
'file://',
Expand All @@ -24,3 +26,14 @@ function htmlPath(fileName: string): string {
}
return path.join('file://', __dirname, fileName);
}

function filePath(fileName: string): string {
if ('APP_RELATIVE_PATH' in process.env) {
return path.join(
__dirname,
process.env.APP_RELATIVE_PATH as string,
fileName
);
}
return path.join(__dirname, fileName);
}
1 change: 0 additions & 1 deletion app/javascripts/main/updateManager.ts
Expand Up @@ -4,7 +4,6 @@ import electronLog from 'electron-log';
import { autoUpdater } from 'electron-updater';
import { MessageType } from '../../../test/TestIpcMessage';
import { AppState } from '../../application';
import { BackupsManager } from './backupsManager';
import { Store, StoreKeys } from './store';
import { updates as str } from './strings';
import { handle } from './testing';
Expand Down
32 changes: 32 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -9,6 +9,7 @@
"license": "AGPL-3.0-or-later",
"dependencies": {
"compare-versions": "^3.6.0",
"decrypt": "github:standardnotes/decrypt#master",
"electron-log": "^4.2.2",
"electron-updater": "^4.3.4",
"mime-types": "^2.1.27",
Expand Down

0 comments on commit 1984a31

Please sign in to comment.