Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export/Import user generative data - alternative #935

Merged
merged 3 commits into from
Sep 28, 2023

Conversation

DivyaniPatel96
Copy link
Contributor

Work on story id: 172387

  1. export/import backup file support json format
  2. encrypt/decrypt json file

Work on story id: 172387
1. export/import backup file support json format
2. encrypt/decrypt json file
export const backupGDriveFileName = 'my.backup';
export const backupGDriveFileName = 'mybackup.json';
export const encryptionsKey = 'f06d3a055c7066de31d6d1ae583d7bd18d99840bc74d14aaed63860054004f15';
export const encryptionsIVKey = 'cd6854fab61ec032563b2e0c38b30c39';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not store keys on public repo, get it from configs.

@@ -183,16 +187,30 @@ const ChildSetup = ({ navigation }: Props): any => {
};
const importFromFile = async (): Promise<any> => {
if (Platform.OS == "android") {
Copy link
Collaborator

@Akhror Akhror Sep 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you refactor code, something like:

// Call the appropriate import function based on the platform

if (Platform.OS === 'android') {
    importDataAndroid();
} else {
    importDataNonAndroid();
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay @Akhror sure, I will refactor above code

const importedJsonData = JSON.parse(await decryptedData);
await RNFS.writeFile(tempRealmFile, JSON.stringify(decryptedData), "utf8");
oldChildrenData = importedJsonData;
} else {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we validate file types? How does app behave if user selects inappropriate file?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this app we don't have any validation for file type, but we are processing only .backup and .json file. If user select any other inappropriate file, bebbo app will not process further.

@@ -204,7 +222,9 @@ const ChildSetup = ({ navigation }: Props): any => {
navigation.navigate('ChildImportSetup', {
importResponse: JSON.stringify(oldChildrenData)
});
importedrealm.close();
if (dataset.name.endsWith('.backup')) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (importedrealm)
check is probably better choice.

return AesCrypto.encrypt(text, key, encryptionsIVKey, 'aes-256-cbc').then((cipher: any) => ({
cipher
}));
}
Copy link
Collaborator

@Akhror Akhror Sep 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good if you could write similar method for decrypt as well since we use it in more than one place.

async function decryptData(data: string): Promise<string> {
  try {
    const decryptedData = await AesCrypto.decrypt(
      data,
      encryptionsKey,
      encryptionsIVKey,
      'aes-256-cbc'
    );
    return decryptedData;
  } catch (error) {
    console.error('Decryption error', error);
    throw error;
  }
}

else {
setLoading(false);
setIsImportRunning(false);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's please introduce a new method to combine lines 218-238 and 280-301

Something like:

function handleImportedData(importedData: any) {
  if (importedData.length > 0) {
    userRealmCommon.openRealm()
      .then(() => userRealmCommon.deleteAllAtOnce())
      .then(() => {
        setLoading(false);
        setIsImportRunning(false);
        navigation.navigate('ChildImportSetup', {
          importResponse: JSON.stringify(importedData),
        });
      })
      .catch((error) => console.error('Error handling imported data:', error));

    if (tempRealmFile.endsWith('.backup')) {
      Realm.deleteFile({ path: tempRealmFile });
    }
  } else {
    setLoading(false);
    setIsImportRunning(false);
  }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay sure @Akhror , I will create this new methods

@DivyaniPatel96 DivyaniPatel96 merged commit 6d5d51f into main Sep 28, 2023
1 of 2 checks passed
@BhumikaDatamatics BhumikaDatamatics deleted the importExportData branch February 15, 2024 11:19
deepakmali-dgsl pushed a commit that referenced this pull request Aug 6, 2024
Export/Import user generative data - alternative
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants