-
Notifications
You must be signed in to change notification settings - Fork 8
/
app.vue
506 lines (459 loc) · 13.3 KB
/
app.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
<template>
<main :class="{ dark: darkMode }">
<Header
:onFetchResults="fetchResults"
:onDownloadBook="downloadBook"
:view="view"
/>
<div
id="content"
@dragover.prevent="isDragging = true"
@dragleave.prevent="isDragging = false"
@drop.prevent="handleDrop"
@scroll="fetchMoreBooks"
:class="{ 'drag-over': isDragging }"
>
<AdvancedSearch v-if="view === 'search'" :onFetchResults="fetchResults" />
<Settings v-if="view === 'settings'" :onFetchResults="fetchResults" />
<Instances v-if="view === 'instances'" />
<Error
v-if="view === 'error'"
:error="error"
:attemped="lastResult"
:onRetry="handleClick"
:onGoBack="() => (view = 'results')"
/>
<BookView
v-if="view === 'book'"
:bookURL="bookURL"
:downloadProgress="downloadProgress"
:bookProgress="bookProgress"
:bookFile="bookFile"
/>
<BooksList
:booksList="results"
:isLoading="isLoading"
:onBookClick="handleClick"
:paginate="true"
:onFetchMoreBooks="fetchMoreBooks"
v-if="view === 'results'"
/>
<BooksList
:booksList="booksList"
:isLoading="false"
:onBookClick="handleClick"
v-if="view === 'history'"
/>
<div id="welcome" v-if="view === 'welcome'">
<LucideBookOpenText color="lightgray" />
</div>
</div>
<footer>
{{ title }} is an instance of
<a href="https://github.com/teatime-library/teatime" target="_blank"
>TeaTime</a
>, a distributed, static, cookie-less system for reading books over IPFS.
There are also
<a href="#" @click="() => (view = 'instances')">other instances</a>.
</footer>
</main>
</template>
<style>
* {
font-family: sans-serif;
}
body,
html {
margin: 0;
}
</style>
<style scoped>
main {
display: grid;
grid-template-columns: 1fr;
grid-template-rows: auto 1fr auto;
grid-column-gap: 0px;
grid-row-gap: 0px;
height: 100dvh;
background: white;
&.dark {
filter: invert(1);
}
}
#welcome {
display: flex;
height: 100%;
align-items: center;
justify-content: center;
flex-direction: column;
svg {
width: 50%;
height: 50%;
}
}
#content {
grid-area: 2 / 1 / 3 / 2;
overflow: auto;
&.drag-over {
border: 10px dashed red;
}
}
footer {
text-align: center;
font-size: 0.75rem;
color: gray;
grid-area: 3 / 1 / 4 / 2;
}
</style>
<script setup lang="ts">
import sanitize from "sanitize-filename";
const appConfig = useAppConfig();
import { createDbWorker } from "sql.js-httpvfs";
import { useLocalStorage } from "@vueuse/core";
import { getFile, saveFile } from "./indexdb";
import { Liquid } from "liquidjs";
const liquid = new Liquid();
useHead({
title: appConfig.title,
link: [
{
rel: "icon",
href:
"data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>" +
appConfig.icon +
"</text></svg>",
},
],
});
const workerUrl = new URL(
"sql.js-httpvfs/dist/sqlite.worker.js",
import.meta.url,
);
const wasmUrl = new URL("sql.js-httpvfs/dist/sql-wasm.wasm", import.meta.url);
const defaultColumns = Object.fromEntries(
["title", "author", "ext", "lang"].map((x) => [x, x]),
);
const remote = useLocalStorage("remote", null);
const lastQuery = useLocalStorage("lastQuery", null);
const remoteConfig = useLocalStorage("remoteConfig", null);
const ipfsGateway = useLocalStorage("ipfsGateway", "ipfs.io");
const searchQuery = useState("searchQuery", () => "");
const offset = useState("offset", () => 0);
const isLoading = useState("isLoading", () => false);
const directLink = useState("directLink", () => false);
const view = useState("view", () => "welcome");
const darkMode = useState("darkMode", () => false);
const bookURL = useState("bookURL", () => "");
const title = useState("title", () => appConfig.title);
const icon = useState("icon", () => appConfig.icon);
const moreResults = useState("moreResults", () => false);
const results = ref([]);
const lastResult = ref(null);
const error = ref(null);
const downloadProgress = ref(0);
const bookFile = ref(null);
const isDragging = ref(false);
const bookProgress = ref(0);
const booksList = computed(() => {
return view.value === "history"
? JSON.parse(localStorage.getItem("history") || "[]")
: results;
});
onMounted(async () => {
if (!remoteConfig.value) {
view.value = "settings";
} else {
fetchResults("", true);
}
});
const setResults = (books, append) => {
const { images = "", columns = {} } = JSON.parse(remoteConfig.value);
results.value = [
...(append ? results.value : []),
...books.map((b) => {
for (const key of Object.keys(columns)) {
b[key] = b[columns[key]];
delete b[columns[key]];
}
b.image_url = liquid.parseAndRenderSync(images, b);
return b;
}),
];
};
const downloadBook = () => {
const link = document.createElement("a");
const url = URL.createObjectURL(bookFile.value);
const b = lastResult.value;
link.href = url;
link.download = sanitize(`${b.author} - ${b.title}.${b.ext}`);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(url); // Release the URL object
};
const handleDrop = (event) => {
const files = event.dataTransfer.files;
if (files.length > 0) {
bookFile.value = files[0];
view.value = "book";
isDragging.value = false;
}
};
const fetchResults = async (query, reset, setRemote) => {
console.log("Running query", query, setRemote);
lastQuery.value = JSON.stringify(query);
if (setRemote) {
const [owner, repo] = setRemote.split("/");
const response = await fetch(
`https://${owner}.github.io/${repo}/config.json`,
);
const config = await response.json();
remoteConfig.value = JSON.stringify(config);
remote.value = setRemote;
offset.value = 0;
}
if (reset) offset.value = 0;
if (!remote.value) {
console.log("you have to choose a remote");
view.value = "settings";
return;
}
if (view === "search") {
searchQuery.value = "";
}
view.value = "results";
isLoading.value = true;
if (!offset.value) results.value = [];
let dbResult = [];
let maxBytesToRead = 10 * 1024 * 1024;
const {
dbConfig,
tableName = "updated_data",
columns = defaultColumns,
} = JSON.parse(remoteConfig.value);
try {
console.log("Connecting to the database...");
const worker = await createDbWorker(
[
{
from: "inline",
config: dbConfig,
},
],
workerUrl.toString(),
wasmUrl.toString(),
maxBytesToRead, // optional, defaults to Infinity
);
const perPage = 5;
const limitOffset = `LIMIT ${perPage * offset.value}, ${perPage}`;
console.log("Making the query...");
if (typeof query === "string") {
if (query) {
dbResult = await worker.db.exec(
`SELECT * FROM ${tableName}
WHERE id IN (
SELECT rowid FROM ${tableName}_fts
WHERE ${tableName}_fts MATCH ?
${limitOffset}
);`,
[`${columns.title}:${query}* OR ${columns.author}:${query}*`],
);
} else {
dbResult = await worker.db.exec(
`SELECT * FROM ${tableName}
${limitOffset}
;`,
[],
);
}
} else {
const whereQueries = [];
const params = [];
if (query.title && query.author) {
whereQueries.push(`${tableName}_fts MATCH ?`);
params.push(
`${columns.title}:${query.title} AND ${columns.author}:${query.author}`,
);
} else if (query.author) {
whereQueries.push(`${tableName}_fts MATCH ?`);
params.push(`${columns.author}:${query.author}`);
} else if (query.title) {
whereQueries.push(`${tableName}_fts MATCH ?`);
params.push(`${columns.title}:${query.title}`);
}
if (query.lang) {
whereQueries.push(columns.lang + " = ?");
params.push(query.lang);
}
if (query.ext) {
whereQueries.push(columns.ext + " = ?");
params.push(query.ext);
}
const where = params.join(" AND ");
const sqlQuery = `
SELECT ${tableName}.* FROM ${tableName}
JOIN ${tableName}_fts ON ${tableName}.id = ${tableName}_fts.rowid
WHERE ${whereQueries.join(" AND ")}
${limitOffset};`;
dbResult = await worker.db.exec(sqlQuery, params);
}
console.log("Gathering results...");
console.log(dbResult);
setResults(
dbResult[0].values.map((line) =>
Object.assign(
{},
...dbResult[0].columns.map((n, index) => ({ [n]: line[index] })),
),
),
!!offset.value,
);
} catch (e) {
console.error("Error while searching: ", e);
} finally {
isLoading.value = false;
}
if (dbResult[0].values.length) {
moreResults.value = true;
} else {
moreResults.value = false;
}
fetchMoreBooks();
};
const fetchMoreBooks = () => {
const obj = document.querySelector("#content");
if (
(obj.scrollTopMax === 0 || // There's no scroll
obj.scrollTop + 1 >= obj.scrollHeight - obj.offsetHeight) && // We're at the bottom
!isLoading.value &&
view.value === "results" &&
moreResults.value
) {
offset.value++;
fetchResults(JSON.parse(lastQuery.value));
}
};
const downloadUrls = (urls: string[], contentLength: number): Promise<Blob> => {
const controller = new AbortController();
const signal = controller.signal;
const downloads = urls.map((url) =>
fetch(url, { signal }).then((response) => {
if (!response.ok)
throw new Error(`HTTP error! status: ${response.status}`);
if (!response.body) throw new Error("Response body is null");
const reader = response.body.getReader();
let receivedLength = 0;
const chunks: Uint8Array[] = [];
return {
read: async function () {
while (true) {
const { done, value } = await reader.read();
if (done) {
break;
}
chunks.push(value);
receivedLength += value.length;
const progress = (receivedLength / contentLength) * 100;
if (progress > downloadProgress.value) {
downloadProgress.value = progress;
}
if (downloadProgress.value >= 100) {
controller.abort();
break;
}
}
return new Blob(chunks, {
type:
response.headers.get("content-type") ||
"application/octet-stream",
});
},
contentType:
response.headers.get("content-type") || "application/octet-stream",
};
}),
);
return new Promise((resolve, reject) => {
let completedDownloads = 0;
let failedDownloads = 0;
downloads.forEach(async (download) => {
try {
const { read, contentType } = await download;
const blob = await read();
resolve(new Blob([blob], { type: contentType }));
controller.abort(); // Cancel other ongoing downloads
} catch (error) {
failedDownloads++;
if (failedDownloads === urls.length) {
reject(new Error("All downloads failed"));
}
} finally {
completedDownloads++;
if (
completedDownloads === urls.length &&
failedDownloads === urls.length
) {
reject(new Error("All downloads failed"));
}
}
});
});
};
const handleClick = async (result) => {
lastResult.value = result;
view.value = "book";
const previousHistory = JSON.parse(localStorage.getItem("history") || "[]");
const updatedHistory = [result, ...previousHistory];
if (!previousHistory.find((x) => x.id === result.id)) {
localStorage.setItem("history", JSON.stringify(updatedHistory));
}
console.log("Selected result:", result.title, result.id);
isLoading.value = true;
error.value = null;
bookFile.value = null;
downloadProgress.value = 0;
const filename = sanitize(
`${result.author} - ${result.title}.${result.ext}`.trim(),
);
try {
const urls = [
"https://ipfs.io",
"https://flk-ipfs.xyz",
"https://ipfs.cyou",
"https://storry.tv",
"https://dweb.link",
"https://gateway.pinata.cloud",
"https://ipfs.runfission.com",
"https://nftstorage.link",
"https://4everland.io",
"https://w3s.link",
"http://localhost:8080",
].map((u) => `${u}/ipfs/${result.ipfs_cid}`);
bookURL.value = urls[0];
const retrievedFile = await getFile(result.ipfs_cid);
if (retrievedFile) {
console.log("Got file from IndexDB");
bookFile.value = retrievedFile;
} else {
const blob = await downloadUrls(urls, result.size);
bookFile.value = new File([blob], filename, {
type: blob.type,
lastModified: new Date().getTime(),
});
console.log("Download completed");
await saveFile(bookFile.value, result.ipfs_cid);
}
const { fraction } = updatedHistory.find((b) =>
bookURL.value.includes(result.ipfs_cid),
);
bookProgress.value = fraction;
} catch (error) {
console.error("Download failed:", error);
view.value = "error";
error.value = error;
return;
} finally {
isLoading.value = false;
}
};
</script>