mobile: check if file exists based on file size

This commit is contained in:
ammarahm-ed
2023-09-21 11:47:35 +05:00
committed by Ammar Ahmed
parent da06184d51
commit 31d4e88efd
4 changed files with 46 additions and 22 deletions

View File

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

View File

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

View File

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

View File

@@ -27,6 +27,7 @@ export type AttachmentGroupProgress = {
filename: string;
canceled?: boolean;
success?: boolean;
error?: any;
};
interface AttachmentStore {