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-08-26 16:19:39 +05:00
|
|
|
import { Platform } from "react-native";
|
|
|
|
|
import RNFetchBlob from "rn-fetch-blob";
|
2022-02-28 13:48:59 +05:00
|
|
|
import {
|
|
|
|
|
decrypt,
|
|
|
|
|
deriveCryptoKey,
|
|
|
|
|
encrypt,
|
|
|
|
|
generateCryptoKey,
|
|
|
|
|
getCryptoKey,
|
|
|
|
|
getRandomBytes,
|
|
|
|
|
hash,
|
|
|
|
|
removeCryptoKey
|
2022-08-26 16:19:39 +05:00
|
|
|
} from "./encryption";
|
|
|
|
|
import { MMKV } from "./mmkv";
|
2022-02-28 13:48:59 +05:00
|
|
|
|
2022-07-19 13:36:45 +05:00
|
|
|
export class KV {
|
|
|
|
|
storage = null;
|
|
|
|
|
constructor(storage) {
|
|
|
|
|
this.storage = storage;
|
|
|
|
|
}
|
|
|
|
|
async read(key) {
|
|
|
|
|
if (!key) return null;
|
|
|
|
|
let data = this.storage.getString(key);
|
|
|
|
|
if (!data) return null;
|
|
|
|
|
try {
|
|
|
|
|
let parse = JSON.parse(data);
|
|
|
|
|
return parse;
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return data;
|
|
|
|
|
}
|
2022-02-28 13:48:59 +05:00
|
|
|
}
|
|
|
|
|
|
2022-07-19 13:36:45 +05:00
|
|
|
async write(key, data) {
|
2022-08-26 16:19:39 +05:00
|
|
|
this.storage.setString(
|
|
|
|
|
key,
|
|
|
|
|
typeof data === "string" ? data : JSON.stringify(data)
|
|
|
|
|
);
|
2022-07-19 13:36:45 +05:00
|
|
|
return true;
|
|
|
|
|
}
|
2022-02-28 13:48:59 +05:00
|
|
|
|
2022-07-19 13:36:45 +05:00
|
|
|
async readMulti(keys) {
|
|
|
|
|
if (keys.length <= 0) {
|
|
|
|
|
return [];
|
|
|
|
|
} else {
|
|
|
|
|
let data = await this.storage.getMultipleItemsAsync(keys.slice());
|
2022-02-28 13:48:59 +05:00
|
|
|
|
2022-07-19 13:36:45 +05:00
|
|
|
return data.map(([key, value]) => {
|
|
|
|
|
let obj;
|
|
|
|
|
try {
|
|
|
|
|
obj = JSON.parse(value);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
obj = value;
|
|
|
|
|
}
|
2022-02-28 13:48:59 +05:00
|
|
|
|
2022-07-19 13:36:45 +05:00
|
|
|
return [key, obj];
|
|
|
|
|
});
|
|
|
|
|
}
|
2022-02-28 13:48:59 +05:00
|
|
|
}
|
|
|
|
|
|
2022-07-19 13:36:45 +05:00
|
|
|
async remove(key) {
|
|
|
|
|
return await this.storage.removeItem(key);
|
|
|
|
|
}
|
2022-02-28 13:48:59 +05:00
|
|
|
|
2022-07-19 13:36:45 +05:00
|
|
|
async clear() {
|
|
|
|
|
return await this.storage.clearStore();
|
|
|
|
|
}
|
2022-02-28 13:48:59 +05:00
|
|
|
|
2022-07-19 13:36:45 +05:00
|
|
|
async getAllKeys() {
|
|
|
|
|
let keys = (await this.storage.indexer.getKeys()) || [];
|
|
|
|
|
keys = keys.filter(
|
2022-08-26 16:19:39 +05:00
|
|
|
(k) =>
|
|
|
|
|
k !== "stringIndex" &&
|
|
|
|
|
k !== "boolIndex" &&
|
|
|
|
|
k !== "mapIndex" &&
|
|
|
|
|
k !== "arrayIndex" &&
|
|
|
|
|
k !== "numberIndex" &&
|
2022-07-19 13:36:45 +05:00
|
|
|
k !== this.storage.instanceID
|
|
|
|
|
);
|
|
|
|
|
return keys;
|
|
|
|
|
}
|
2022-02-28 13:48:59 +05:00
|
|
|
}
|
|
|
|
|
|
2022-07-19 13:36:45 +05:00
|
|
|
const DefaultStorage = new KV(MMKV);
|
|
|
|
|
|
2022-02-28 13:48:59 +05:00
|
|
|
async function requestPermission() {
|
2022-08-26 16:19:39 +05:00
|
|
|
if (Platform.OS === "ios") return true;
|
2022-02-28 13:48:59 +05:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
async function checkAndCreateDir(path) {
|
|
|
|
|
let dir =
|
2022-08-26 16:19:39 +05:00
|
|
|
Platform.OS === "ios"
|
2022-02-28 13:48:59 +05:00
|
|
|
? RNFetchBlob.fs.dirs.DocumentDir + path
|
2022-08-26 16:19:39 +05:00
|
|
|
: RNFetchBlob.fs.dirs.SDCardDir + "/Notesnook/" + path;
|
2022-02-28 13:48:59 +05:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
let exists = await RNFetchBlob.fs.exists(dir);
|
|
|
|
|
let isDir = await RNFetchBlob.fs.isDir(dir);
|
|
|
|
|
if (!exists || !isDir) {
|
|
|
|
|
await RNFetchBlob.fs.mkdir(dir);
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
await RNFetchBlob.fs.mkdir(dir);
|
|
|
|
|
}
|
|
|
|
|
return dir;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default {
|
2022-08-26 16:19:39 +05:00
|
|
|
read: (key) => DefaultStorage.read(key),
|
2022-07-19 13:36:45 +05:00
|
|
|
write: (key, value) => DefaultStorage.write(key, value),
|
2022-08-26 16:19:39 +05:00
|
|
|
readMulti: (keys) => DefaultStorage.readMulti(keys),
|
|
|
|
|
remove: (key) => DefaultStorage.remove(key),
|
2022-07-19 13:36:45 +05:00
|
|
|
clear: () => DefaultStorage.clear(),
|
|
|
|
|
getAllKeys: () => DefaultStorage.getAllKeys(),
|
2022-02-28 13:48:59 +05:00
|
|
|
encrypt,
|
|
|
|
|
decrypt,
|
|
|
|
|
getRandomBytes,
|
|
|
|
|
checkAndCreateDir,
|
|
|
|
|
requestPermission,
|
|
|
|
|
deriveCryptoKey,
|
|
|
|
|
getCryptoKey,
|
|
|
|
|
removeCryptoKey,
|
|
|
|
|
hash,
|
|
|
|
|
generateCryptoKey
|
|
|
|
|
};
|