Skip to content

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

mobile: add bulk delete support in file storage

Ammar Ahmed1cf28c56c22f
diff --git a/apps/mobile/app/common/filesystem/index.ts b/apps/mobile/app/common/filesystem/index.ts
index e236bbe3a..fe41cda9d 100644
--- a/apps/mobile/app/common/filesystem/index.ts
+++ b/apps/mobile/app/common/filesystem/index.ts
@@ -30,7 +30,8 @@ import {
getCacheSize,
hashBase64,
readEncrypted,
- writeEncryptedBase64
+ writeEncryptedBase64,
+ bulkDeleteFiles
} from "./io";
import { uploadFile } from "./upload";
import {
@@ -61,5 +62,6 @@ export const FileStorage: IFileStorage = {
exists,
clearFileStorage,
getUploadedFileSize,
- bulkExists
+ bulkExists,
+ bulkDeleteFiles
};
diff --git a/apps/mobile/app/common/filesystem/io.ts b/apps/mobile/app/common/filesystem/io.ts
index 2c6b84e64..ff9723bea 100644
--- a/apps/mobile/app/common/filesystem/io.ts
+++ b/apps/mobile/app/common/filesystem/io.ts
@@ -30,7 +30,14 @@ import RNFetchBlob from "react-native-blob-util";
import { eSendEvent } from "../../services/event-manager";
import { IOS_APPGROUPID } from "../../utils/constants";
import { DatabaseLogger, db } from "../database";
-import { ABYTES, cacheDir, cacheDirOld, getRandomId } from "./utils";
+import {
+ ABYTES,
+ cacheDir,
+ cacheDirOld,
+ getRandomId,
+ isSuccessStatusCode,
+ parseS3Error
+} from "./utils";
export async function readEncrypted<TOutputFormat extends DataFormat>(
filename: string,
@@ -102,16 +109,41 @@ export async function writeEncryptedBase64(
};
}
+async function deleteLocalFile(filename: string) {
+ try {
+ await createCacheDir();
+ let path = cacheDir + `/${filename}`;
+ let exists = await RNFetchBlob.fs.exists(path);
+ if (Platform.OS === "ios" && !exists) {
+ const iosAppGroup =
+ Platform.OS === "ios"
+ ? await (RNFetchBlob.fs as any).pathForAppGroup(IOS_APPGROUPID)
+ : null;
+ const appGroupPath = `${iosAppGroup}/${filename}`;
+ if (await RNFetchBlob.fs.exists(appGroupPath)) {
+ RNFetchBlob.fs.unlink(appGroupPath).catch(() => {
+ /* empty */
+ });
+ return;
+ }
+ }
+ if (exists) {
+ RNFetchBlob.fs.unlink(path).catch(() => {
+ /* empty */
+ });
+ }
+ } catch (e) {
+ DatabaseLogger.error(e as Error, "deleteLocalFile");
+ }
+}
+
export async function deleteFile(
filename: string,
requestOptions?: RequestOptions
): Promise<boolean> {
await createCacheDir();
- const localFilePath = cacheDir + `/${filename}`;
if (!requestOptions) {
- RNFetchBlob.fs.unlink(localFilePath).catch(() => {
- /* empty */
- });
+ deleteLocalFile(filename);
return true;
}
@@ -122,9 +154,7 @@ export async function deleteFile(
const status = response.info().status;
const ok = status >= 200 && status < 300;
if (ok) {
- RNFetchBlob.fs.unlink(localFilePath).catch(() => {
- /* empty */
- });
+ deleteLocalFile(filename);
}
return ok;
} catch (e) {
@@ -135,6 +165,42 @@ export async function deleteFile(
}
}
+export async function bulkDeleteFiles(
+ filenames: string[],
+ requestOptions?: RequestOptions
+) {
+ await createCacheDir();
+ if (!requestOptions) {
+ filenames.forEach((filename) => {
+ deleteLocalFile(filename);
+ });
+ return true;
+ }
+
+ try {
+ const { url, headers } = requestOptions;
+ const response = await RNFetchBlob.fetch("POST", url, headers, {
+ names: filenames
+ });
+
+ const result = isSuccessStatusCode(response.respInfo.status);
+ if (result) {
+ filenames.forEach((filename) => {
+ deleteLocalFile(filename);
+ });
+ } else {
+ throw await response.text();
+ }
+ return result;
+ } catch (e) {
+ DatabaseLogger.error(
+ typeof e === "string" ? parseS3Error(e as string) : (e as Error),
+ "Could not bulk delete files"
+ );
+ return false;
+ }
+}
+
export async function clearFileStorage() {
try {
await createCacheDir();
diff --git a/apps/mobile/app/common/filesystem/utils.ts b/apps/mobile/app/common/filesystem/utils.ts
index 04db1a456..9717dadb5 100644
--- a/apps/mobile/app/common/filesystem/utils.ts
+++ b/apps/mobile/app/common/filesystem/utils.ts
@@ -193,3 +193,7 @@ export async function checkAndCreateDir(path: string) {
export const santizeUri = (uri: string) => {
return Platform.OS === "ios" ? decodeURI(uri).replace("file:///", "/") : uri;
};
+
+export function isSuccessStatusCode(statusCode: number) {
+ return statusCode >= 200 && statusCode <= 299;
+}