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
refactor backup & restore code
ammarahm-ed4bfe99bb0d76
diff --git a/apps/mobile/src/components/RestoreDialog/index.js b/apps/mobile/src/components/RestoreDialog/index.jsindex f0e711f66..b84dfa3af 100644--- a/apps/mobile/src/components/RestoreDialog/index.js+++ b/apps/mobile/src/components/RestoreDialog/index.js@@ -45,29 +45,48 @@ const RestoreDialog = () => {setVisible(false);};+ const restore = async () => {++ if (Platform.OS === 'android') {+ let granted = storage.requestPermission();+ if (!granted) {+ ToastEvent.show('Restore Failed! Storage access denied');+ return;+ }+ }+ setRestoring(true);+ let backup = await RNFetchBlob.fs.readFile(+ 'file:/' + item.path,+ 'utf8',+ );+ await db.backup.import(backup);+ await sleep(2000);+ setRestoring(false);+ dispatch({type: Actions.ALL});+ ToastEvent.show('Restore Complete!', 'success');+ setVisible(false);++ }++ const checkBackups = async () => {+ if (Platform.OS === "android") {+ let granted = await storage.requestPermission();+ if (!granted) {+ ToastEvent.show('Storage permission required to check for backups.');+ return;+ }+ }+ let path = await storage.checkAndCreateDir('/backups/');+ let files = await RNFetchBlob.fs.lstat(path);+ console.log(files);+ setFiles(files);+ }+return (<BaseDialoganimation="slide"visible={visible}- onShow={async () => {- if (Platform.OS === "android") {- let granted = await storage.requestPermission();- if (!granted) {- ToastEvent.show('Storage permission required to check for backups.');- return;- }- }-- let path =- Platform.OS === 'ios'- ? RNFetchBlob.fs.dirs.DocumentDir + "/backups/'"- : RNFetchBlob.fs.dirs.SDCardDir + '/Notesnook/backups/';-- await storage.checkAndCreateDir(path);- let files = await RNFetchBlob.fs.lstat(path);- console.log(files);- setFiles(files);- }}+ onShow={checkBackups}onRequestClose={close}><Viewstyle={{@@ -212,28 +231,7 @@ const RestoreDialog = () => {title="Restore"width={80}height={30}- onPress={async () => {- if (Platform.OS === 'android') {- let granted = storage.requestPermission();- if (!granted) {- ToastEvent.show('Restore Failed! Storage access denied');- return;- }- }--- setRestoring(true);- let backup = await RNFetchBlob.fs.readFile(- 'file:/' + item.path,- 'utf8',- );- await db.backup.import(backup);- await sleep(2000);- setRestoring(false);- dispatch({type: Actions.ALL});- ToastEvent.show('Restore Complete!', 'success');- setVisible(false);- }}+ onPress={restore}/></View>)}diff --git a/apps/mobile/src/services/Exporter.js b/apps/mobile/src/services/Exporter.jsindex a7155c433..ea18bd04f 100644--- a/apps/mobile/src/services/Exporter.js+++ b/apps/mobile/src/services/Exporter.js@@ -6,8 +6,6 @@ import he from "he";import RNHTMLtoPDF from "react-native-html-to-pdf";import Storage from "../utils/Storage";-const dirs = RNFetchBlob.fs.dirs;-export async function saveToPDF(note) {let androidSavePath = '/Notesnook/exported/PDF';if (Platform.OS === 'android') {@@ -16,10 +14,9 @@ export async function saveToPDF(note) {ToastEvent.show('Failed to get storage permission');return null;}- } else {- await Storage.checkAndCreateDir (dirs.DocumentDir + '/exported/PDF/');- }-+ }++ await Storage.checkAndCreateDir ('/exported/PDF/');let html = await db.notes.note(note).export('html');html = he.decode(html);@@ -39,15 +36,11 @@ export async function saveToPDF(note) {}export async function saveToMarkdown(note) {- let savePath =- Platform.OS === 'ios'- ? dirs.DocumentDir + '/exported/Markdown/'- : dirs.SDCardDir + '/Notesnook/exported/Markdown/';-- await Storage.checkAndCreateDir(savePath);++ let path = await Storage.checkAndCreateDir('/exported/Markdown/');let markdown = await db.notes.note(note.id).export('md');- let path = savePath + note.title + '.md';+ path = path + note.title + '.md';await RNFetchBlob.fs.writeFile(path, markdown, 'utf8');return {@@ -58,14 +51,10 @@ export async function saveToMarkdown(note) {}export async function saveToText(note) {- let savePath =- Platform.OS === 'ios'- ? dirs.DocumentDir + '/exported/Text/'- : dirs.SDCardDir + '/Notesnook/exported/Text/';- await Storage.checkAndCreateDir(savePath);++ let path = await Storage.checkAndCreateDir('/exported/Text/');let markdown = await db.notes.note(note.id).export('txt');-- let path = savePath + note.title + '.txt';+ path = path + note.title + '.txt';await RNFetchBlob.fs.writeFile(path, markdown, 'utf8');return {@@ -76,14 +65,10 @@ export async function saveToText(note) {}export async function saveToHTML(note) {- let savePath =- Platform.OS === 'ios'- ? dirs.DocumentDir + '/exported/Html/'- : dirs.SDCardDir + '/Notesnook/exported/Html/';- await Storage.checkAndCreateDir(savePath);++ let path = await Storage.checkAndCreateDir('/exported/Html/');let markdown = await db.notes.note(note.id).export('html');-- let path = savePath + note.title + '.html';+ path = path + note.title + '.html';await RNFetchBlob.fs.writeFile(path, markdown, 'utf8');return {diff --git a/apps/mobile/src/utils/Storage.js b/apps/mobile/src/utils/Storage.jsindex f261eb688..94eec816f 100644--- a/apps/mobile/src/utils/Storage.js+++ b/apps/mobile/src/utils/Storage.js@@ -4,6 +4,8 @@ import Sodium from 'react-native-sodium';import RNFetchBlob from 'rn-fetch-blob';import {PERMISSIONS, requestMultiple, RESULTS} from "react-native-permissions";import {MMKV} from "./MMKV";+import { Platform } from "react-native";+import { ANDROID_PATH, IOS_PATH } from ".";async function read(key, isArray = false) {let data;@@ -97,7 +99,10 @@ async function requestPermission() {return granted;}-async function checkAndCreateDir(dir) {+++async function checkAndCreateDir(path) {+ let dir = Platform.OS === "ios"? IOS_PATH + path : ANDROID_PATH + path;try {let exists = await RNFetchBlob.fs.exists(dir);let isDir = await RNFetchBlob.fs.isDir(dir);diff --git a/apps/mobile/src/utils/index.js b/apps/mobile/src/utils/index.jsindex 18aecc553..4add1ba1e 100755--- a/apps/mobile/src/utils/index.js+++ b/apps/mobile/src/utils/index.js@@ -3,6 +3,7 @@ import {eSendEvent} from '../services/EventManager';import {updateEvent} from '../components/DialogManager/recievers';import {Actions} from '../provider/Actions';import {MMKV} from "./MMKV";+import RNFetchBlob from 'rn-fetch-blob';export async function setSetting(settings, name, value) {let s = {...settings};@@ -12,6 +13,11 @@ export async function setSetting(settings, name, value) {updateEvent({type: Actions.SETTINGS, settings: s});}+export const dirs = RNFetchBlob.fs.dirs;+export const ANDROID_PATH = dirs.SDCardDir + '/Notesnook/';+export const IOS_PATH = dirs.DocumentDir;++export const getElevation = (elevation) => {return {elevation,diff --git a/apps/mobile/src/views/Settings/index.js b/apps/mobile/src/views/Settings/index.jsindex ddf8b7659..07dfb6231 100644--- a/apps/mobile/src/views/Settings/index.js+++ b/apps/mobile/src/views/Settings/index.js@@ -123,11 +123,10 @@ export const Settings = ({ navigation}) => {return;}}-eSendEvent(eOpenProgressDialog, {title: 'Backing up your data',paragraph:- "All your backups are stored in 'Phone Storage/Notesnook/backups' folder",+ "All your backups are stored in 'Phone Storage/Notesnook/backups/' folder",});let backup;try {@@ -138,18 +137,11 @@ export const Settings = ({ navigation}) => {let backupName ='notesnook_backup_' + new Date().toString() + '.nnbackup';- let path =- Platform.OS === 'ios'- ? RNFetchBlob.fs.dirs.DocumentDir + "/backups/'"- : RNFetchBlob.fs.dirs.SDCardDir + '/Notesnook/backups/';- console.log('HERE');- await storage.checkAndCreateDir(path);+ let path = await storage.checkAndCreateDir('/backups/');await RNFetchBlob.fs.writeFile(path + backupName, backup, 'utf8');-await sleep(2000);eSendEvent(eCloseProgressDialog);ToastEvent.show('Backup complete!', 'success');- //Linking.openURL('https://www.notesnook.com/privacy.html');},desc: 'Backup all your data to phone storage',},