diff --git a/SqliteWasmHelper/BrowserCache.cs b/SqliteWasmHelper/BrowserCache.cs index 3cce255..cd9228e 100644 --- a/SqliteWasmHelper/BrowserCache.cs +++ b/SqliteWasmHelper/BrowserCache.cs @@ -48,6 +48,18 @@ public async Task SyncDbWithCacheAsync(string filename) return await module.InvokeAsync("synchronizeDbWithCache", filename); } + /// + /// Calls the code to restore the database from a given ArrayBuffer. + /// + /// The ArrayBuffer containing the database file. + /// The name of the file to process. + /// 0 if successful, -1 otherwise. + public async Task ManualRestore(byte[] arrayBuffer, string filename) + { + var module = await moduleTask.Value; + return await module.InvokeAsync("manualRestore", arrayBuffer, filename); + } + /// /// Creates an anchor tag to download the database and injects it into the parent. /// diff --git a/SqliteWasmHelper/IBrowserCache.cs b/SqliteWasmHelper/IBrowserCache.cs index 21ea2a3..d39d39b 100644 --- a/SqliteWasmHelper/IBrowserCache.cs +++ b/SqliteWasmHelper/IBrowserCache.cs @@ -18,6 +18,14 @@ public interface IBrowserCache /// Either -1 (no cache), 0 (restored), or 1 (cached). Task SyncDbWithCacheAsync(string filename); + /// + /// Calls the code to restore the database from a given ArrayBuffer. + /// + /// The ArrayBuffer containing the database file. + /// The name of the file to process. + /// 0 if successful, -1 otherwise. + Task ManualRestore(byte[] arrayBuffer, string filename); + /// /// Creates an anchor tag to download the database and injects it into the parent. /// diff --git a/SqliteWasmHelper/ISqliteWasmDbContextFactory.cs b/SqliteWasmHelper/ISqliteWasmDbContextFactory.cs index e23ada2..1f820e4 100644 --- a/SqliteWasmHelper/ISqliteWasmDbContextFactory.cs +++ b/SqliteWasmHelper/ISqliteWasmDbContextFactory.cs @@ -18,5 +18,12 @@ public interface ISqliteWasmDbContextFactory /// /// The new context. Task CreateDbContextAsync(); + + /// + /// Calls the code to restore the database from a given ArrayBuffer. + /// + /// The ArrayBuffer containing the database file. + /// 0 if successful, -1 otherwise. + Task ManualRestore(byte[] arrayBuffer); } } diff --git a/SqliteWasmHelper/SqliteWasmDbContextFactory.cs b/SqliteWasmHelper/SqliteWasmDbContextFactory.cs index 860dbb6..80b7b15 100644 --- a/SqliteWasmHelper/SqliteWasmDbContextFactory.cs +++ b/SqliteWasmHelper/SqliteWasmDbContextFactory.cs @@ -99,6 +99,23 @@ public async Task CreateDbContextAsync() return ctx; } + /// + /// Calls the code to restore the database from a given ArrayBuffer. + /// + /// The ArrayBuffer containing the database file. + /// 0 if successful, -1 otherwise. + public async Task ManualRestore(byte[] arrayBuffer) + { + var filename = $"{GetFilename()}_bak"; + int restoreStatus = await cache.ManualRestore(arrayBuffer, filename); + if (restoreStatus == 0) + { + DoSwap(filename, FileNames[typeof(TContext)]); + } + + return restoreStatus; + } + private void DoSwap(string source, string target) => swap.DoSwap(source, target); diff --git a/SqliteWasmHelper/wwwroot/browserCache.js b/SqliteWasmHelper/wwwroot/browserCache.js index 969c64f..2f93bb5 100644 --- a/SqliteWasmHelper/wwwroot/browserCache.js +++ b/SqliteWasmHelper/wwwroot/browserCache.js @@ -86,3 +86,40 @@ export async function generateDownloadLink(parent, file) { return false; } + +export async function manualRestore(arrayBuffer, file) { + window.sqlitedb = window.sqlitedb || { + init: false, + cache: await caches.open('SqliteWasmHelper') + }; + + const db = window.sqlitedb; + + const backupPath = `/${file}`; + const cachePath = `/data/cache/${file.substring(0, file.indexOf('_bak'))}`; + + if (arrayBuffer) { + console.log(`Restoring ${arrayBuffer.byteLength} bytes.`); + window.Module.FS.writeFile(backupPath, new Uint8Array(arrayBuffer)); + + const blob = new Blob([arrayBuffer], { + type: 'application/octet-stream', + ok: true, + status: 200 + }); + + const headers = new Headers({ + 'content-length': blob.size + }); + + const response = new Response(blob, { + headers + }); + + await db.cache.put(cachePath, response); + + return 0; + } + + return -1; +} diff --git a/SqliteWasmTests/TestHelpers/MockBrowserCache.cs b/SqliteWasmTests/TestHelpers/MockBrowserCache.cs index 8b0ae0d..7e0eee0 100644 --- a/SqliteWasmTests/TestHelpers/MockBrowserCache.cs +++ b/SqliteWasmTests/TestHelpers/MockBrowserCache.cs @@ -52,5 +52,10 @@ public Task SyncDbWithCacheAsync(string filename) } return Task.FromResult(SyncDbResult); } + + public Task ManualRestore(byte[] arrayBuffer, string filename) + { + throw new System.NotImplementedException(); + } } }