Skip to content

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

mobile: update react-native-sodium library

Ammar Ahmed6281de0e797d
diff --git a/apps/mobile/app/components/image-preview/index.tsx b/apps/mobile/app/components/image-preview/index.tsx
index 6bc808a5e..9df026ce0 100644
--- a/apps/mobile/app/components/image-preview/index.tsx
+++ b/apps/mobile/app/components/image-preview/index.tsx
@@ -19,17 +19,19 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
import Sodium from "@ammarahmed/react-native-sodium";
import { DataURL, getFileNameWithExtension } from "@notesnook/core";
+import { strings } from "@notesnook/intl";
import type { ImageAttributes } from "@notesnook/editor";
import { useThemeColors } from "@notesnook/theme";
import React, { useEffect, useRef, useState } from "react";
import { Platform, View } from "react-native";
import ImageViewer from "react-native-image-zoom-viewer";
-import { db } from "../../common/database";
+import { DatabaseLogger, db } from "../../common/database";
import downloadAttachment from "../../common/filesystem/download-attachment";
import { cacheDir } from "../../common/filesystem/utils";
import {
eSubscribeEvent,
- eUnSubscribeEvent
+ eUnSubscribeEvent,
+ ToastManager
} from "../../services/event-manager";
import BaseDialog from "../dialog/base-dialog";
import { IconButton } from "../ui/icon-button";
@@ -37,7 +39,6 @@ import { ProgressBarComponent } from "../ui/svg/lazy";
import RNFetchBlob from "react-native-blob-util";
import Share from "react-native-share";
import useGlobalSafeAreaInsets from "../../hooks/use-global-safe-area-insets";
-import { useSettingStore } from "../../stores/use-setting-store";
import { DefaultAppStyles } from "../../utils/styles";
import FileViewer from "react-native-file-viewer";
@@ -63,38 +64,48 @@ const ImagePreview = () => {
setVisible(true);
setLoading(true);
setTimeout(async () => {
- let hash = image.hash;
- if (!hash && image.src && DataURL.toObject(image.src)) {
- const data = DataURL.toObject(image.src);
- if (!data) return;
- hash = await Sodium.hashFile({
- data: data.data,
- type: "base64",
- uri: ""
- });
- if (imageRef.current) {
- imageRef.current.hash = hash;
+ try {
+ let hash = image.hash;
+ if (!hash && image.src && DataURL.toObject(image.src)) {
+ const data = DataURL.toObject(image.src);
+ if (!data) return;
+ hash = await Sodium.hashFile({
+ data: data.data,
+ type: "base64",
+ uri: ""
+ });
+ if (imageRef.current) {
+ imageRef.current.hash = hash;
+ }
}
- }
- if (!hash) return;
- //@ts-ignore // FIX ME
- const uri = await downloadAttachment(hash, false, {
- silent: true,
- cache: true
- });
+ if (!hash) return;
+ //@ts-ignore // FIX ME
+ const uri = await downloadAttachment(hash, false, {
+ silent: true,
+ cache: true
+ });
+
+ if (!uri) return;
- if (!uri) {
+ const attachment = await db.attachments.attachment(hash);
+ const path = `${cacheDir}/${
+ "NN_" + (await getFileNameWithExtension(hash, attachment?.mimeType))
+ }`;
+ await RNFetchBlob.fs.mv(`${cacheDir}/${uri}`, path).catch(() => {
+ /* empty */
+ });
+ setImage("file://" + path);
+ } catch (e) {
+ DatabaseLogger.error(e);
+ ToastManager.show({
+ heading: strings.failToOpen(),
+ message: (e as Error)?.message,
+ type: "error",
+ context: "global"
+ });
+ } finally {
setLoading(false);
- return;
}
- const attachment = await db.attachments.attachment(hash);
- const path = `${cacheDir}/${"NN_" + (await getFileNameWithExtension(hash, attachment?.mimeType))
- }`;
- await RNFetchBlob.fs.mv(`${cacheDir}/${uri}`, path).catch(() => {
- /* empty */
- });
- setImage("file://" + path);
- setLoading(false);
}, 100);
};
diff --git a/apps/mobile/app/screens/editor/tiptap/picker.ts b/apps/mobile/app/screens/editor/tiptap/picker.ts
index 31dfaa45f..5044e39ff 100644
--- a/apps/mobile/app/screens/editor/tiptap/picker.ts
+++ b/apps/mobile/app/screens/editor/tiptap/picker.ts
@@ -152,6 +152,19 @@ const file = async (fileOptions: PickerOptions) => {
}
};
+const onImagePickerError = (e: unknown) => {
+ const error = e as Error & { code?: string };
+ if (error?.code === "E_PICKER_CANCELLED") return;
+
+ DatabaseLogger.error(error);
+ ToastManager.show({
+ heading: strings.failToOpen(),
+ message: error?.message,
+ type: "error",
+ context: "global"
+ });
+};
+
const camera = async (options: PickerOptions) => {
try {
await db.attachments.generateKey();
@@ -165,15 +178,13 @@ const camera = async (options: PickerOptions) => {
writeTempFile: true,
compressImageQuality: 1
})
- .then((response) => {
+ .then((response) =>
handleImageResponse(
Array.isArray(response) ? response : [response],
options
- );
- })
- .catch((e) => {
- console.log(e);
- });
+ )
+ )
+ .catch(onImagePickerError);
} catch (e) {
ToastManager.show({
heading: (e as Error).message,
@@ -201,7 +212,7 @@ const gallery = async (options: PickerOptions) => {
options
)
)
- .catch((e) => {});
+ .catch(onImagePickerError);
} catch (e) {
useSettingStore.getState().setAppDidEnterBackgroundForAction(false);
ToastManager.show({
diff --git a/apps/mobile/ios/Podfile.lock b/apps/mobile/ios/Podfile.lock
index d576a454d..722aabed9 100644
--- a/apps/mobile/ios/Podfile.lock
+++ b/apps/mobile/ios/Podfile.lock
@@ -2322,8 +2322,9 @@ PODS:
- Yoga
- react-native-share-extension (2.9.5):
- React
- - react-native-sodium (1.6.8):
- - React
+ - react-native-sodium (2.0.0-alpha.0):
+ - Base64
+ - React-Core
- react-native-theme-switch-animation (0.6.0):
- boost
- DoubleConversion
@@ -4024,7 +4025,7 @@ SPEC CHECKSUMS:
react-native-safe-area-context: c6e2edd1c1da07bdce287fa9d9e60c5f7b514616
react-native-screenguard: 975d4612dce0c348b19b08a48aa23e9b68584d98
react-native-share-extension: fdc6aaab51591a2d445df239c446aaa3a99658ec
- react-native-sodium: 066f76e46c9be13e9260521e3fa994937c4cdab4
+ react-native-sodium: 3af198005f79911feb702701086195c091b9c827
react-native-theme-switch-animation: 449d6db7a760f55740505e7403ae8061debc9a7e
react-native-upload: 7b7ad336c3db703b68a8c07cf38e9319ae90d695
react-native-view-shot: 6c008e58f4720de58370848201c5d4a082c6d4ca
diff --git a/apps/mobile/package-lock.json b/apps/mobile/package-lock.json
index 95e17f28c..4112340fc 100644
--- a/apps/mobile/package-lock.json
+++ b/apps/mobile/package-lock.json
@@ -15,7 +15,7 @@
"@ammarahmed/react-native-eventsource": "1.1.0",
"@ammarahmed/react-native-fingerprint-scanner": "^5.0.1",
"@ammarahmed/react-native-share-extension": "^2.9.5",
- "@ammarahmed/react-native-sodium": "^1.6.8",
+ "@ammarahmed/react-native-sodium": "^2.0.0-alpha.0",
"@ammarahmed/react-native-upload": "^6.33.0",
"@azure/core-asynciterator-polyfill": "^1.0.2",
"@bam.tech/react-native-image-resizer": "3.0.11",
@@ -283,7 +283,9 @@
"dependencies": {
"@notesnook/sodium": "file:../sodium"
},
- "devDependencies": {}
+ "devDependencies": {
+ "vitest": "2.1.8"
+ }
},
"../../packages/editor": {
"name": "@notesnook/editor",
@@ -531,9 +533,9 @@
}
},
"node_modules/@ammarahmed/react-native-sodium": {
- "version": "1.6.8",
- "resolved": "https://registry.npmjs.org/@ammarahmed/react-native-sodium/-/react-native-sodium-1.6.8.tgz",
- "integrity": "sha512-FbX9fMfqJ3ysd+zkSS5di459tPv5iB0fhmK3dx/xo+l07sf7f1gzJu17mghSGkOKkbjXaoLPcq3XLMwWGpvapQ==",
+ "version": "2.0.0-alpha.0",
+ "resolved": "https://registry.npmjs.org/@ammarahmed/react-native-sodium/-/react-native-sodium-2.0.0-alpha.0.tgz",
+ "integrity": "sha512-42c3mGNDR3ICvxIWftom+eR4Zr2Cvbfkk9gPNd9l05EGUvvZ5hxJ1sGH9GC6PnZ7EBaPPhv2wEQZfePZ2QtKAQ==",
"license": "ISC"
},
"node_modules/@ammarahmed/react-native-upload": {
diff --git a/apps/mobile/package.json b/apps/mobile/package.json
index 38a3071a1..36dc71d4c 100644
--- a/apps/mobile/package.json
+++ b/apps/mobile/package.json
@@ -33,7 +33,7 @@
"@ammarahmed/react-native-eventsource": "1.1.0",
"@ammarahmed/react-native-fingerprint-scanner": "^5.0.1",
"@ammarahmed/react-native-share-extension": "^2.9.5",
- "@ammarahmed/react-native-sodium": "^1.6.8",
+ "@ammarahmed/react-native-sodium": "^2.0.0-alpha.0",
"@ammarahmed/react-native-upload": "^6.33.0",
"@azure/core-asynciterator-polyfill": "^1.0.2",
"@bam.tech/react-native-image-resizer": "3.0.11",