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
android: use scoped storage for exporting note
ammarahm-edd197ca6496e6
diff --git a/apps/mobile/src/services/Exporter.js b/apps/mobile/src/services/Exporter.jsindex d8716f519..9e9eaf6ab 100644--- a/apps/mobile/src/services/Exporter.js+++ b/apps/mobile/src/services/Exporter.js@@ -1,11 +1,11 @@-import {Platform} from 'react-native';-import {db} from '../utils/database';-import {ToastEvent} from './EventManager';+import jsdom from 'jsdom-jscore-rn';+import { Platform } from 'react-native';import RNHTMLtoPDF from 'react-native-html-to-pdf-lite';-import Storage from '../utils/storage';+import * as ScopedStorage from 'react-native-scoped-storage';import showdown from 'showdown';-import jsdom from 'jsdom-jscore-rn';-import {sanitizeFilename} from '../utils/filename';+import { db } from '../utils/database';+import { sanitizeFilename } from '../utils/filename';+import Storage from '../utils/storage';let RNFetchBlob;@@ -14,7 +14,7 @@ const defaultStyle = `<style>width:100%;}.img_size_two {- width:50%;+ width:50%;}.img_size_three {width:25%;@@ -61,19 +61,13 @@ td {async function saveToPDF(note) {let androidSavePath = '/Notesnook/exported/PDF';if (Platform.OS === 'android') {- let hasPermission = await Storage.requestPermission();- if (!hasPermission) {- ToastEvent.show({- heading: 'Cannot export',- message: 'You must provide phone storage access to backup data.',- type: 'error',- context: 'local',- });- return null;- }+ let file = await ScopedStorage.openDocumentTree(true);+ if (!file) return;+ androidSavePath = file.uri;}- await Storage.checkAndCreateDir('/exported/PDF/');+ Platform.OS === 'ios' && (await Storage.checkAndCreateDir('/exported/PDF/'));+let html = await db.notes.note(note).export('html');let he = require('he');html = he.decode(html);@@ -88,34 +82,49 @@ async function saveToPDF(note) {let options = {html: html3,fileName: Platform.OS === 'ios' ? '/exported/PDF/' + fileName : fileName,- directory: Platform.OS === 'ios' ? 'Documents' : androidSavePath,width: 595,height: 852,bgColor: '#FFFFFF',padding: 30,+ base64: Platform.OS === 'android'};+ if (Platform.OS === 'ios') {+ options.directory = 'Documents';+ }let res = await RNHTMLtoPDF.convert(options);+ let fileUri;+ if (Platform.OS === 'android') {+ fileUri = await ScopedStorage.writeFile(+ androidSavePath,+ fileName,+ 'application/pdf',+ res.base64,+ 'base64',+ false+ );+ await ScopedStorage.releasePersistableUriPermission(androidSavePath);+ if (res.filePath) {+ await RNFetchBlob.fs.unlink(res.filePath);+ }+ }+return {- filePath: res.filePath,+ filePath: fileUri || res.filePath,type: 'application/pdf',name: 'PDF',+ fileName: fileName};}async function saveToMarkdown(note) {- let path = await Storage.checkAndCreateDir('/exported/Markdown/');+ let path =+ Platform.OS === 'ios' &&+ (await Storage.checkAndCreateDir('/exported/Markdown/'));if (Platform.OS === 'android') {- let hasPermission = await Storage.requestPermission();- if (!hasPermission) {- ToastEvent.show({- heading: 'Cannot export',- message: 'You must provide phone storage access to backup data.',- type: 'error',- context: 'local',- });- return null;- }+ let file = await ScopedStorage.openDocumentTree(true);+ if (!file) return;+ path = file.uri;}RNFetchBlob = require('rn-fetch-blob').default;@@ -129,60 +138,80 @@ async function saveToMarkdown(note) {markdown = await db.notes.note(note.id).export('md', markdown);let fileName = sanitizeFilename(note.title + Date.now(), {replacement: '_'});- path = path + fileName + '.md';- await RNFetchBlob.fs.writeFile(path, markdown, 'utf8');+ let fileUri;+ if (Platform.OS === 'android') {+ fileUri = await ScopedStorage.writeFile(+ path,+ fileName + '.md',+ 'text/markdown',+ markdown,+ 'utf8',+ false+ );+ await ScopedStorage.releasePersistableUriPermission(path);+ } else {+ path = path + fileName + '.md';+ await RNFetchBlob.fs.writeFile(path, markdown, 'utf8');+ }return {- filePath: path,+ filePath: fileUri || path,type: 'text/markdown',name: 'Markdown',+ fileName: fileName};}async function saveToText(note) {- let path = await Storage.checkAndCreateDir('/exported/Text/');+ let path =+ Platform.OS === 'ios' &&+ (await Storage.checkAndCreateDir('/exported/Text/'));if (Platform.OS === 'android') {- let hasPermission = await Storage.requestPermission();- if (!hasPermission) {- ToastEvent.show({- heading: 'Cannot export',- message: 'You must provide phone storage access to backup data.',- type: 'error',- context: 'local',- });- return null;- }+ let file = await ScopedStorage.openDocumentTree(true);+ if (!file) return;+ path = file.uri;}+RNFetchBlob = require('rn-fetch-blob').default;let text = await db.notes.note(note.id).export('txt');let fileName = sanitizeFilename(note.title + Date.now(), {replacement: '_'});- path = path + fileName + '.txt';- await RNFetchBlob.fs.writeFile(path, text, 'utf8');+ let fileUri;+ if (Platform.OS === 'android') {+ fileUri = await ScopedStorage.writeFile(+ path,+ fileName + '.txt',+ 'text/plain',+ text,+ 'utf8',+ false+ );+ await ScopedStorage.releasePersistableUriPermission(path);+ } else {+ path = path + fileName + '.txt';+ await RNFetchBlob.fs.writeFile(path, text, 'utf8');+ }return {- filePath: path,+ filePath: fileUri || path,type: 'text/plain',name: 'Text',+ fileName: fileName};}async function saveToHTML(note) {- let path = await Storage.checkAndCreateDir('/exported/Html/');+ let path =+ Platform.OS === 'ios' &&+ (await Storage.checkAndCreateDir('/exported/Html/'));if (Platform.OS === 'android') {- let hasPermission = await Storage.requestPermission();- if (!hasPermission) {- ToastEvent.show({- heading: 'Cannot export',- message: 'You must provide phone storage access to backup data.',- type: 'error',- context: 'local',- });- return null;- }+ let file = await ScopedStorage.openDocumentTree(true);+ if (!file) return;+ path = file.uri;}+RNFetchBlob = require('rn-fetch-blob').default;let html = await db.notes.note(note.id).export('html');let fileName = sanitizeFilename(note.title + Date.now(), {replacement: '_'});@@ -193,13 +222,27 @@ async function saveToHTML(note) {html3 = html1 + defaultStyle + html2;}- path = path + fileName + '.html';- await RNFetchBlob.fs.writeFile(path, html3, 'utf8');+ let fileUri;+ if (Platform.OS === 'android') {+ fileUri = await ScopedStorage.writeFile(+ path,+ fileName + '.html',+ 'text/html',+ html3,+ 'utf8',+ false+ );+ await ScopedStorage.releasePersistableUriPermission(path);+ } else {+ path = path + fileName + '.html';+ await RNFetchBlob.fs.writeFile(path, html3, 'utf8');+ }return {- filePath: path,+ filePath: fileUri || path,type: 'text/html',name: 'Html',+ fileName: fileName};}@@ -207,5 +250,5 @@ export default {saveToHTML,saveToText,saveToMarkdown,- saveToPDF,+ saveToPDF};