diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2025-05-26 17:05:36 +0200 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2025-05-26 17:05:36 +0200 |
| commit | fa0fa1fcb494483020a736a2bc2c5c639a2bd870 (patch) | |
| tree | 909db148c91e9d9650b2f5d8eb1dee04ea8cae1b /web/storage.cpp | |
| parent | 87852933851674cacbc8b2295d046831db27ab81 (diff) | |
| download | nissy-core-fa0fa1fcb494483020a736a2bc2c5c639a2bd870.tar.gz nissy-core-fa0fa1fcb494483020a736a2bc2c5c639a2bd870.zip | |
Progress with web version
Diffstat (limited to 'web/storage.cpp')
| -rw-r--r-- | web/storage.cpp | 35 |
1 files changed, 32 insertions, 3 deletions
diff --git a/web/storage.cpp b/web/storage.cpp index 4d0959f..cbf9e9c 100644 --- a/web/storage.cpp +++ b/web/storage.cpp | |||
| @@ -13,13 +13,12 @@ std::string getprefix() { | |||
| 13 | } | 13 | } |
| 14 | 14 | ||
| 15 | EM_ASYNC_JS(int, loadfs, (), { | 15 | EM_ASYNC_JS(int, loadfs, (), { |
| 16 | const dir = '/tables'; | ||
| 17 | const inBrowser = typeof window !== 'undefined'; | 16 | const inBrowser = typeof window !== 'undefined'; |
| 18 | const inWorker = typeof WorkerGlobalScope !== 'undefined' && | 17 | const inWorker = typeof WorkerGlobalScope !== 'undefined' && |
| 19 | self instanceof WorkerGlobalScope; | 18 | self instanceof WorkerGlobalScope; |
| 19 | console.assert(inBrowser || inWorker, "Non-browsers not supported"); | ||
| 20 | 20 | ||
| 21 | if (!(inBrowser || inWorker)) | 21 | const dir = '/tables'; |
| 22 | return; | ||
| 23 | 22 | ||
| 24 | if (!FS.analyzePath(dir).exists) | 23 | if (!FS.analyzePath(dir).exists) |
| 25 | FS.mkdir(dir); | 24 | FS.mkdir(dir); |
| @@ -38,6 +37,30 @@ EM_ASYNC_JS(int, loadfs, (), { | |||
| 38 | } | 37 | } |
| 39 | }); | 38 | }); |
| 40 | 39 | ||
| 40 | EM_ASYNC_JS(int, download_and_store, (const char *key, const char *url), { | ||
| 41 | const inBrowser = typeof window !== 'undefined'; | ||
| 42 | const inWorker = typeof WorkerGlobalScope !== 'undefined' && | ||
| 43 | self instanceof WorkerGlobalScope; | ||
| 44 | console.assert(inBrowser || inWorker, "Non-browsers not supported"); | ||
| 45 | |||
| 46 | url = UTF8ToString(url); | ||
| 47 | key = UTF8ToString(key); | ||
| 48 | let response = await fetch(url); | ||
| 49 | if (!response.ok) { | ||
| 50 | console.log("Error downloading data for " + key); | ||
| 51 | console.log("" + response.status + ": " + response.statusText); | ||
| 52 | return 0; | ||
| 53 | } | ||
| 54 | |||
| 55 | console.log("Data for " + key + " downloaded, writing to storage..."); | ||
| 56 | let data = await response.bytes(); | ||
| 57 | var stream = FS.open("/tables/" + key, "w+"); | ||
| 58 | FS.write(stream, data, 0, data.length, 0); | ||
| 59 | FS.close(stream); | ||
| 60 | console.log("Data for " + key + " stored (" + data.length + " bytes)"); | ||
| 61 | return 1; | ||
| 62 | }); | ||
| 63 | |||
| 41 | bool storage::read(std::string key, size_t data_size, char *data) | 64 | bool storage::read(std::string key, size_t data_size, char *data) |
| 42 | { | 65 | { |
| 43 | loadfs(); | 66 | loadfs(); |
| @@ -65,3 +88,9 @@ bool storage::write(std::string key, size_t data_size, const char *data) | |||
| 65 | 88 | ||
| 66 | return !ofs.fail(); | 89 | return !ofs.fail(); |
| 67 | } | 90 | } |
| 91 | |||
| 92 | int storage::download(std::string key, std::string url) | ||
| 93 | { | ||
| 94 | loadfs(); | ||
| 95 | return download_and_store(key.c_str(), url.c_str()); | ||
| 96 | } | ||
