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

110 lines
3.0 KiB
JavaScript
Raw Normal View History

2022-08-30 16:13:11 +05:00
/* This file is part of the Notesnook project (https://notesnook.com/)
*
* Copyright (C) 2022 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/>.
*/
import { Platform } from "react-native";
import Sodium from "react-native-sodium";
import RNFetchBlob from "rn-fetch-blob";
import { cacheDir, getRandomId } from "./utils";
2022-02-28 13:48:59 +05:00
export async function readEncrypted(filename, key, cipherData) {
let path = `${cacheDir}/${filename}`;
try {
let exists = await RNFetchBlob.fs.exists(path);
if (!exists) {
return false;
}
let output = await Sodium.decryptFile(
key,
{
...cipherData,
hash: filename
},
true
);
console.log("output length: ", output?.length);
2022-02-28 13:48:59 +05:00
return output;
} catch (e) {
RNFetchBlob.fs.unlink(path).catch(console.log);
console.log(e);
console.log("error");
2022-02-28 13:48:59 +05:00
return false;
}
}
export async function writeEncrypted(filename, { data, type, key }) {
console.log("file input: ", { type, key });
let filepath = cacheDir + `/${getRandomId("imagecache_")}`;
2022-02-28 13:48:59 +05:00
console.log(filepath);
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) {
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);
for (let file of files) {
try {
await RNFetchBlob.fs.unlink(cacheDir + `/${file}`);
} catch (e) {
console.log(e);
}
}
} catch (e) {
console.log(e);
}
}
export async function exists(filename) {
let exists = await RNFetchBlob.fs.exists(`${cacheDir}/${filename}`);
return exists;
}