2022-08-31 06:33:37 +05:00
|
|
|
/*
|
|
|
|
|
This file is part of the Notesnook project (https://notesnook.com/)
|
|
|
|
|
|
2023-01-16 13:44:52 +05:00
|
|
|
Copyright (C) 2023 Streetwriters (Private) Limited
|
2022-08-31 06:33:37 +05:00
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
2022-08-30 16:13:11 +05:00
|
|
|
|
2022-11-28 18:47:41 +05:00
|
|
|
import Sodium from "@ammarahmed/react-native-sodium";
|
2024-03-01 20:28:21 +05:00
|
|
|
import { Platform } from "react-native";
|
2023-05-19 11:05:16 +05:00
|
|
|
import RNFetchBlob from "react-native-blob-util";
|
2024-08-07 11:17:05 +05:00
|
|
|
import { eSendEvent } from "../../services/event-manager";
|
2023-06-05 16:26:39 +05:00
|
|
|
import { IOS_APPGROUPID } from "../../utils/constants";
|
2024-05-04 00:31:02 +05:00
|
|
|
import { DatabaseLogger, db } from "../database";
|
2024-08-08 12:19:21 +05:00
|
|
|
import { ABYTES, cacheDir, cacheDirOld, getRandomId } from "./utils";
|
2022-02-28 13:48:59 +05:00
|
|
|
|
|
|
|
|
export async function readEncrypted(filename, key, cipherData) {
|
2023-09-21 13:18:12 +05:00
|
|
|
await migrateFilesFromCache();
|
2024-05-04 00:31:02 +05:00
|
|
|
DatabaseLogger.log("Read encrypted file...");
|
2022-02-28 13:48:59 +05:00
|
|
|
let path = `${cacheDir}/${filename}`;
|
2023-09-20 19:45:57 +05:00
|
|
|
|
2022-02-28 13:48:59 +05:00
|
|
|
try {
|
2023-09-21 11:47:35 +05:00
|
|
|
if (!(await exists(filename))) {
|
2022-02-28 13:48:59 +05:00
|
|
|
return false;
|
|
|
|
|
}
|
2023-06-05 16:26:39 +05:00
|
|
|
|
2022-02-28 13:48:59 +05:00
|
|
|
let output = await Sodium.decryptFile(
|
|
|
|
|
key,
|
|
|
|
|
{
|
|
|
|
|
...cipherData,
|
2023-06-05 16:26:39 +05:00
|
|
|
hash: filename,
|
|
|
|
|
appGroupId: IOS_APPGROUPID
|
2022-02-28 13:48:59 +05:00
|
|
|
},
|
2024-03-01 20:28:21 +05:00
|
|
|
cipherData.outputType === "base64" ? "base64" : "text"
|
2022-02-28 13:48:59 +05:00
|
|
|
);
|
2024-05-04 00:31:02 +05:00
|
|
|
|
|
|
|
|
DatabaseLogger.log("File decrypted...");
|
2023-02-27 12:58:48 +05:00
|
|
|
|
2022-02-28 13:48:59 +05:00
|
|
|
return output;
|
|
|
|
|
} catch (e) {
|
|
|
|
|
RNFetchBlob.fs.unlink(path).catch(console.log);
|
2024-05-04 00:31:02 +05:00
|
|
|
DatabaseLogger.error(e);
|
2022-02-28 13:48:59 +05:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-22 10:42:41 +05:00
|
|
|
export async function hashBase64(data) {
|
2023-03-22 12:41:33 +05:00
|
|
|
const hash = await Sodium.hashFile({
|
|
|
|
|
type: "base64",
|
|
|
|
|
data,
|
|
|
|
|
uri: ""
|
|
|
|
|
});
|
2023-03-22 10:42:41 +05:00
|
|
|
return {
|
2023-03-22 12:41:33 +05:00
|
|
|
hash: hash,
|
2023-03-22 10:42:41 +05:00
|
|
|
type: "xxh64"
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-18 09:45:56 +05:00
|
|
|
export async function writeEncryptedBase64(data, key) {
|
2023-09-21 13:18:12 +05:00
|
|
|
await createCacheDir();
|
2022-08-26 16:19:39 +05:00
|
|
|
let filepath = cacheDir + `/${getRandomId("imagecache_")}`;
|
|
|
|
|
await RNFetchBlob.fs.writeFile(filepath, data, "base64");
|
2022-02-28 13:48:59 +05:00
|
|
|
let output = await Sodium.encryptFile(key, {
|
2022-08-27 15:23:11 +05:00
|
|
|
uri: Platform.OS === "ios" ? filepath : "file://" + filepath,
|
2022-08-26 16:19:39 +05:00
|
|
|
type: "url"
|
2022-02-28 13:48:59 +05:00
|
|
|
});
|
|
|
|
|
RNFetchBlob.fs.unlink(filepath).catch(console.log);
|
2022-08-26 16:19:39 +05:00
|
|
|
console.log("encrypted file output: ", output);
|
2024-04-18 09:45:56 +05:00
|
|
|
output.size = output.length;
|
|
|
|
|
delete output.length;
|
2022-02-28 13:48:59 +05:00
|
|
|
return {
|
|
|
|
|
...output,
|
2022-08-27 15:23:11 +05:00
|
|
|
alg: "xcha-stream"
|
2022-02-28 13:48:59 +05:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function deleteFile(filename, data) {
|
2023-09-21 13:18:12 +05:00
|
|
|
await createCacheDir();
|
2022-03-07 15:19:07 +05:00
|
|
|
let delFilePath = cacheDir + `/${filename}`;
|
2022-02-28 13:48:59 +05:00
|
|
|
if (!data) {
|
|
|
|
|
if (!filename) return;
|
|
|
|
|
RNFetchBlob.fs.unlink(delFilePath).catch(console.log);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let { url, headers } = data;
|
|
|
|
|
try {
|
2022-08-26 16:19:39 +05:00
|
|
|
let response = await RNFetchBlob.fetch("DELETE", url, headers);
|
2022-02-28 13:48:59 +05:00
|
|
|
let status = response.info().status;
|
2022-03-14 16:26:48 +05:00
|
|
|
let ok = status >= 200 && status < 300;
|
|
|
|
|
if (ok) {
|
2022-03-07 15:19:07 +05:00
|
|
|
RNFetchBlob.fs.unlink(delFilePath).catch(console.log);
|
|
|
|
|
}
|
2022-03-14 16:26:48 +05:00
|
|
|
return ok;
|
2022-02-28 13:48:59 +05:00
|
|
|
} catch (e) {
|
2022-08-26 16:19:39 +05:00
|
|
|
console.log("delete file: ", e, url, headers);
|
2022-02-28 13:48:59 +05:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function clearFileStorage() {
|
|
|
|
|
try {
|
|
|
|
|
let files = await RNFetchBlob.fs.ls(cacheDir);
|
2023-09-21 13:18:12 +05:00
|
|
|
let oldCache = await RNFetchBlob.fs.ls(cacheDirOld);
|
|
|
|
|
|
2022-02-28 13:48:59 +05:00
|
|
|
for (let file of files) {
|
2023-09-21 13:18:12 +05:00
|
|
|
await RNFetchBlob.fs.unlink(cacheDir + `/${file}`).catch(console.log);
|
|
|
|
|
}
|
|
|
|
|
for (let file of oldCache) {
|
|
|
|
|
await RNFetchBlob.fs.unlink(cacheDirOld + `/${file}`).catch(console.log);
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.log("clearFileStorage", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function createCacheDir() {
|
|
|
|
|
if (!(await RNFetchBlob.fs.exists(cacheDir))) {
|
|
|
|
|
await RNFetchBlob.fs.mkdir(cacheDir);
|
2024-05-04 00:31:02 +05:00
|
|
|
DatabaseLogger.log("Cache directory created");
|
2023-09-21 13:18:12 +05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function migrateFilesFromCache() {
|
|
|
|
|
try {
|
|
|
|
|
await createCacheDir();
|
|
|
|
|
const migratedFilesPath = cacheDir + "/.migrated_1";
|
|
|
|
|
const migrated = await RNFetchBlob.fs.exists(migratedFilesPath);
|
2023-09-22 14:47:04 +05:00
|
|
|
if (migrated) {
|
|
|
|
|
console.log("Files migrated already");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-09-21 13:18:12 +05:00
|
|
|
|
|
|
|
|
let files = await RNFetchBlob.fs.ls(cacheDir);
|
|
|
|
|
console.log("Files to migrate:", files.join(","));
|
|
|
|
|
|
|
|
|
|
let oldCache = await RNFetchBlob.fs.ls(cacheDirOld);
|
|
|
|
|
for (let file of oldCache) {
|
|
|
|
|
if (file.startsWith("org.") || file.startsWith("com.")) continue;
|
|
|
|
|
RNFetchBlob.fs
|
|
|
|
|
.mv(cacheDirOld + `/${file}`, cacheDir + `/${file}`)
|
|
|
|
|
.catch(console.log);
|
|
|
|
|
console.log("Moved", file);
|
2022-02-28 13:48:59 +05:00
|
|
|
}
|
2023-09-21 13:18:12 +05:00
|
|
|
await RNFetchBlob.fs.createFile(migratedFilesPath, "1", "utf8");
|
2022-02-28 13:48:59 +05:00
|
|
|
} catch (e) {
|
2023-09-21 13:18:12 +05:00
|
|
|
console.log("migrateFilesFromCache", e);
|
2022-02-28 13:48:59 +05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-31 15:44:18 +05:00
|
|
|
export async function clearCache() {
|
2024-08-07 11:17:05 +05:00
|
|
|
await RNFetchBlob.fs.unlink(cacheDir).catch(console.log);
|
|
|
|
|
await createCacheDir();
|
|
|
|
|
eSendEvent("cache-cleared");
|
2024-07-31 15:44:18 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function deleteCacheFileByPath(path) {
|
|
|
|
|
await RNFetchBlob.fs.unlink(path).catch(console.log);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function deleteCacheFileByName(name) {
|
|
|
|
|
await RNFetchBlob.fs.unlink(`${cacheDir}/${name}`).catch(console.log);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function deleteDCacheFiles() {
|
|
|
|
|
const files = await RNFetchBlob.fs.ls(cacheDir);
|
|
|
|
|
for (const file of files) {
|
|
|
|
|
if (file.includes("_dcache")) {
|
|
|
|
|
await RNFetchBlob.fs.unlink(file).catch(console.log);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-28 13:48:59 +05:00
|
|
|
export async function exists(filename) {
|
2023-09-21 11:47:35 +05:00
|
|
|
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) {
|
2023-12-27 09:40:15 +05:00
|
|
|
const attachment = await db.attachments.attachment(filename);
|
|
|
|
|
const totalChunks = Math.ceil(attachment.size / attachment.chunkSize);
|
2023-09-21 11:47:35 +05:00
|
|
|
const totalAbytes = totalChunks * ABYTES;
|
2023-12-27 09:40:15 +05:00
|
|
|
const expectedFileSize = attachment.size + totalAbytes;
|
2023-09-21 11:47:35 +05:00
|
|
|
|
|
|
|
|
const stat = await RNFetchBlob.fs.stat(
|
|
|
|
|
existsInAppGroup ? appGroupPath : path
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (stat.size !== expectedFileSize) {
|
2024-05-04 00:31:02 +05:00
|
|
|
DatabaseLogger.log(
|
|
|
|
|
`File size mismatch: ${filename}, expected: ${expectedFileSize}, actual: ${stat.size}`
|
|
|
|
|
);
|
2023-09-21 11:47:35 +05:00
|
|
|
RNFetchBlob.fs
|
|
|
|
|
.unlink(existsInAppGroup ? appGroupPath : path)
|
|
|
|
|
.catch(console.log);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
exists = true;
|
|
|
|
|
}
|
2022-02-28 13:48:59 +05:00
|
|
|
return exists;
|
|
|
|
|
}
|
2024-08-07 11:17:05 +05:00
|
|
|
|
|
|
|
|
export async function bulkExists(files) {
|
|
|
|
|
const cacheFiles = await RNFetchBlob.fs.ls(cacheDir);
|
|
|
|
|
let missingFiles = files.filter((file) => !cacheFiles.includes(file));
|
|
|
|
|
|
|
|
|
|
if (Platform.OS === "ios") {
|
|
|
|
|
const iosAppGroup =
|
|
|
|
|
Platform.OS === "ios"
|
|
|
|
|
? await RNFetchBlob.fs.pathForAppGroup(IOS_APPGROUPID)
|
|
|
|
|
: null;
|
|
|
|
|
const appGroupFiles = await RNFetchBlob.fs.ls(iosAppGroup);
|
|
|
|
|
missingFiles = missingFiles.filter((file) => !appGroupFiles.includes(file));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return missingFiles;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getCacheSize() {
|
|
|
|
|
const stat = await RNFetchBlob.fs.lstat(`file://` + cacheDir);
|
|
|
|
|
let total = 0;
|
|
|
|
|
console.log("Total files", stat.length);
|
|
|
|
|
stat.forEach((s) => {
|
|
|
|
|
total += parseInt(s.size);
|
|
|
|
|
});
|
|
|
|
|
return total;
|
|
|
|
|
}
|