Skip to content

Commit

Permalink
s3 update
Browse files Browse the repository at this point in the history
  • Loading branch information
itcon-pty-au committed Sep 4, 2024
1 parent b900506 commit a74a12f
Showing 1 changed file with 7 additions and 23 deletions.
30 changes: 7 additions & 23 deletions s3.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ function openSyncModal() {

const data = await exportBackupData();
const dataStr = JSON.stringify(data);
const dataFileName = 'typingmind-backup.json'; // Static file name for export
const dataFileName = 'typingmind-backup.json';

const s3 = new AWS.S3();
const uploadParams = {
Expand Down Expand Up @@ -157,15 +157,14 @@ function openSyncModal() {
AWS.config.update({
accessKeyId: awsAccessKey,
secretAccessKey: awsSecretKey,
region: 'ap-southeast-2' // You can change this to your desired region
region: 'ap-southeast-2'
});

const s3 = new AWS.S3();
const params = {
Bucket: bucketName,
Key: 'typingmind-backup.json' // Static file name for import
Key: 'typingmind-backup.json'
};

// Fetch the data from S3
s3.getObject(params, function (err, data) {
const actionMsgElement = document.getElementById('action-msg');
Expand All @@ -174,11 +173,9 @@ function openSyncModal() {
actionMsgElement.style.color = 'red';
return;
}

// Parse the data and store it back to localStorage and IndexedDB
const importedData = JSON.parse(data.Body.toString('utf-8'));
importDataToStorage(importedData);

actionMsgElement.textContent = `Import successful!`;
actionMsgElement.style.color = 'green';
//modalPopup.remove(); // Close modal after import
Expand All @@ -200,38 +197,28 @@ async function loadAwsSdk() {
});
}

// Function to import data to localStorage and IndexedDB
function importDataToStorage(data) {
console.log("Imported data", data);

// Import to localStorage
// Import the localStorage records
Object.keys(data.localStorage).forEach(key => {
localStorage.setItem(key, data.localStorage[key]);
});

// Open the IndexedDB
const request = indexedDB.open("keyval-store");

request.onsuccess = function (event) {
const db = event.target.result;
const transaction = db.transaction(["keyval"], "readwrite");

const objectStore = transaction.objectStore("keyval");

// Process each imported record
Object.keys(data).forEach(key => {
// Import the IndexedDB records
Object.keys(data.indexedDB.keyval).forEach(key => {
objectStore.put(data[key], key);
});

transaction.oncomplete = () => {
console.log("All records imported successfully!");
};

transaction.onerror = (e) => {
console.error("Error during import transaction:", e.target.error);
};
};

request.onerror = function (event) {
console.error("Error opening IndexedDB:", event.target.error);
};
Expand All @@ -256,7 +243,6 @@ function exportIndexedDB() {
return new Promise((resolve) => {
const data = {};
const request = indexedDB.open("keyval-store");

request.onsuccess = function (event) {
const db = event.target.result;
const transaction = db.transaction(db.objectStoreNames, "readonly");
Expand All @@ -266,17 +252,15 @@ function exportIndexedDB() {

allRecords.onsuccess = function (event) {
event.target.result.forEach(record => {
data[record.key] = record; // Store each record separately using its own key
data[record.key] = record;
});
resolve(data);
};

allRecords.onerror = function (event) {
console.error("Error fetching records from object store:", event.target.error);
resolve({});
};
};

request.onerror = function (event) {
console.error("IndexedDB error:", event.target.error);
resolve({});
Expand Down

0 comments on commit a74a12f

Please sign in to comment.