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

190 lines
5.5 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
import { Platform } from "react-native";
2022-11-28 18:47:41 +05:00
import Sodium from "@ammarahmed/react-native-sodium";
2023-05-19 11:05:16 +05:00
import RNFetchBlob from "react-native-blob-util";
import { cacheDir, cacheDirOld, getRandomId } from "./utils";
import { db } from "../database";
import { compressToBase64 } from "./compress";
import { IOS_APPGROUPID } from "../../utils/constants";
2022-02-28 13:48:59 +05:00
export async function readEncrypted(filename, key, cipherData) {
await migrateFilesFromCache();
console.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 {
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)));
2022-02-28 13:48:59 +05:00
if (!exists) {
2023-09-20 19:45:57 +05:00
console.log("Will download file...", filename);
2022-02-28 13:48:59 +05:00
return false;
} else {
RNFetchBlob.fs.stat(path).then((r) => {
console.log(r);
});
2022-02-28 13:48:59 +05:00
}
const attachment = db.attachments.attachment(filename);
const isPng = /(png)/g.test(attachment?.metadata.type);
const isJpeg = /(jpeg|jpg)/g.test(attachment?.metadata.type);
console.log("decrypting....");
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
},
cipherData.outputType === "base64"
? isPng || isJpeg
? "cache"
: "base64"
: "text"
2022-02-28 13:48:59 +05:00
);
console.log("file decrypted...");
if (cipherData.outputType === "base64" && (isPng || isJpeg)) {
const dCachePath = `${cacheDir}/${output}`;
output = await compressToBase64(
`file://${dCachePath}`,
isPng ? "PNG" : "JPEG"
);
}
2022-02-28 13:48:59 +05:00
return output;
} catch (e) {
RNFetchBlob.fs.unlink(path).catch(console.log);
console.log("readEncrypted", 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);
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);
console.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) 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 exists(filename) {
let exists = await RNFetchBlob.fs.exists(`${cacheDir}/${filename}`);
return exists;
}