Files
notesnook/apps/mobile/app/common/filesystem/io.js

253 lines
7.1 KiB
JavaScript
Raw Normal View History

/*
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
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";
import { eSendEvent } from "../../services/event-manager";
import { IOS_APPGROUPID } from "../../utils/constants";
2024-05-04 00:31:02 +05:00
import { DatabaseLogger, db } from "../database";
2024-03-01 20:28:21 +05:00
import { cacheDir, cacheDirOld, getRandomId } from "./utils";
2022-02-28 13:48:59 +05:00
export async function readEncrypted(filename, key, cipherData) {
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 {
if (!(await exists(filename))) {
2022-02-28 13:48:59 +05:00
return false;
}
2022-02-28 13:48:59 +05:00
let output = await Sodium.decryptFile(
key,
{
...cipherData,
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...");
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;
}
}
export async function hashBase64(data) {
2023-03-22 12:41:33 +05:00
const hash = await Sodium.hashFile({
type: "base64",
data,
uri: ""
});
return {
2023-03-22 12:41:33 +05:00
hash: hash,
type: "xxh64"
};
}
export async function writeEncryptedBase64(data, key) {
await createCacheDir();
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, {
uri: Platform.OS === "ios" ? filepath : "file://" + filepath,
type: "url"
2022-02-28 13:48:59 +05:00
});
RNFetchBlob.fs.unlink(filepath).catch(console.log);
console.log("encrypted file output: ", output);
output.size = output.length;
delete output.length;
2022-02-28 13:48:59 +05:00
return {
...output,
alg: "xcha-stream"
2022-02-28 13:48:59 +05:00
};
}
export async function deleteFile(filename, data) {
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 {
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) {
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);
let oldCache = await RNFetchBlob.fs.ls(cacheDirOld);
2022-02-28 13:48:59 +05:00
for (let file of files) {
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");
}
}
export async function migrateFilesFromCache() {
try {
await createCacheDir();
const migratedFilesPath = cacheDir + "/.migrated_1";
const migrated = await RNFetchBlob.fs.exists(migratedFilesPath);
if (migrated) {
console.log("Files migrated already");
return;
}
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
}
await RNFetchBlob.fs.createFile(migratedFilesPath, "1", "utf8");
2022-02-28 13:48:59 +05:00
} catch (e) {
console.log("migrateFilesFromCache", e);
2022-02-28 13:48:59 +05:00
}
}
export async function clearCache() {
await RNFetchBlob.fs.unlink(cacheDir).catch(console.log);
await createCacheDir();
eSendEvent("cache-cleared");
}
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);
}
}
}
const ABYTES = 17;
2022-02-28 13:48:59 +05:00
export async function exists(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) {
2023-12-27 09:40:15 +05:00
const attachment = await db.attachments.attachment(filename);
const totalChunks = Math.ceil(attachment.size / attachment.chunkSize);
const totalAbytes = totalChunks * ABYTES;
2023-12-27 09:40:15 +05:00
const expectedFileSize = attachment.size + totalAbytes;
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}`
);
RNFetchBlob.fs
.unlink(existsInAppGroup ? appGroupPath : path)
.catch(console.log);
return false;
}
exists = true;
}
2022-02-28 13:48:59 +05:00
return exists;
}
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;
}