sm-click-library-ui 0.0.389 → 0.0.391

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,438 @@
1
+ import { W as P, b as x } from "./index-BQGTiyKc.js";
2
+ import { E } from "./web-CFqJDFIT.js";
3
+ function m(w) {
4
+ const e = w.split("/").filter((t) => t !== "."), r = [];
5
+ return e.forEach((t) => {
6
+ t === ".." && r.length > 0 && r[r.length - 1] !== ".." ? r.pop() : r.push(t);
7
+ }), r.join("/");
8
+ }
9
+ function R(w, e) {
10
+ w = m(w), e = m(e);
11
+ const r = w.split("/"), t = e.split("/");
12
+ return w !== e && r.every((i, n) => i === t[n]);
13
+ }
14
+ class g extends P {
15
+ constructor() {
16
+ super(...arguments), this.DB_VERSION = 1, this.DB_NAME = "Disc", this._writeCmds = ["add", "put", "delete"], this.downloadFile = async (e) => {
17
+ var r, t;
18
+ const i = x(e, e.webFetchExtra), n = await fetch(e.url, i);
19
+ let s;
20
+ if (!e.progress)
21
+ s = await n.blob();
22
+ else if (!(n != null && n.body))
23
+ s = new Blob();
24
+ else {
25
+ const c = n.body.getReader();
26
+ let o = 0;
27
+ const d = [], h = n.headers.get("content-type"), y = parseInt(n.headers.get("content-length") || "0", 10);
28
+ for (; ; ) {
29
+ const { done: f, value: p } = await c.read();
30
+ if (f)
31
+ break;
32
+ d.push(p), o += (p == null ? void 0 : p.length) || 0;
33
+ const b = {
34
+ url: e.url,
35
+ bytes: o,
36
+ contentLength: y
37
+ };
38
+ this.notifyListeners("progress", b);
39
+ }
40
+ const u = new Uint8Array(o);
41
+ let l = 0;
42
+ for (const f of d)
43
+ typeof f > "u" || (u.set(f, l), l += f.length);
44
+ s = new Blob([u.buffer], { type: h || void 0 });
45
+ }
46
+ return { path: (await this.writeFile({
47
+ path: e.path,
48
+ directory: (r = e.directory) !== null && r !== void 0 ? r : void 0,
49
+ recursive: (t = e.recursive) !== null && t !== void 0 ? t : !1,
50
+ data: s
51
+ })).uri, blob: s };
52
+ };
53
+ }
54
+ readFileInChunks(e, r) {
55
+ throw this.unavailable("Method not implemented.");
56
+ }
57
+ async initDb() {
58
+ if (this._db !== void 0)
59
+ return this._db;
60
+ if (!("indexedDB" in window))
61
+ throw this.unavailable("This browser doesn't support IndexedDB");
62
+ return new Promise((e, r) => {
63
+ const t = indexedDB.open(this.DB_NAME, this.DB_VERSION);
64
+ t.onupgradeneeded = g.doUpgrade, t.onsuccess = () => {
65
+ this._db = t.result, e(t.result);
66
+ }, t.onerror = () => r(t.error), t.onblocked = () => {
67
+ console.warn("db blocked");
68
+ };
69
+ });
70
+ }
71
+ static doUpgrade(e) {
72
+ const t = e.target.result;
73
+ switch (e.oldVersion) {
74
+ case 0:
75
+ case 1:
76
+ default:
77
+ t.objectStoreNames.contains("FileStorage") && t.deleteObjectStore("FileStorage"), t.createObjectStore("FileStorage", { keyPath: "path" }).createIndex("by_folder", "folder");
78
+ }
79
+ }
80
+ async dbRequest(e, r) {
81
+ const t = this._writeCmds.indexOf(e) !== -1 ? "readwrite" : "readonly";
82
+ return this.initDb().then((i) => new Promise((n, s) => {
83
+ const o = i.transaction(["FileStorage"], t).objectStore("FileStorage")[e](...r);
84
+ o.onsuccess = () => n(o.result), o.onerror = () => s(o.error);
85
+ }));
86
+ }
87
+ async dbIndexRequest(e, r, t) {
88
+ const i = this._writeCmds.indexOf(r) !== -1 ? "readwrite" : "readonly";
89
+ return this.initDb().then((n) => new Promise((s, a) => {
90
+ const h = n.transaction(["FileStorage"], i).objectStore("FileStorage").index(e)[r](...t);
91
+ h.onsuccess = () => s(h.result), h.onerror = () => a(h.error);
92
+ }));
93
+ }
94
+ getPath(e, r) {
95
+ const t = r !== void 0 ? r.replace(/^[/]+|[/]+$/g, "") : "";
96
+ let i = "";
97
+ return e !== void 0 && (i += "/" + e), r !== "" && (i += "/" + t), i;
98
+ }
99
+ async clear() {
100
+ (await this.initDb()).transaction(["FileStorage"], "readwrite").objectStore("FileStorage").clear();
101
+ }
102
+ /**
103
+ * Read a file from disk
104
+ * @param options options for the file read
105
+ * @return a promise that resolves with the read file data result
106
+ */
107
+ async readFile(e) {
108
+ const r = this.getPath(e.directory, e.path), t = await this.dbRequest("get", [r]);
109
+ if (t === void 0)
110
+ throw Error("File does not exist.");
111
+ return { data: t.content ? t.content : "" };
112
+ }
113
+ /**
114
+ * Write a file to disk in the specified location on device
115
+ * @param options options for the file write
116
+ * @return a promise that resolves with the file write result
117
+ */
118
+ async writeFile(e) {
119
+ const r = this.getPath(e.directory, e.path);
120
+ let t = e.data;
121
+ const i = e.encoding, n = e.recursive, s = await this.dbRequest("get", [r]);
122
+ if (s && s.type === "directory")
123
+ throw Error("The supplied path is a directory.");
124
+ const a = r.substr(0, r.lastIndexOf("/"));
125
+ if (await this.dbRequest("get", [a]) === void 0) {
126
+ const h = a.indexOf("/", 1);
127
+ if (h !== -1) {
128
+ const y = a.substr(h);
129
+ await this.mkdir({
130
+ path: y,
131
+ directory: e.directory,
132
+ recursive: n
133
+ });
134
+ }
135
+ }
136
+ if (!i && !(t instanceof Blob) && (t = t.indexOf(",") >= 0 ? t.split(",")[1] : t, !this.isBase64String(t)))
137
+ throw Error("The supplied data is not valid base64 content.");
138
+ const o = Date.now(), d = {
139
+ path: r,
140
+ folder: a,
141
+ type: "file",
142
+ size: t instanceof Blob ? t.size : t.length,
143
+ ctime: o,
144
+ mtime: o,
145
+ content: t
146
+ };
147
+ return await this.dbRequest("put", [d]), {
148
+ uri: d.path
149
+ };
150
+ }
151
+ /**
152
+ * Append to a file on disk in the specified location on device
153
+ * @param options options for the file append
154
+ * @return a promise that resolves with the file write result
155
+ */
156
+ async appendFile(e) {
157
+ const r = this.getPath(e.directory, e.path);
158
+ let t = e.data;
159
+ const i = e.encoding, n = r.substr(0, r.lastIndexOf("/")), s = Date.now();
160
+ let a = s;
161
+ const c = await this.dbRequest("get", [r]);
162
+ if (c && c.type === "directory")
163
+ throw Error("The supplied path is a directory.");
164
+ if (await this.dbRequest("get", [n]) === void 0) {
165
+ const h = n.indexOf("/", 1);
166
+ if (h !== -1) {
167
+ const y = n.substr(h);
168
+ await this.mkdir({
169
+ path: y,
170
+ directory: e.directory,
171
+ recursive: !0
172
+ });
173
+ }
174
+ }
175
+ if (!i && !this.isBase64String(t))
176
+ throw Error("The supplied data is not valid base64 content.");
177
+ if (c !== void 0) {
178
+ if (c.content instanceof Blob)
179
+ throw Error("The occupied entry contains a Blob object which cannot be appended to.");
180
+ c.content !== void 0 && !i ? t = btoa(atob(c.content) + atob(t)) : t = c.content + t, a = c.ctime;
181
+ }
182
+ const d = {
183
+ path: r,
184
+ folder: n,
185
+ type: "file",
186
+ size: t.length,
187
+ ctime: a,
188
+ mtime: s,
189
+ content: t
190
+ };
191
+ await this.dbRequest("put", [d]);
192
+ }
193
+ /**
194
+ * Delete a file from disk
195
+ * @param options options for the file delete
196
+ * @return a promise that resolves with the deleted file data result
197
+ */
198
+ async deleteFile(e) {
199
+ const r = this.getPath(e.directory, e.path);
200
+ if (await this.dbRequest("get", [r]) === void 0)
201
+ throw Error("File does not exist.");
202
+ if ((await this.dbIndexRequest("by_folder", "getAllKeys", [IDBKeyRange.only(r)])).length !== 0)
203
+ throw Error("Folder is not empty.");
204
+ await this.dbRequest("delete", [r]);
205
+ }
206
+ /**
207
+ * Create a directory.
208
+ * @param options options for the mkdir
209
+ * @return a promise that resolves with the mkdir result
210
+ */
211
+ async mkdir(e) {
212
+ const r = this.getPath(e.directory, e.path), t = e.recursive, i = r.substr(0, r.lastIndexOf("/")), n = (r.match(/\//g) || []).length, s = await this.dbRequest("get", [i]), a = await this.dbRequest("get", [r]);
213
+ if (n === 1)
214
+ throw Error("Cannot create Root directory");
215
+ if (a !== void 0)
216
+ throw Error("Current directory does already exist.");
217
+ if (!t && n !== 2 && s === void 0)
218
+ throw Error("Parent directory must exist");
219
+ if (t && n !== 2 && s === void 0) {
220
+ const d = i.substr(i.indexOf("/", 1));
221
+ await this.mkdir({
222
+ path: d,
223
+ directory: e.directory,
224
+ recursive: t
225
+ });
226
+ }
227
+ const c = Date.now(), o = {
228
+ path: r,
229
+ folder: i,
230
+ type: "directory",
231
+ size: 0,
232
+ ctime: c,
233
+ mtime: c
234
+ };
235
+ await this.dbRequest("put", [o]);
236
+ }
237
+ /**
238
+ * Remove a directory
239
+ * @param options the options for the directory remove
240
+ */
241
+ async rmdir(e) {
242
+ const { path: r, directory: t, recursive: i } = e, n = this.getPath(t, r), s = await this.dbRequest("get", [n]);
243
+ if (s === void 0)
244
+ throw Error("Folder does not exist.");
245
+ if (s.type !== "directory")
246
+ throw Error("Requested path is not a directory");
247
+ const a = await this.readdir({ path: r, directory: t });
248
+ if (a.files.length !== 0 && !i)
249
+ throw Error("Folder is not empty");
250
+ for (const c of a.files) {
251
+ const o = `${r}/${c.name}`;
252
+ (await this.stat({ path: o, directory: t })).type === "file" ? await this.deleteFile({ path: o, directory: t }) : await this.rmdir({ path: o, directory: t, recursive: i });
253
+ }
254
+ await this.dbRequest("delete", [n]);
255
+ }
256
+ /**
257
+ * Return a list of files from the directory (not recursive)
258
+ * @param options the options for the readdir operation
259
+ * @return a promise that resolves with the readdir directory listing result
260
+ */
261
+ async readdir(e) {
262
+ const r = this.getPath(e.directory, e.path), t = await this.dbRequest("get", [r]);
263
+ if (e.path !== "" && t === void 0)
264
+ throw Error("Folder does not exist.");
265
+ const i = await this.dbIndexRequest("by_folder", "getAllKeys", [IDBKeyRange.only(r)]);
266
+ return { files: await Promise.all(i.map(async (s) => {
267
+ let a = await this.dbRequest("get", [s]);
268
+ return a === void 0 && (a = await this.dbRequest("get", [s + "/"])), {
269
+ name: s.substring(r.length + 1),
270
+ type: a.type,
271
+ size: a.size,
272
+ ctime: a.ctime,
273
+ mtime: a.mtime,
274
+ uri: a.path
275
+ };
276
+ })) };
277
+ }
278
+ /**
279
+ * Return full File URI for a path and directory
280
+ * @param options the options for the stat operation
281
+ * @return a promise that resolves with the file stat result
282
+ */
283
+ async getUri(e) {
284
+ const r = this.getPath(e.directory, e.path);
285
+ let t = await this.dbRequest("get", [r]);
286
+ return t === void 0 && (t = await this.dbRequest("get", [r + "/"])), {
287
+ uri: (t == null ? void 0 : t.path) || r
288
+ };
289
+ }
290
+ /**
291
+ * Return data about a file
292
+ * @param options the options for the stat operation
293
+ * @return a promise that resolves with the file stat result
294
+ */
295
+ async stat(e) {
296
+ const r = this.getPath(e.directory, e.path);
297
+ let t = await this.dbRequest("get", [r]);
298
+ if (t === void 0 && (t = await this.dbRequest("get", [r + "/"])), t === void 0)
299
+ throw Error("Entry does not exist.");
300
+ return {
301
+ name: t.path.substring(r.length + 1),
302
+ type: t.type,
303
+ size: t.size,
304
+ ctime: t.ctime,
305
+ mtime: t.mtime,
306
+ uri: t.path
307
+ };
308
+ }
309
+ /**
310
+ * Rename a file or directory
311
+ * @param options the options for the rename operation
312
+ * @return a promise that resolves with the rename result
313
+ */
314
+ async rename(e) {
315
+ await this._copy(e, !0);
316
+ }
317
+ /**
318
+ * Copy a file or directory
319
+ * @param options the options for the copy operation
320
+ * @return a promise that resolves with the copy result
321
+ */
322
+ async copy(e) {
323
+ return this._copy(e, !1);
324
+ }
325
+ async requestPermissions() {
326
+ return { publicStorage: "granted" };
327
+ }
328
+ async checkPermissions() {
329
+ return { publicStorage: "granted" };
330
+ }
331
+ /**
332
+ * Function that can perform a copy or a rename
333
+ * @param options the options for the rename operation
334
+ * @param doRename whether to perform a rename or copy operation
335
+ * @return a promise that resolves with the result
336
+ */
337
+ async _copy(e, r = !1) {
338
+ let { toDirectory: t } = e;
339
+ const { to: i, from: n, directory: s } = e;
340
+ if (!i || !n)
341
+ throw Error("Both to and from must be provided");
342
+ t || (t = s);
343
+ const a = this.getPath(s, n), c = this.getPath(t, i);
344
+ if (a === c)
345
+ return {
346
+ uri: c
347
+ };
348
+ if (R(a, c))
349
+ throw Error("To path cannot contain the from path");
350
+ let o;
351
+ try {
352
+ o = await this.stat({
353
+ path: i,
354
+ directory: t
355
+ });
356
+ } catch {
357
+ const l = i.split("/");
358
+ l.pop();
359
+ const f = l.join("/");
360
+ if (l.length > 0 && (await this.stat({
361
+ path: f,
362
+ directory: t
363
+ })).type !== "directory")
364
+ throw new Error("Parent directory of the to path is a file");
365
+ }
366
+ if (o && o.type === "directory")
367
+ throw new Error("Cannot overwrite a directory with a file");
368
+ const d = await this.stat({
369
+ path: n,
370
+ directory: s
371
+ }), h = async (u, l, f) => {
372
+ const p = this.getPath(t, u), b = await this.dbRequest("get", [p]);
373
+ b.ctime = l, b.mtime = f, await this.dbRequest("put", [b]);
374
+ }, y = d.ctime ? d.ctime : Date.now();
375
+ switch (d.type) {
376
+ case "file": {
377
+ const u = await this.readFile({
378
+ path: n,
379
+ directory: s
380
+ });
381
+ r && await this.deleteFile({
382
+ path: n,
383
+ directory: s
384
+ });
385
+ let l;
386
+ !(u.data instanceof Blob) && !this.isBase64String(u.data) && (l = E.UTF8);
387
+ const f = await this.writeFile({
388
+ path: i,
389
+ directory: t,
390
+ data: u.data,
391
+ encoding: l
392
+ });
393
+ return r && await h(i, y, d.mtime), f;
394
+ }
395
+ case "directory": {
396
+ if (o)
397
+ throw Error("Cannot move a directory over an existing object");
398
+ try {
399
+ await this.mkdir({
400
+ path: i,
401
+ directory: t,
402
+ recursive: !1
403
+ }), r && await h(i, y, d.mtime);
404
+ } catch {
405
+ }
406
+ const u = (await this.readdir({
407
+ path: n,
408
+ directory: s
409
+ })).files;
410
+ for (const l of u)
411
+ await this._copy({
412
+ from: `${n}/${l.name}`,
413
+ to: `${i}/${l.name}`,
414
+ directory: s,
415
+ toDirectory: t
416
+ }, r);
417
+ r && await this.rmdir({
418
+ path: n,
419
+ directory: s
420
+ });
421
+ }
422
+ }
423
+ return {
424
+ uri: c
425
+ };
426
+ }
427
+ isBase64String(e) {
428
+ try {
429
+ return btoa(atob(e)) == e;
430
+ } catch {
431
+ return !1;
432
+ }
433
+ }
434
+ }
435
+ g._debug = !0;
436
+ export {
437
+ g as FilesystemWeb
438
+ };