mobile: fix picking files on ios and android

This commit is contained in:
Ammar Ahmed
2025-12-25 13:47:35 +05:00
parent 0643a74ade
commit 19e59b778d
2 changed files with 10 additions and 31 deletions

View File

@@ -22,10 +22,10 @@ import { isFeatureAvailable } from "@notesnook/common";
import { isImage } from "@notesnook/core";
import { strings } from "@notesnook/intl";
import {
pick as pickFile,
DocumentPickerOptions,
DocumentPickerResponse,
keepLocalCopy
keepLocalCopy,
KeepLocalCopyResponse,
pick as pickFile
} from "@react-native-documents/picker";
import { basename } from "pathe";
import { Platform } from "react-native";
@@ -35,26 +35,13 @@ import { DatabaseLogger, db } from "../../../common/database";
import filesystem from "../../../common/filesystem";
import { compressToFile } from "../../../common/filesystem/compress";
import AttachImage from "../../../components/dialogs/attach-image-dialog";
import {
ToastManager,
eSendEvent,
presentSheet
} from "../../../services/event-manager";
import { ToastManager } from "../../../services/event-manager";
import PremiumService from "../../../services/premium";
import { useSettingStore } from "../../../stores/use-setting-store";
import { useUserStore } from "../../../stores/use-user-store";
import { eCloseSheet } from "../../../utils/events";
import { useTabStore } from "./use-tab-store";
import { editorController, editorState } from "./utils";
const showEncryptionSheet = (file: DocumentPickerResponse) => {
presentSheet({
title: strings.encryptingAttachment(),
paragraph: strings.encryptingAttachmentDesc(file.name || ""),
icon: "attachment"
});
};
const santizeUri = (uri: string) => {
uri = decodeURI(uri);
uri = Platform.OS === "ios" ? uri.replace("file:///", "/") : uri;
@@ -80,7 +67,7 @@ const file = async (fileOptions: PickerOptions) => {
await db.attachments.generateKey();
let file;
let fileCopyUri;
let fileCopyUri: KeepLocalCopyResponse[0];
let fileName;
try {
useSettingStore.getState().setAppDidEnterBackgroundForAction(true);
@@ -97,12 +84,10 @@ const file = async (fileOptions: PickerOptions) => {
});
fileCopyUri = result[0];
} catch (e) {
DatabaseLogger.error(e as Error, "Error picking file");
return;
}
let uri =
Platform.OS === "ios" ? fileCopyUri.sourceUri || file.uri : file.uri;
const featureResult = await isFeatureAvailable("fileSize", file.size || 0);
if (!featureResult.isAllowed) {
ToastManager.show({
@@ -122,8 +107,8 @@ const file = async (fileOptions: PickerOptions) => {
return;
}
let uri = fileCopyUri.localUri;
uri = Platform.OS === "ios" ? santizeUri(uri) : uri;
showEncryptionSheet(file);
const hash = await Sodium.hashFile({
uri: uri,
type: "url"
@@ -139,7 +124,8 @@ const file = async (fileOptions: PickerOptions) => {
) {
throw new Error("Failed to attach file");
}
if (Platform.OS === "ios") await RNFetchBlob.fs.unlink(uri);
await RNFetchBlob.fs.unlink(uri);
if (
fileOptions.tabId !== undefined &&
@@ -173,10 +159,7 @@ const file = async (fileOptions: PickerOptions) => {
} else {
throw new Error("Failed to attach file, no tabId is set");
}
eSendEvent(eCloseSheet);
} catch (e) {
eSendEvent(eCloseSheet);
ToastManager.show({
heading: (e as Error).message,
type: "error",

View File

@@ -367,7 +367,6 @@ export const RestoreBackup = () => {
disableAppLockRequests: true
});
const file = await pick();
const fileCopy = await keepLocalCopy({
destination: "cachesDirectory",
files: [
@@ -390,10 +389,7 @@ export const RestoreBackup = () => {
}, 1000);
restoreBackup({
uri:
Platform.OS === "android"
? (("file://" + fileCopy[0].sourceUri) as string)
: (fileCopy[0].sourceUri as string),
uri: fileCopy[0].localUri,
deleteFile: true
});
},