A fully open source & end-to-end encrypted note taking alternative to Evernote.
TypeScript asp-net-coredotnet-coreelectronfosshacktoberfestnote-managmentnote-takingnote-taking-appnotebooknotesnotes-appopen-sourceopensourceprivacyproductivityreactreact-nativereactjsself-hosted
feat(desktop): impl automatic backups
thecodrrb3381bc1f273
diff --git a/apps/web/src/commands/save-file.js b/apps/web/src/commands/save-file.jsnew file mode 100644index 000000000..b8d2a0743--- /dev/null+++ b/apps/web/src/commands/save-file.js@@ -0,0 +1,5 @@+import { invokeCommand } from "./index";++export default function saveFile(filePath, data) {+ invokeCommand("saveFile", { filePath, data });+}diff --git a/apps/web/src/common/index.js b/apps/web/src/common/index.jsindex a3df6e9e3..cff18ce93 100644--- a/apps/web/src/common/index.js+++ b/apps/web/src/common/index.js@@ -11,6 +11,7 @@ import Config from "../utils/config";import { store as userstore } from "../stores/user-store";import { hashNavigate, getCurrentHash } from "../navigation";import { db } from "./db";+import { sanitizeFilename } from "../utils/filename";export const COLORS = {red: "#f44336",@@ -93,7 +94,7 @@ export function notesFromContext(context) {return notes;}-export async function createBackup() {+export async function createBackup(save = true) {const encryptBackups = Config.get("encryptBackups", false);const data = await showLoadingDialog({title: "Creating backup",@@ -107,12 +108,14 @@ export async function createBackup() {</Text>),});- download(- `notesnook-backup-${new Date().toLocaleString("en")}`,- data,- "nnbackup"+ const filename = sanitizeFilename(+ `notesnook-backup-${new Date().toLocaleString("en")}`);- await showToast("success", "Backup created!");+ const ext = "nnbackup";+ showToast("success", "Backup created!");++ if (!save) return { data, filename, ext };+ else download(filename, data, ext);}export function isUserPremium() {diff --git a/apps/web/src/common/reminders.js b/apps/web/src/common/reminders.jsindex 28ddd0f99..b0b473de1 100644--- a/apps/web/src/common/reminders.js+++ b/apps/web/src/common/reminders.js@@ -6,6 +6,9 @@ import * as Icon from "../components/icons";import dayjs from "dayjs";import { showRecoveryKeyDialog } from "../common/dialog-controller";import { hashNavigate } from "../navigation";+import { isDesktop } from "../utils/platform";+import saveFile from "../commands/save-file";+import { PATHS } from "@notesnook/desktop/paths";export async function shouldAddBackupReminder() {const backupReminderOffset = Config.get("backupReminderOffset", 0);@@ -60,7 +63,12 @@ export const Reminders = {export async function resetReminders() {appStore.set((state) => (state.reminders = []));if (await shouldAddBackupReminder()) {- appStore.addReminder("backup", "high");+ if (isDesktop()) {+ const { data, filename, ext } = await createBackup(false);+ saveFile(`${PATHS.backupsDirectory}/${filename}.${ext}`, data);+ } else {+ appStore.addReminder("backup", "high");+ }}if (await shouldAddSignupReminder()) {appStore.addReminder("signup", "low");diff --git a/apps/web/src/index.js b/apps/web/src/index.jsindex 47a858459..9a8b2503f 100644--- a/apps/web/src/index.js+++ b/apps/web/src/index.js@@ -3,6 +3,7 @@ import React from "react";import { initializeDatabase } from "./common/db";import "./index.css";import * as serviceWorker from "./serviceWorkerRegistration";+import "./utils/overrides";import { loadTrackerScript } from "./utils/analytics";if (process.env.NODE_ENV === "production") {diff --git a/apps/web/src/utils/download.js b/apps/web/src/utils/download.jsindex fb7c0da1f..526f7faac 100644--- a/apps/web/src/utils/download.js+++ b/apps/web/src/utils/download.js@@ -8,11 +8,7 @@ const map = {};export default function download(filename, text, type) {- downloadFile(- text,- sanitizeFilename(filename, { replacement: "-" }) + `.${type}`,- map[type]- );+ downloadFile(text, sanitizeFilename(filename) + `.${type}`, map[type]);}function downloadFile(data, filename, mime, bom) {diff --git a/apps/web/src/utils/filename.js b/apps/web/src/utils/filename.jsindex a19e63d14..be3123642 100644--- a/apps/web/src/utils/filename.js+++ b/apps/web/src/utils/filename.js@@ -48,7 +48,7 @@ function sanitize(input, replacement) {return sanitized.slice(0, 254).toLowerCase();}-export function sanitizeFilename(input, options) {- var replacement = (options && options.replacement) || "";+export function sanitizeFilename(input) {+ var replacement = "-";return sanitize(input, replacement);}