|
1 |
| -let arcData = null; |
| 1 | +const STO_DATA = 'STO_DATA'; |
| 2 | +const STO_HELP = 'STO_HELP'; |
| 3 | +const STOF_TEXT = 'text'; |
| 4 | +const STOF_B64 = 'base64'; |
| 5 | + |
| 6 | +const STORAGE_ENGINES = { |
| 7 | + '.zip': async (path) => newStorageZip(path), |
| 8 | + '/': async (path) => newStorageDir(path), |
| 9 | +}; |
| 10 | + |
| 11 | +var _Storage = (() => { |
| 12 | + var storagesC = new Map(); |
| 13 | + |
| 14 | + async function add(key, path) { |
| 15 | + for (const keyC in STORAGE_ENGINES) { |
| 16 | + if (path.endsWith(keyC)) { |
| 17 | + const eng = await STORAGE_ENGINES[keyC](path); |
| 18 | + storagesC.set(key, eng); |
| 19 | + return true; |
| 20 | + } |
| 21 | + } |
| 22 | + return null; |
| 23 | + } |
| 24 | + |
| 25 | + async function search(key, filePath, format = STOF_TEXT) { |
| 26 | + if (!storagesC.has(key)) |
| 27 | + return null; |
| 28 | + |
| 29 | + return await storagesC.get(key).search(filePath, format); |
| 30 | + } |
| 31 | + |
| 32 | + async function getSubdirs(key, parentPath) { |
| 33 | + if (!storagesC.has(key)) |
| 34 | + return []; |
| 35 | + |
| 36 | + return await storagesC.get(key).getSubdirs(parentPath); |
| 37 | + } |
| 38 | + |
| 39 | + async function searchImage(key, filePath) { |
| 40 | + if (!storagesC.has(key)) |
| 41 | + return null; |
| 42 | + |
| 43 | + return await storagesC.get(key).searchImage(filePath); |
| 44 | + } |
| 45 | + |
| 46 | + return { |
| 47 | + add, |
| 48 | + search, |
| 49 | + getSubdirs, |
| 50 | + searchImage |
| 51 | + }; |
| 52 | +})(); |
| 53 | + |
| 54 | +async function newStorageZip(path) { |
| 55 | + var storageO = await init(path); |
| 56 | + |
| 57 | + async function init(path) { |
| 58 | + return await ZIPHelpers.loadZipFromUrl(path); |
| 59 | + } |
| 60 | + |
| 61 | + async function search(filePath, format = STOF_TEXT) { |
| 62 | + return await ZIPHelpers.searchArchiveForFile(filePath, storageO, format); |
| 63 | + } |
| 64 | + |
| 65 | + async function getSubdirs(parentPath) { |
| 66 | + const subdirs = new Set(); |
| 67 | + |
| 68 | + storageO?.forEach((relativePath, file) => { |
| 69 | + if (relativePath.startsWith(parentPath) && relativePath !== parentPath) |
| 70 | + { |
| 71 | + const subPath = relativePath.slice(parentPath.length); |
| 72 | + const parts = subPath.split("/"); |
| 73 | + |
| 74 | + if (parts.length > 1) { |
| 75 | + subdirs.add(parts[0]); |
| 76 | + } else if (file.dir) { |
| 77 | + subdirs.add(parts[0]); |
| 78 | + } |
| 79 | + } |
| 80 | + }); |
| 81 | + |
| 82 | + return [...subdirs]; |
| 83 | + } |
| 84 | + |
| 85 | + async function searchImage(filePath) { |
| 86 | + const content = await search(filePath, STOF_B64); |
| 87 | + if (!content) return null; |
| 88 | + var mimeType = 'image/' + filePath.split('.').pop().toLowerCase(); |
| 89 | + return `data:${mimeType};base64,${content}`; |
| 90 | + } |
| 91 | + |
| 92 | + return { |
| 93 | + search, |
| 94 | + getSubdirs, |
| 95 | + searchImage |
| 96 | + }; |
| 97 | +} |
| 98 | + |
| 99 | +async function newStorageDir(path) { |
| 100 | + var storageO = await init(path); |
| 101 | + |
| 102 | + async function init(path) { |
| 103 | + return path.replace(/\/$/, ''); |
| 104 | + } |
| 105 | + async function search(filePath, format = STOF_TEXT) { |
| 106 | + const fpath = `${storageO}/${filePath}`; |
| 107 | + const response = await fetchDataOrEmpty(fpath); |
| 108 | + |
| 109 | + switch (format) { |
| 110 | + case STOF_B64: |
| 111 | + const zip = new JSZip(); |
| 112 | + const fname = "a.txt"; |
| 113 | + zip.file(fname, response, { compression: "STORE" }); |
| 114 | + const base64zip = await zip.generateAsync({ type: STOF_B64 }); |
| 115 | + await zip.loadAsync(base64zip, { base64: true }); |
| 116 | + const file = zip.file(fname); |
| 117 | + const b64Data = await file.async(STOF_B64); |
| 118 | + return b64Data; |
| 119 | + case STOF_TEXT: |
| 120 | + default: |
| 121 | + return toText(response); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + function toText(ab) { |
| 126 | + const decoder = new TextDecoder("utf-8"); |
| 127 | + const text = decoder.decode(ab); |
| 128 | + return text; |
| 129 | + } |
| 130 | + |
| 131 | + async function getSubdirs(parentPath) { |
| 132 | + const list = search(`${storageO}/${parentPath}/__dir.lst`, format = STOF_TEXT); |
| 133 | + const text = toText(list); |
| 134 | + text = text.trim().replace(/\r\n/g, "\n").split('\n'); |
| 135 | + |
| 136 | + const subdirs = new Set(); |
| 137 | + text?.forEach((line, index) => { |
| 138 | + subdirs.add(line); |
| 139 | + }); |
| 140 | + |
| 141 | + return [...subdirs]; |
| 142 | + } |
| 143 | + |
| 144 | + async function fetchDataOrEmpty(url) { |
| 145 | + try { |
| 146 | + const response = await fetchData(url); |
| 147 | + return response; |
| 148 | + } catch (error) { |
| 149 | + return new ArrayBuffer(0); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + async function searchImage(filePath) { |
| 154 | + const fpath = `${storageO}/${filePath}`; |
| 155 | + const response = await fetchDataOrEmpty(fpath); |
| 156 | + if (response.byteLength == 0) |
| 157 | + return null; |
| 158 | + return fpath; |
| 159 | + } |
| 160 | + |
| 161 | + return { |
| 162 | + search, |
| 163 | + getSubdirs, |
| 164 | + searchImage |
| 165 | + }; |
| 166 | +} |
2 | 167 |
|
3 | 168 | async function main() {
|
4 |
| - arcData = await loadZipFromUrl('hvdata/data.zip'); |
5 |
| - const srcT = await searchArchiveForFile('appmainRun.js', arcData); |
| 169 | + var st = await _Storage.add(STO_DATA, 'hvdata/data.zip'); |
| 170 | + const srcT = await _Storage.search(STO_DATA, 'appmainRun.js'); |
6 | 171 | appendJavaScript('appRun', srcT, document.body);
|
7 | 172 | runApp();
|
8 | 173 | }
|
9 | 174 |
|
10 |
| -async function loadZipFromUrl(url) { |
| 175 | +async function fetchData(url) { |
11 | 176 | try {
|
12 | 177 | const response = await fetch(url);
|
13 | 178 | if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
|
14 | 179 | const arrayBuffer = await response.arrayBuffer();
|
15 |
| - |
16 |
| - const arch = await JSZip.loadAsync(arrayBuffer); |
17 |
| - return arch; |
| 180 | + return arrayBuffer; |
18 | 181 | } catch (error) {
|
19 | 182 | throw error;
|
20 | 183 | }
|
21 | 184 | }
|
22 | 185 |
|
23 |
| -async function searchArchiveForFile(fileName, arch) { |
24 |
| - try { |
25 |
| - const fileContent = await arch.file(fileName)?.async('text'); |
26 |
| - return fileContent ?? ""; |
27 |
| - } catch (error) { |
28 |
| - return ""; |
| 186 | +const ZIPHelpers = (() => { |
| 187 | + async function loadZipFromUrl(url) { |
| 188 | + try { |
| 189 | + const arrayBuffer = await fetchData(url); |
| 190 | + const arch = await JSZip.loadAsync(arrayBuffer); |
| 191 | + return arch; |
| 192 | + } catch (error) { |
| 193 | + throw error; |
| 194 | + } |
29 | 195 | }
|
30 |
| -} |
| 196 | + |
| 197 | + async function searchArchiveForFile(fileName, arch, format = STOF_TEXT) { |
| 198 | + try { |
| 199 | + const fileContent = await arch.file(fileName)?.async(format); |
| 200 | + return fileContent ?? ""; |
| 201 | + } catch (error) { |
| 202 | + return ""; |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + return { |
| 207 | + loadZipFromUrl, |
| 208 | + searchArchiveForFile |
| 209 | + }; |
| 210 | +})(); |
31 | 211 |
|
32 | 212 | function appendCSS(id, content) {
|
33 | 213 | //if (document.getElementById(id)) return;
|
|
0 commit comments