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
mobile: migrate to persisted app cache
ammarahm-eda81c621d4911
diff --git a/apps/mobile/app/common/filesystem/download-attachment.js b/apps/mobile/app/common/filesystem/download-attachment.jsindex bbafffb47..aa0d9959a 100644--- a/apps/mobile/app/common/filesystem/download-attachment.js+++ b/apps/mobile/app/common/filesystem/download-attachment.js@@ -62,6 +62,7 @@ export async function downloadAttachments(canceled,groupId) {+ await createCacheDir();if (!attachments || !attachments.length) return;const result = new Map();@@ -182,6 +183,7 @@ export default async function downloadAttachment(groupId: undefined}) {+ await createCacheDir();let attachment = db.attachments.attachment(hash);if (!attachment) {console.log("attachment not found");diff --git a/apps/mobile/app/common/filesystem/download.js b/apps/mobile/app/common/filesystem/download.jsindex ce1760a87..60138f431 100644--- a/apps/mobile/app/common/filesystem/download.js+++ b/apps/mobile/app/common/filesystem/download.js@@ -24,9 +24,13 @@ import { ToastEvent } from "../../services/event-manager";import { useAttachmentStore } from "../../stores/use-attachment-store";import { db } from "../database";import { cacheDir, fileCheck } from "./utils";+import { createCacheDir } from "./io";export async function downloadFile(filename, data, cancelToken) {if (!data) return false;++ await createCacheDir();+let { url, headers } = data;let path = `${cacheDir}/${filename}`;diff --git a/apps/mobile/app/common/filesystem/io.js b/apps/mobile/app/common/filesystem/io.jsindex 76dfe9e14..c1db4d66a 100644--- a/apps/mobile/app/common/filesystem/io.js+++ b/apps/mobile/app/common/filesystem/io.js@@ -20,12 +20,14 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.import { Platform } from "react-native";import Sodium from "@ammarahmed/react-native-sodium";import RNFetchBlob from "react-native-blob-util";-import { cacheDir, getRandomId } from "./utils";+import { cacheDir, cacheDirOld, getRandomId } from "./utils";import { db } from "../database";import { compressToBase64 } from "./compress";import { IOS_APPGROUPID } from "../../utils/constants";export async function readEncrypted(filename, key, cipherData) {+ await migrateFilesFromCache();+ console.log("read encrypted file...");let path = `${cacheDir}/${filename}`;try {const iosAppGroup =@@ -40,12 +42,16 @@ export async function readEncrypted(filename, key, cipherData) {if (!exists) {return false;+ } else {+ RNFetchBlob.fs.stat(path).then((r) => {+ console.log(r);+ });}const attachment = db.attachments.attachment(filename);const isPng = /(png)/g.test(attachment?.metadata.type);const isJpeg = /(jpeg|jpg)/g.test(attachment?.metadata.type);-+ console.log("decrypting....");let output = await Sodium.decryptFile(key,{@@ -59,6 +65,7 @@ export async function readEncrypted(filename, key, cipherData) {: "base64": "text");+ console.log("file decrypted...");if (cipherData.outputType === "base64" && (isPng || isJpeg)) {const dCachePath = `${cacheDir}/${output}`;output = await compressToBase64(@@ -70,7 +77,7 @@ export async function readEncrypted(filename, key, cipherData) {return output;} catch (e) {RNFetchBlob.fs.unlink(path).catch(console.log);- console.log(e);+ console.log("readEncrypted", e);return false;}}@@ -88,6 +95,7 @@ export async function hashBase64(data) {}export async function writeEncryptedBase64({ data, key }) {+ await createCacheDir();let filepath = cacheDir + `/${getRandomId("imagecache_")}`;await RNFetchBlob.fs.writeFile(filepath, data, "base64");let output = await Sodium.encryptFile(key, {@@ -103,6 +111,7 @@ export async function writeEncryptedBase64({ data, key }) {}export async function deleteFile(filename, data) {+ await createCacheDir();let delFilePath = cacheDir + `/${filename}`;if (!data) {if (!filename) return;@@ -128,15 +137,47 @@ export async function deleteFile(filename, data) {export async function clearFileStorage() {try {let files = await RNFetchBlob.fs.ls(cacheDir);+ let oldCache = await RNFetchBlob.fs.ls(cacheDirOld);+for (let file of files) {- try {- await RNFetchBlob.fs.unlink(cacheDir + `/${file}`);- } catch (e) {- console.log(e);- }+ await RNFetchBlob.fs.unlink(cacheDir + `/${file}`).catch(console.log);+ }+ for (let file of oldCache) {+ await RNFetchBlob.fs.unlink(cacheDirOld + `/${file}`).catch(console.log);+ }+ } catch (e) {+ console.log("clearFileStorage", e);+ }+}++export async function createCacheDir() {+ if (!(await RNFetchBlob.fs.exists(cacheDir))) {+ await RNFetchBlob.fs.mkdir(cacheDir);+ console.log("Cache directory created");+ }+}++export async function migrateFilesFromCache() {+ try {+ await createCacheDir();+ const migratedFilesPath = cacheDir + "/.migrated_1";+ const migrated = await RNFetchBlob.fs.exists(migratedFilesPath);+ if (migrated) return;++ let files = await RNFetchBlob.fs.ls(cacheDir);+ console.log("Files to migrate:", files.join(","));++ let oldCache = await RNFetchBlob.fs.ls(cacheDirOld);+ for (let file of oldCache) {+ if (file.startsWith("org.") || file.startsWith("com.")) continue;+ RNFetchBlob.fs+ .mv(cacheDirOld + `/${file}`, cacheDir + `/${file}`)+ .catch(console.log);+ console.log("Moved", file);}+ await RNFetchBlob.fs.createFile(migratedFilesPath, "1", "utf8");} catch (e) {- console.log(e);+ console.log("migrateFilesFromCache", e);}}diff --git a/apps/mobile/app/common/filesystem/upload.js b/apps/mobile/app/common/filesystem/upload.jsindex a50560558..16c3ec75a 100644--- a/apps/mobile/app/common/filesystem/upload.js+++ b/apps/mobile/app/common/filesystem/upload.js@@ -24,11 +24,12 @@ import { cacheDir } from "./utils";import { isImage, isDocument } from "@notesnook/core/dist/utils/filename";import { Platform } from "react-native";import { IOS_APPGROUPID } from "../../utils/constants";+import { createCacheDir } from "./io";export async function uploadFile(filename, data, cancelToken) {if (!data) return false;let { url, headers } = data;-+ await createCacheDir();DatabaseLogger.info(`Preparing to upload file: ${filename}`);try {diff --git a/apps/mobile/app/common/filesystem/utils.js b/apps/mobile/app/common/filesystem/utils.jsindex 11b92c6e1..5d57420b4 100644--- a/apps/mobile/app/common/filesystem/utils.js+++ b/apps/mobile/app/common/filesystem/utils.js@@ -21,7 +21,12 @@ import * as ScopedStorage from "react-native-scoped-storage";import { Platform } from "react-native";import RNFetchBlob from "react-native-blob-util";-export const cacheDir = RNFetchBlob.fs.dirs.CacheDir;+export const cacheDirOld = RNFetchBlob.fs.dirs.CacheDir;++export const cacheDir =+ Platform.OS == "ios"+ ? RNFetchBlob.fs.dirs.LibraryDir + "/.cache"+ : RNFetchBlob.fs.dirs.DocumentDir + "/.cache";export function getRandomId(prefix) {return Math.random()diff --git a/apps/mobile/native/package.json b/apps/mobile/native/package.jsonindex e10d98745..deb418eb0 100644--- a/apps/mobile/native/package.json+++ b/apps/mobile/native/package.json@@ -7,7 +7,7 @@"dependencies": {"@ammarahmed/notifee-react-native": "7.4.4","@ammarahmed/react-native-share-extension": "^2.5.5",- "@ammarahmed/react-native-sodium": "1.5.3",+ "@ammarahmed/react-native-sodium": "1.5.4","@bam.tech/react-native-image-resizer": "3.0.5","@callstack/repack": "^3.2.0","@react-native-clipboard/clipboard": "^1.9.0",diff --git a/apps/mobile/package-lock.json b/apps/mobile/package-lock.jsonindex 9247deb54..f1b321035 100644--- a/apps/mobile/package-lock.json+++ b/apps/mobile/package-lock.json@@ -281,7 +281,7 @@"@ammarahmed/notifee-react-native": "7.4.4","@ammarahmed/react-native-eventsource": "1.1.0","@ammarahmed/react-native-share-extension": "^2.5.5",- "@ammarahmed/react-native-sodium": "1.5.2",+ "@ammarahmed/react-native-sodium": "1.5.4","@bam.tech/react-native-image-resizer": "3.0.5","@callstack/repack": "^3.2.0","@react-native-clipboard/clipboard": "^1.9.0",@@ -2419,9 +2419,9 @@}},"node_modules/@ammarahmed/react-native-sodium": {- "version": "1.5.2",- "resolved": "https://registry.npmjs.org/@ammarahmed/react-native-sodium/-/react-native-sodium-1.5.2.tgz",- "integrity": "sha512-M309uV2DCK3EPNfsgWYthOApJhQgQKOOxYF19VerUeX7UYnm9VggW073oO0NNqqZqg5GS6bTlrtYMAz7MAKvUw=="+ "version": "1.5.4",+ "resolved": "https://registry.npmjs.org/@ammarahmed/react-native-sodium/-/react-native-sodium-1.5.4.tgz",+ "integrity": "sha512-+JwYnaSJxnwpI1947sUacFXxRxK1f9ykuxDJaGkCUKvh5CkDYC5VyZFnq2BbcuIcx4OTMOG8a/a46hWJbFKQPQ=="},"node_modules/@ampproject/remapping": {"version": "2.2.1",@@ -23703,9 +23703,9 @@}},"@ammarahmed/react-native-sodium": {- "version": "1.5.2",- "resolved": "https://registry.npmjs.org/@ammarahmed/react-native-sodium/-/react-native-sodium-1.5.2.tgz",- "integrity": "sha512-M309uV2DCK3EPNfsgWYthOApJhQgQKOOxYF19VerUeX7UYnm9VggW073oO0NNqqZqg5GS6bTlrtYMAz7MAKvUw=="+ "version": "1.5.4",+ "resolved": "https://registry.npmjs.org/@ammarahmed/react-native-sodium/-/react-native-sodium-1.5.4.tgz",+ "integrity": "sha512-+JwYnaSJxnwpI1947sUacFXxRxK1f9ykuxDJaGkCUKvh5CkDYC5VyZFnq2BbcuIcx4OTMOG8a/a46hWJbFKQPQ=="},"@ampproject/remapping": {"version": "2.2.1",@@ -26600,7 +26600,7 @@"@ammarahmed/notifee-react-native": "7.4.4","@ammarahmed/react-native-eventsource": "1.1.0","@ammarahmed/react-native-share-extension": "^2.5.5",- "@ammarahmed/react-native-sodium": "1.5.2",+ "@ammarahmed/react-native-sodium": "1.5.4","@babel/core": "^7.20.0","@babel/eslint-parser": "^7.16.5","@babel/plugin-transform-named-capturing-groups-regex": "^7.16.5",