Files
inji-wallet/shared/fileStorage.ts
PoojaBabusing d778b94403 [INJI-628 & INJI-630]: Local backup creation (#1160)
* [INJI-630]: add backup machine and screens for data backup

Signed-off-by: Pooja Babusingh <68894211+PoojaBabusingh@users.noreply.github.com>

* [INJI-630]: add argon2 configs for hashing salt, password and phoneNumber

Signed-off-by: Pooja Babusingh <68894211+PoojaBabusingh@users.noreply.github.com>

* [INJI-630]: add key encryption and store hashed encryption key

Signed-off-by: Pooja Babusingh <68894211+PoojaBabusingh@users.noreply.github.com>

* [INJI-630]: add toggle backup check to make modal invisible in ios

Signed-off-by: Pooja Babusingh <68894211+PoojaBabusingh@users.noreply.github.com>

* (INJI-630): update package-lock.json

Signed-off-by: Sreenadh S <32409698+sree96@users.noreply.github.com>

* [INJI-628]: restrict logic to double stringify JSON object before saving to DB

Signed-off-by: Alka <prasadalka1998@gmail.com>

* [INJI-628]: create local compressed backup file

Signed-off-by: Alka <prasadalka1998@gmail.com>

* [INJI-628]: move some functions to appropriate files

Signed-off-by: Alka <prasadalka1998@gmail.com>

* [INJI-628]: remove RN aes crypto library and use existing library to generate encryption key

Signed-off-by: Alka <prasadalka1998@gmail.com>

* [INJI-628]: update logic to check storage availability for backup creation

Signed-off-by: Alka <prasadalka1998@gmail.com>

* [INJI-628]: declare the variable and use then use it

Signed-off-by: Alka <prasadalka1998@gmail.com>

---------

Signed-off-by: Pooja Babusingh <68894211+PoojaBabusingh@users.noreply.github.com>
Signed-off-by: PoojaBabusing <115976560+PoojaBabusing@users.noreply.github.com>
Signed-off-by: Sreenadh S <32409698+sree96@users.noreply.github.com>
Signed-off-by: Alka <prasadalka1998@gmail.com>
Signed-off-by: Alka Prasad <Alka1703@users.noreply.github.com>
Co-authored-by: Pooja Babusingh <68894211+PoojaBabusingh@users.noreply.github.com>
Co-authored-by: Sreenadh S <32409698+sree96@users.noreply.github.com>
Co-authored-by: Alka <prasadalka1998@gmail.com>
Co-authored-by: Alka Prasad <Alka1703@users.noreply.github.com>
2024-01-19 18:24:55 +05:30

100 lines
2.4 KiB
TypeScript

import {
DocumentDirectoryPath,
exists,
mkdir,
readFile,
stat,
unlink,
writeFile,
readDir,
ReadDirItem,
} from 'react-native-fs';
interface CacheData {
data?: any;
count?: number;
}
interface Cache {
[key: string]: CacheData;
}
import * as RNZipArchive from 'react-native-zip-archive';
class FileStorage {
cache: Cache = {};
async readFile(path: string) {
return await readFile(path, 'utf8');
}
async getAllFilesInDirectory(path: string) {
return await readDir(path);
}
async writeFile(path: string, data: string) {
return await writeFile(path, data, 'utf8');
}
async createDirectory(path: string) {
return await mkdir(path);
}
async exists(path: string) {
return await exists(path);
}
async removeItem(path: string) {
return await unlink(path);
}
async getInfo(path: string) {
return await stat(path);
}
}
export default new FileStorage();
/**
* iOS: /var/mobile/Containers/Data/Application/196A05AD-6B11-403D-BA2D-6DC1F30075E1/Documents/inji/VC/<filename>
* android: /data/user/0/io.mosip.residentapp/files/inji/VC/<filename>
* These paths are coming from DocumentDirectoryPath in react-native-fs.
*/
export const vcDirectoryPath = `${DocumentDirectoryPath}/inji/VC`;
export const backupDirectoryPath = `${DocumentDirectoryPath}/inji/backup`;
export const zipFilePath = (filename: string) =>
`${DocumentDirectoryPath}/inji/backup/${filename}.zip`;
export const getFilePath = (key: string) => {
return `${vcDirectoryPath}/${key}.txt`;
};
export const getBackupFilePath = (key: string) => {
return `${backupDirectoryPath}/${key}.injibackup`;
};
export async function compressAndRemoveFile(fileName: string): Promise<string> {
const result = await compressFile(fileName);
await removeFile(fileName);
return result;
}
async function compressFile(fileName: string): Promise<string> {
return await RNZipArchive.zip(backupDirectoryPath, zipFilePath(fileName));
}
async function removeFile(fileName: string) {
await new FileStorage().removeItem(getBackupFilePath(fileName));
}
export async function getDirectorySize(path: string) {
const directorySize = await new FileStorage()
.getAllFilesInDirectory(path)
.then((result: ReadDirItem[]) => {
let folderEntriesSizeInBytes = 0;
result.forEach(fileItem => {
folderEntriesSizeInBytes += Number(fileItem.size);
});
return folderEntriesSizeInBytes;
});
return directorySize;
}