Skip to content

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

mobile: check if file exists based on file size

ammarahm-ed31d4e88efd64
diff --git a/apps/mobile/app/common/filesystem/download.js b/apps/mobile/app/common/filesystem/download.js
index 4821e755c..7d92bb27d 100644
--- a/apps/mobile/app/common/filesystem/download.js
+++ b/apps/mobile/app/common/filesystem/download.js
@@ -24,7 +24,7 @@ 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";
+import { createCacheDir, exists } from "./io";
export async function downloadFile(filename, data, cancelToken) {
if (!data) return false;
@@ -32,12 +32,10 @@ export async function downloadFile(filename, data, cancelToken) {
await createCacheDir();
let { url, headers } = data;
-
let path = `${cacheDir}/${filename}`;
+
try {
- let exists = await RNFetchBlob.fs.exists(path);
- if (exists) {
- console.log("exists", filename);
+ if (await exists(filename)) {
return true;
}
diff --git a/apps/mobile/app/common/filesystem/io.js b/apps/mobile/app/common/filesystem/io.js
index dbaa0a681..e51600452 100644
--- a/apps/mobile/app/common/filesystem/io.js
+++ b/apps/mobile/app/common/filesystem/io.js
@@ -31,18 +31,7 @@ export async function readEncrypted(filename, key, cipherData) {
let path = `${cacheDir}/${filename}`;
try {
- const iosAppGroup =
- Platform.OS === "ios"
- ? await RNFetchBlob.fs.pathForAppGroup(IOS_APPGROUPID)
- : null;
-
- const appGroupPath = `${iosAppGroup}/${filename}`;
- let exists =
- (await RNFetchBlob.fs.exists(path)) ||
- (Platform.OS === "ios" && (await RNFetchBlob.fs.exists(appGroupPath)));
-
- if (!exists) {
- console.log("Will download file...", filename);
+ if (!(await exists(filename))) {
return false;
} else {
RNFetchBlob.fs.stat(path).then((r) => {
@@ -183,7 +172,42 @@ export async function migrateFilesFromCache() {
}
}
+const ABYTES = 17;
export async function exists(filename) {
- let exists = await RNFetchBlob.fs.exists(`${cacheDir}/${filename}`);
+ let path = `${cacheDir}/${filename}`;
+
+ const iosAppGroup =
+ Platform.OS === "ios"
+ ? await RNFetchBlob.fs.pathForAppGroup(IOS_APPGROUPID)
+ : null;
+ const appGroupPath = `${iosAppGroup}/${filename}`;
+
+ let exists = await RNFetchBlob.fs.exists(path);
+
+ // Check if file is present in app group path.
+ let existsInAppGroup = false;
+ if (!exists && Platform.OS === "ios") {
+ existsInAppGroup = await RNFetchBlob.fs.exists(appGroupPath);
+ }
+
+ if (exists || existsInAppGroup) {
+ const attachment = db.attachments.attachment(filename);
+ const totalChunks = Math.ceil(attachment.length / attachment.chunkSize);
+ const totalAbytes = totalChunks * ABYTES;
+ const expectedFileSize = attachment.length + totalAbytes;
+
+ const stat = await RNFetchBlob.fs.stat(
+ existsInAppGroup ? appGroupPath : path
+ );
+
+ if (stat.size !== expectedFileSize) {
+ RNFetchBlob.fs
+ .unlink(existsInAppGroup ? appGroupPath : path)
+ .catch(console.log);
+ return false;
+ }
+
+ exists = true;
+ }
return exists;
}
diff --git a/apps/mobile/app/screens/editor/progress.tsx b/apps/mobile/app/screens/editor/progress.tsx
index c235908df..25194927a 100644
--- a/apps/mobile/app/screens/editor/progress.tsx
+++ b/apps/mobile/app/screens/editor/progress.tsx
@@ -54,12 +54,13 @@ export const ProgressBar = () => {
? attachmentProgress?.[loading?.filename]
: undefined;
- console.log(loading);
-
useEffect(() => {
if (loading) {
- if (loading.current === loading.total && loading.success) {
- console.log("download completed");
+ console.log(loading);
+ if (
+ loading.current === loading.total &&
+ typeof loading.success === "boolean"
+ ) {
clear();
return;
}
diff --git a/apps/mobile/app/stores/use-attachment-store.ts b/apps/mobile/app/stores/use-attachment-store.ts
index 604498261..0ea08479e 100644
--- a/apps/mobile/app/stores/use-attachment-store.ts
+++ b/apps/mobile/app/stores/use-attachment-store.ts
@@ -27,6 +27,7 @@ export type AttachmentGroupProgress = {
filename: string;
canceled?: boolean;
success?: boolean;
+ error?: any;
};
interface AttachmentStore {