Skip to content

Commit

Permalink
mobile: check if file exists based on file size
Browse files Browse the repository at this point in the history
  • Loading branch information
ammarahm-ed committed Sep 21, 2023
1 parent da06184 commit 31d4e88
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 22 deletions.
8 changes: 3 additions & 5 deletions apps/mobile/app/common/filesystem/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,18 @@ 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;

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;
}

Expand Down
50 changes: 37 additions & 13 deletions apps/mobile/app/common/filesystem/io.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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;
}
9 changes: 5 additions & 4 deletions apps/mobile/app/screens/editor/progress.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions apps/mobile/app/stores/use-attachment-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export type AttachmentGroupProgress = {
filename: string;
canceled?: boolean;
success?: boolean;
error?: any;
};

interface AttachmentStore {
Expand Down

0 comments on commit 31d4e88

Please sign in to comment.