Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

mobile: load compressed images in editor

ammarahm-ed4c541f399598
diff --git a/apps/mobile/app/common/filesystem/compress.js b/apps/mobile/app/common/filesystem/compress.js
new file mode 100644
index 000000000..1f81b747b
--- /dev/null
+++ b/apps/mobile/app/common/filesystem/compress.js
@@ -0,0 +1,50 @@
+/*
+This file is part of the Notesnook project (https://notesnook.com/)
+
+Copyright (C) 2023 Streetwriters (Private) Limited
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+import { Dimensions, Platform } from "react-native";
+import ImageResizer from "@bam.tech/react-native-image-resizer";
+import RNFetchBlob from "rn-fetch-blob";
+/**
+ * Scale down & compress images to screen width
+ * for loading in editor.
+ * @returns
+ */
+export async function compressToBase64(path, type) {
+ const { width, scale } = Dimensions.get("window");
+ const response = await ImageResizer.createResizedImage(
+ path,
+ width * scale,
+ 9999,
+ type,
+ 80,
+ 0,
+ undefined,
+ true,
+ {
+ mode: "contain",
+ onlyScaleDown: true
+ }
+ );
+ const base64 = await RNFetchBlob.fs.readFile(
+ Platform.OS === "ios" ? response.uri?.replace("file://", "") : response.uri,
+ "base64"
+ );
+ RNFetchBlob.fs.unlink(path.replace("file://", "")).catch(console.log);
+ RNFetchBlob.fs.unlink(response.uri.replace("file://", "")).catch(console.log);
+ return base64;
+}
diff --git a/apps/mobile/app/common/filesystem/io.js b/apps/mobile/app/common/filesystem/io.js
index 9d0fa71f9..16ebc464f 100644
--- a/apps/mobile/app/common/filesystem/io.js
+++ b/apps/mobile/app/common/filesystem/io.js
@@ -21,6 +21,8 @@ import { Platform } from "react-native";
import Sodium from "@ammarahmed/react-native-sodium";
import RNFetchBlob from "rn-fetch-blob";
import { cacheDir, getRandomId } from "./utils";
+import { db } from "../database";
+import { compressToBase64 } from "./compress";
export async function readEncrypted(filename, key, cipherData) {
let path = `${cacheDir}/${filename}`;
@@ -29,6 +31,9 @@ export async function readEncrypted(filename, key, cipherData) {
if (!exists) {
return false;
}
+ const attachment = db.attachments.attachment(filename);
+ const isPng = /(png)/g.test(attachment?.metadata.type);
+ const isJpeg = /(jpeg|jpg)/g.test(attachment?.metadata.type);
let output = await Sodium.decryptFile(
key,
@@ -36,8 +41,20 @@ export async function readEncrypted(filename, key, cipherData) {
...cipherData,
hash: filename
},
- cipherData.outputType === "base64" ? "base64" : "text"
+ cipherData.outputType === "base64"
+ ? isPng || isJpeg
+ ? "cache"
+ : "base64"
+ : "text"
);
+ if (cipherData.outputType === "base64" && (isPng || isJpeg)) {
+ const dCachePath = `${cacheDir}/${output}`;
+ output = await compressToBase64(
+ `file://${dCachePath}`,
+ isPng ? "PNG" : "JPEG"
+ );
+ }
+
return output;
} catch (e) {
RNFetchBlob.fs.unlink(path).catch(console.log);
diff --git a/apps/mobile/app/screens/editor/index.tsx b/apps/mobile/app/screens/editor/index.tsx
index 0d83e1dd1..71137f974 100755
--- a/apps/mobile/app/screens/editor/index.tsx
+++ b/apps/mobile/app/screens/editor/index.tsx
@@ -107,7 +107,6 @@ const Editor = React.memo(
attachmentType: string;
}) => {
if (groupId !== editor.note.current?.id) return;
- console.log("marking image as loaded", hash);
editorController.current.markImageLoaded(hash);
if (attachmentType === "webclip") {
editor.commands.updateWebclip({
diff --git a/apps/mobile/app/screens/editor/tiptap/commands.ts b/apps/mobile/app/screens/editor/tiptap/commands.ts
index 3b3f303f5..e8340de6c 100644
--- a/apps/mobile/app/screens/editor/tiptap/commands.ts
+++ b/apps/mobile/app/screens/editor/tiptap/commands.ts
@@ -57,7 +57,7 @@ const fn = (fn: string) => {
if (DEV_MODE && typeof logger !== "undefined") logger('error', "webview: ", e.message, e.stack);
}
return true;
- })();`,
+ })();true;`,
id: id
};
};
diff --git a/apps/mobile/app/screens/editor/tiptap/picker.js b/apps/mobile/app/screens/editor/tiptap/picker.js
index cf1e8b10b..757dc8427 100644
--- a/apps/mobile/app/screens/editor/tiptap/picker.js
+++ b/apps/mobile/app/screens/editor/tiptap/picker.js
@@ -34,6 +34,7 @@ import PremiumService from "../../../services/premium";
import { eCloseSheet } from "../../../utils/events";
import { sleep } from "../../../utils/time";
import { editorController, editorState } from "./utils";
+import { compressToBase64 } from "../../../common/filesystem/compress";
const FILE_SIZE_LIMIT = 500 * 1024 * 1024;
const IMAGE_SIZE_LIMIT = 50 * 1024 * 1024;
@@ -124,6 +125,7 @@ const file = async (fileOptions) => {
});
if (!(await attachFile(uri, hash, file.type, file.name, fileOptions)))
return;
+ if (Platform.OS === "ios") await RNFetchBlob.fs.unlink(uri);
editorController.current?.commands.insertAttachment({
hash: hash,
filename: file.name,
@@ -234,7 +236,7 @@ const handleImageResponse = async (response, options) => {
});
return;
}
- const b64 = `data:${image.type};base64, ` + image.base64;
+ let b64 = `data:${image.type};base64, ` + image.base64;
const uri = decodeURI(image.uri);
const hash = await Sodium.hashFile({
uri: uri,
@@ -243,6 +245,15 @@ const handleImageResponse = async (response, options) => {
let fileName = image.originalFileName || image.fileName;
if (!(await attachFile(uri, hash, image.type, fileName, options))) return;
+ const isPng = /(png)/g.test(image.type);
+ const isJpeg = /(jpeg|jpg)/g.test(image.type);
+ if (isPng || isJpeg) {
+ b64 =
+ `data:${image.type};base64, ` +
+ (await compressToBase64(image.uri, isPng ? "PNG" : "JPEG"));
+ }
+
+ if (Platform.OS === "ios") await RNFetchBlob.fs.unlink(uri);
editorController.current?.commands.insertImage({
hash: hash,
@@ -289,7 +300,6 @@ async function attachFile(uri, hash, type, filename, options) {
encryptionInfo,
editorController.current?.note?.id
);
- if (Platform.OS === "ios") await RNFetchBlob.fs.unlink(uri);
return true;
} catch (e) {
diff --git a/apps/mobile/app/screens/editor/tiptap/use-editor.ts b/apps/mobile/app/screens/editor/tiptap/use-editor.ts
index 5714d6342..733fa20f2 100644
--- a/apps/mobile/app/screens/editor/tiptap/use-editor.ts
+++ b/apps/mobile/app/screens/editor/tiptap/use-editor.ts
@@ -157,6 +157,7 @@ export const useEditor = (
currentNote.current?.id && db.fs.cancel(currentNote.current.id);
currentNote.current = null;
attachedImages.current = [];
+ loadedImages.current = [];
currentContent.current = null;
clearTimeout(timers.current["loading-images"]);
sessionHistoryId.current = undefined;
@@ -337,7 +338,7 @@ export const useEditor = (
db.attachments?.downloadMedia(currentNote.current?.id, images);
}
}
- }, 300);
+ }, 100);
}, []);
const loadNote = useCallback(
@@ -364,7 +365,6 @@ export const useEditor = (
if (!item.forced && currentNote.current?.id === item.id) return;
isDefaultEditor && editorState.setCurrentlyEditingNote(item.id);
overlay(true, item);
- attachedImages.current = [];
currentNote.current && (await reset(false));
await loadContent(item as NoteType);
lastContentChangeTime.current = item.dateEdited;
diff --git a/apps/mobile/native/ios/Podfile.lock b/apps/mobile/native/ios/Podfile.lock
index 750b2c710..6e2c3583c 100644
--- a/apps/mobile/native/ios/Podfile.lock
+++ b/apps/mobile/native/ios/Podfile.lock
@@ -254,6 +254,8 @@ PODS:
- React
- react-native-image-picker (4.1.2):
- React-Core
+ - react-native-image-resizer (3.0.5):
+ - React-Core
- react-native-keep-awake (1.1.0):
- React-Core
- react-native-mmkv-storage (0.8.0):
@@ -271,7 +273,7 @@ PODS:
- RCTTypeSafety
- React-Core
- ReactCommon/turbomodule/core
- - react-native-sodium (1.2.0):
+ - react-native-sodium (1.3.0):
- React
- react-native-webview (11.23.1):
- React-Core
@@ -459,6 +461,7 @@ DEPENDENCIES:
- react-native-gzip (from `../../node_modules/react-native-gzip`)
- react-native-html-to-pdf-lite (from `../../node_modules/react-native-html-to-pdf-lite`)
- react-native-image-picker (from `../../node_modules/react-native-image-picker`)
+ - "react-native-image-resizer (from `../../node_modules/@bam.tech/react-native-image-resizer`)"
- "react-native-keep-awake (from `../../node_modules/@sayem314/react-native-keep-awake`)"
- react-native-mmkv-storage (from `../../node_modules/react-native-mmkv-storage`)
- "react-native-netinfo (from `../../node_modules/@react-native-community/netinfo`)"
@@ -578,6 +581,8 @@ EXTERNAL SOURCES:
:path: "../../node_modules/react-native-html-to-pdf-lite"
react-native-image-picker:
:path: "../../node_modules/react-native-image-picker"
+ react-native-image-resizer:
+ :path: "../../node_modules/@bam.tech/react-native-image-resizer"
react-native-keep-awake:
:path: "../../node_modules/@sayem314/react-native-keep-awake"
react-native-mmkv-storage:
@@ -714,13 +719,14 @@ SPEC CHECKSUMS:
react-native-gzip: 02f9968afa759e189f0414d41f8f4a951a86b4f1
react-native-html-to-pdf-lite: 21bfb169bf4cbcd7bec9f736975ee1b3f5292d4a
react-native-image-picker: 9c8a2687b69300ad9e95cec5d38f35ab9d32467d
+ react-native-image-resizer: 00ceb0e05586c7aadf061eea676957a6c2ec60fa
react-native-keep-awake: acbee258db16483744910f0da3ace39eb9ab47fd
react-native-mmkv-storage: 8ba3c0216a6df283ece11205b442a3e435aec4e5
react-native-netinfo: 2517ad504b3d303e90d7a431b0fcaef76d207983
react-native-notification-sounds: da78c828fe1bcbb92d8b505d5261890ed315ff39
react-native-orientation: f1caf84d65f1a4fd4511a18f2b924e634ad7a628
react-native-safe-area-context: 99b24a0c5acd0d5dcac2b1a7f18c49ea317be99a
- react-native-sodium: 460beee758415bd72cd8d820820fae0bc4076f07
+ react-native-sodium: 1681828855ec18fa952f4557cd595bf048cf5c32
react-native-webview: d33e2db8925d090871ffeb232dfa50cb3a727581
React-perflogger: 8e832d4e21fdfa613033c76d58d7e617341e804b
React-RCTActionSheet: 9ca778182a9523991bff6381045885b6e808bb73
diff --git a/apps/mobile/native/package.json b/apps/mobile/native/package.json
index 53d1cc008..5753d4dfd 100644
--- a/apps/mobile/native/package.json
+++ b/apps/mobile/native/package.json
@@ -43,7 +43,7 @@
"react-native-screens": "^3.13.1",
"react-native-securerandom": "^1.0.1",
"react-native-share": "^7.2.0",
- "@ammarahmed/react-native-sodium": "1.2.0",
+ "@ammarahmed/react-native-sodium": "1.3.0",
"react-native-svg": "^12.3.0",
"react-native-tooltips": "^1.0.3",
"react-native-vector-icons": "^9.0.0",
@@ -56,7 +56,8 @@
"react-native-modal-datetime-picker":"14.0.0",
"@react-native-community/datetimepicker":"6.6.0",
"react-native-date-picker": "4.2.6",
- "react-native-notification-sounds": "0.5.5"
+ "react-native-notification-sounds": "0.5.5",
+ "@bam.tech/react-native-image-resizer": "3.0.5"
},
"devDependencies": {
"@babel/core": "^7.12.9",
diff --git a/apps/mobile/package-lock.json b/apps/mobile/package-lock.json
index cce93de46..1a0976eb0 100644
--- a/apps/mobile/package-lock.json
+++ b/apps/mobile/package-lock.json
@@ -61,7 +61,8 @@
"license": "GPL-3.0-or-later",
"dependencies": {
"@ammarahmed/notifee-react-native": "7.4.3",
- "@ammarahmed/react-native-sodium": "1.2.0",
+ "@ammarahmed/react-native-sodium": "1.3.0",
+ "@bam.tech/react-native-image-resizer": "3.0.5",
"@callstack/repack": "^3.0.0",
"@react-native-clipboard/clipboard": "^1.9.0",
"@react-native-community/checkbox": "^0.5.8",
@@ -162,9 +163,9 @@
}
},
"node_modules/@ammarahmed/react-native-sodium": {
- "version": "1.2.0",
- "resolved": "https://registry.npmjs.org/@ammarahmed/react-native-sodium/-/react-native-sodium-1.2.0.tgz",
- "integrity": "sha512-KOVTeQMIeo8BXHBABIlYDj9QhhcrVzqxaLRra2gg2pB08V3Nq/fPPVexRf0Ir8q7vChd1+gzEFWjMYlvTIssQg=="
+ "version": "1.3.0",
+ "resolved": "https://registry.npmjs.org/@ammarahmed/react-native-sodium/-/react-native-sodium-1.3.0.tgz",
+ "integrity": "sha512-Z0wjclwl69QttV6kupzY4bHrRX4ijjZMhjhy1P+WodNCrJpSnG4hI1Jne0hjKi0ghajuFZi+yPB8rjnDvsgQjA=="
},
"node_modules/@ampproject/remapping": {
"version": "2.2.0",
@@ -2131,6 +2132,15 @@
"node": ">=6.9.0"
}
},
+ "node_modules/@bam.tech/react-native-image-resizer": {
+ "version": "3.0.5",
+ "resolved": "https://registry.npmjs.org/@bam.tech/react-native-image-resizer/-/react-native-image-resizer-3.0.5.tgz",
+ "integrity": "sha512-u5QGUQGGVZiVCJ786k9/kd7pPRZ6eYfJCYO18myVCH8FbVI7J8b5GT2Svjj2x808DlWeqfaZOOzxPqo27XYvrQ==",
+ "peerDependencies": {
+ "react": "*",
+ "react-native": "*"
+ }
+ },
"node_modules/@bcoe/v8-coverage": {
"version": "0.2.3",
"resolved": "https://registry.npmjs.org/@bcoe/v8-coverage/-/v8-coverage-0.2.3.tgz",
@@ -21515,9 +21525,9 @@
"integrity": "sha512-7jz91BZLxsz3v2nOPSnUf0ULa/k+KtkyX8zN2ijeluLHSQ5fjSd5jQ+u+4+Rcja0g0xQze7fpg26q6lA6F2j1g=="
},
"@ammarahmed/react-native-sodium": {
- "version": "1.2.0",
- "resolved": "https://registry.npmjs.org/@ammarahmed/react-native-sodium/-/react-native-sodium-1.2.0.tgz",
- "integrity": "sha512-KOVTeQMIeo8BXHBABIlYDj9QhhcrVzqxaLRra2gg2pB08V3Nq/fPPVexRf0Ir8q7vChd1+gzEFWjMYlvTIssQg=="
+ "version": "1.3.0",
+ "resolved": "https://registry.npmjs.org/@ammarahmed/react-native-sodium/-/react-native-sodium-1.3.0.tgz",
+ "integrity": "sha512-Z0wjclwl69QttV6kupzY4bHrRX4ijjZMhjhy1P+WodNCrJpSnG4hI1Jne0hjKi0ghajuFZi+yPB8rjnDvsgQjA=="
},
"@ampproject/remapping": {
"version": "2.2.0",
@@ -22856,6 +22866,11 @@
"to-fast-properties": "^2.0.0"
}
},
+ "@bam.tech/react-native-image-resizer": {
+ "version": "3.0.5",
+ "resolved": "https://registry.npmjs.org/@bam.tech/react-native-image-resizer/-/react-native-image-resizer-3.0.5.tgz",
+ "integrity": "sha512-u5QGUQGGVZiVCJ786k9/kd7pPRZ6eYfJCYO18myVCH8FbVI7J8b5GT2Svjj2x808DlWeqfaZOOzxPqo27XYvrQ=="
+ },
"@bcoe/v8-coverage": {
"version": "0.2.3",
"resolved": "https://registry.npmjs.org/@bcoe/v8-coverage/-/v8-coverage-0.2.3.tgz",
@@ -24311,12 +24326,13 @@
"version": "file:native",
"requires": {
"@ammarahmed/notifee-react-native": "7.4.3",
- "@ammarahmed/react-native-sodium": "1.2.0",
+ "@ammarahmed/react-native-sodium": "1.3.0",
"@babel/core": "^7.12.9",
"@babel/eslint-parser": "^7.16.5",
"@babel/plugin-transform-named-capturing-groups-regex": "^7.16.5",
"@babel/preset-env": "^7.18.6",
"@babel/runtime": "^7.12.5",
+ "@bam.tech/react-native-image-resizer": "3.0.5",
"@callstack/repack": "^3.0.0",
"@react-native-clipboard/clipboard": "^1.9.0",
"@react-native-community/checkbox": "^0.5.8",