Skip to content

Commit

Permalink
Restore old database files
Browse files Browse the repository at this point in the history
  • Loading branch information
Namaneo committed Aug 9, 2023
1 parent 619d717 commit f9fe90c
Show file tree
Hide file tree
Showing 2 changed files with 173 additions and 0 deletions.
13 changes: 13 additions & 0 deletions ui/sources/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { HomePage } from './pages/home-page';
import { InstallPage } from './pages/install-page';
import { SavesPage } from './pages/saves-page';
import { CheatsPage } from './pages/cheats-page';
import Database from './services/database';
import Files from './services/files';

/* Core CSS required for Ionic components to work properly */
import '@ionic/react/css/core.css';
Expand Down Expand Up @@ -95,4 +97,15 @@ function Junie() {
);
}

try {
const paths = await Database.list();
for (const path of paths) {
const buffer = await Database.read(path);
await Files.write(path, buffer);
await Database.remove(path);
}
} catch (e) {
console.error(e);
}

createRoot(document.getElementById('root')).render(<Junie />);
160 changes: 160 additions & 0 deletions ui/sources/services/database.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
export default class Database {
/** @type {IDBDatabase} */
static #db = null;

/** @type {string} */
static get #name() { return 'Junie'; };

/** @type {string} */
static get #store() { return 'FILE_DATA'; };

/**
* @param {IDBDatabase} db
* @returns {void}
*/
static #upgrade(db) {
db.createObjectStore(this.#store, { keyPath: 'name' });
}

/**
* @returns {Promise<number>}
*/
static async #version() {
return new Promise((resolve, reject) => {
const request = indexedDB.open(this.#name);

request.onupgradeneeded = (event) => this.#upgrade(event.target.result);
request.onerror = (event) => reject(event.target.error);
request.onsuccess = (event) => {
resolve(event.target.result.version);
event.target.result.close();
}
});
}

/**
* @param {string} type
* @returns {Promise<IDBObjectStore>}
*/
static async #get(type) {
const transaction = (db) => db
.transaction([this.#store], type)
.objectStore(this.#store);

if (await this.#version() != 1)
indexedDB.deleteDatabase(this.#name);

return new Promise((resolve, reject) => {
if (this.#db)
return resolve(transaction(this.#db));

const request = indexedDB.open(this.#name);

request.onupgradeneeded = (event) => this.#upgrade(event.target.result);
request.onerror = (event) => reject(event.target.error);
request.onsuccess = (event) => {
this.#db = event.target.result;
resolve(transaction(this.#db));
};
});
}

/**
* @param {String} path
* @returns {Promise<string[]>}
*/
static async list(path) {
const store = await this.#get('readonly');

return new Promise((resolve, reject) => {
const request = store.getAllKeys();

request.onerror = (event) => reject(event.target.error);
request.onsuccess = (event) => resolve(event.target.result.filter(x => !path || x.startsWith(path)));
});
}

/**
* @param {String} path
* @returns {Promise<File>}
*/
static async file(path) {
const store = await this.#get('readonly');

return new Promise((resolve, reject) => {
const request = store.get(path);

request.onerror = (event) => reject(event.target.error);
request.onsuccess = (event) => resolve(event.target.result);
});
}

/**
* @param {String} path
* @returns {Promise<Uint8Array>}
*/
static async read(path) {
const file = await this.file(path);
if (!file) return null;

const reader = file.stream().getReader();
const buffer = new Uint8Array(file.size);

let offset = 0
return reader.read().then(function process({ done, value }) {
if (done)
return buffer;

buffer.set(value, offset);
offset += value.length;

return reader.read().then(process);
});
}

/**
* @param {File} file
* @returns {Promise<void>}
*/
static async add(file) {
const store = await this.#get('readwrite');

return new Promise((resolve, reject) => {
const request = store.put(file);

request.onerror = (event) => reject(event.target.error);
request.onsuccess = () => resolve();
});
}

/**
* @param {String} path
* @param {Uint8Array} data
* @returns {Promise<void>}
*/
static async write(path, data) {
const store = await this.#get('readwrite');

return new Promise((resolve, reject) => {
const request = store.put(new File([data], path));

request.onerror = (event) => reject(event.target.error);
request.onsuccess = () => resolve();
});
}

/**
* @param {String} path
* @returns {Promise<void>}
*/
static async remove(path) {
const store = await this.#get('readwrite');

return new Promise((resolve, reject) => {
const request = store.delete(path);

request.onerror = (event) => reject(event.target.error);
request.onsuccess = () => resolve();
});
}
}

0 comments on commit f9fe90c

Please sign in to comment.