Algorick commited on
Commit
3437bb4
·
verified ·
1 Parent(s): fc46458

dcc632483e2a

Browse files
Files changed (4) hide show
  1. web/backend.js +28 -11
  2. web/i18n/en.json +3 -1
  3. web/online.js +1 -0
  4. web/sw.js +3 -2
web/backend.js CHANGED
@@ -43,10 +43,17 @@
43
 
44
  const req2p = (r) => new Promise((resolve, reject) => { r.onsuccess = () => resolve(r.result); r.onerror = () => reject(r.error); });
45
 
46
- async function readBlob(path) {
 
 
 
 
47
  const d = await db();
48
  return req2p(d.transaction("blobs").objectStore("blobs").get(path));
49
  }
 
 
 
50
  async function readItem(file) {
51
  const d = await db();
52
  return req2p(d.transaction("items").objectStore("items").get(file));
@@ -129,9 +136,10 @@
129
  const file = `${SCOPE_PREFIX[scope]}${day}/${stem}.png`;
130
  const mini = thumbPath(scope, day, stem);
131
  const item = { ...meta, file, miniatura: mini, bozza: scope === "bozze", titolo: meta.titolo || "", preferita: !!meta.preferita };
 
132
  await tx(["blobs", "items"], "readwrite", (blobs, items) => {
133
- blobs.put(png, "outputs/" + file);
134
- blobs.put(thumb, "outputs/" + mini);
135
  items.put({ file, scope, t: Date.now(), meta: item });
136
  });
137
  return item;
@@ -143,8 +151,8 @@
143
  const { day, stem } = splitFile(file);
144
  const newFile = `${SCOPE_PREFIX[toScope]}${day}/${stem}.png`;
145
  const newMini = thumbPath(toScope, day, stem);
146
- const png = await readBlob("outputs/" + file);
147
- const thumb = await readBlob("outputs/" + rec.meta.miniatura);
148
  const meta = { ...rec.meta, file: newFile, miniatura: newMini, bozza: toScope === "bozze" };
149
  await tx(["blobs", "items"], "readwrite", (blobs, items) => {
150
  blobs.delete("outputs/" + file);
@@ -203,7 +211,8 @@
203
 
204
  async function saveInput(blob, name) {
205
  const path = `inputs/${today()}/${clock()}_${rand()}.png`;
206
- await tx("blobs", "readwrite", (s) => { s.put(blob, path); });
 
207
  const inputs = (await kvGet("inputs", {})) || {};
208
  inputs[path] = Date.now();
209
  await kvSet("inputs", inputs);
@@ -455,11 +464,19 @@
455
  if (pub.status === "fatto" && out && out.length) {
456
  state.job = { ...live, status: "salvataggio" };
457
  const results = [];
458
- for (let i = 0; i < out.length; i++) {
459
- const res = await fetch(out[i].url);
460
- const png = await res.blob();
461
- const meta = { ...pub.results[i], riferimenti: payload.riferimenti || [], maschera: payload.maschera || null };
462
- results.push(await storeItem("bozze", meta, png, await thumbnail(png)));
 
 
 
 
 
 
 
 
463
  }
464
  markGpuUse();
465
  state.job = { ...live, status: "fatto", results, progress: 1 };
 
43
 
44
  const req2p = (r) => new Promise((resolve, reject) => { r.onsuccess = () => resolve(r.result); r.onerror = () => reject(r.error); });
45
 
46
+ // Le immagini si salvano come ArrayBuffer: Safari in navigazione privata rifiuta i Blob in IndexedDB.
47
+ const pack = async (blob) => (blob instanceof Blob ? { type: blob.type, data: await blob.arrayBuffer() } : blob);
48
+ const unpack = (value) => (value && !(value instanceof Blob) && value.data ? new Blob([value.data], { type: value.type }) : value);
49
+
50
+ async function readRaw(path) {
51
  const d = await db();
52
  return req2p(d.transaction("blobs").objectStore("blobs").get(path));
53
  }
54
+ async function readBlob(path) {
55
+ return unpack(await readRaw(path));
56
+ }
57
  async function readItem(file) {
58
  const d = await db();
59
  return req2p(d.transaction("items").objectStore("items").get(file));
 
136
  const file = `${SCOPE_PREFIX[scope]}${day}/${stem}.png`;
137
  const mini = thumbPath(scope, day, stem);
138
  const item = { ...meta, file, miniatura: mini, bozza: scope === "bozze", titolo: meta.titolo || "", preferita: !!meta.preferita };
139
+ const [image, small] = await Promise.all([pack(png), pack(thumb)]);
140
  await tx(["blobs", "items"], "readwrite", (blobs, items) => {
141
+ blobs.put(image, "outputs/" + file);
142
+ blobs.put(small, "outputs/" + mini);
143
  items.put({ file, scope, t: Date.now(), meta: item });
144
  });
145
  return item;
 
151
  const { day, stem } = splitFile(file);
152
  const newFile = `${SCOPE_PREFIX[toScope]}${day}/${stem}.png`;
153
  const newMini = thumbPath(toScope, day, stem);
154
+ const png = await readRaw("outputs/" + file);
155
+ const thumb = await readRaw("outputs/" + rec.meta.miniatura);
156
  const meta = { ...rec.meta, file: newFile, miniatura: newMini, bozza: toScope === "bozze" };
157
  await tx(["blobs", "items"], "readwrite", (blobs, items) => {
158
  blobs.delete("outputs/" + file);
 
211
 
212
  async function saveInput(blob, name) {
213
  const path = `inputs/${today()}/${clock()}_${rand()}.png`;
214
+ const data = await pack(blob);
215
+ await tx("blobs", "readwrite", (s) => { s.put(data, path); });
216
  const inputs = (await kvGet("inputs", {})) || {};
217
  inputs[path] = Date.now();
218
  await kvSet("inputs", inputs);
 
464
  if (pub.status === "fatto" && out && out.length) {
465
  state.job = { ...live, status: "salvataggio" };
466
  const results = [];
467
+ try {
468
+ for (let i = 0; i < out.length; i++) {
469
+ const res = await fetch(out[i].url);
470
+ if (!res.ok) throw new Error(`download ${res.status}`);
471
+ const png = await res.blob();
472
+ const meta = { ...pub.results[i], riferimenti: payload.riferimenti || [], maschera: payload.maschera || null };
473
+ results.push(await storeItem("bozze", meta, png, await thumbnail(png)));
474
+ }
475
+ } catch (e) {
476
+ report(`salvataggio: ${e?.name || ""} ${e?.message || e}`);
477
+ const err = new Error("L'immagine è pronta ma il browser non è riuscito a salvarla. Riprova; se succede ancora, esci dalla navigazione privata.");
478
+ err.lost = false;
479
+ throw err;
480
  }
481
  markGpuUse();
482
  state.job = { ...live, status: "fatto", results, progress: 1 };
web/i18n/en.json CHANGED
@@ -620,5 +620,7 @@
620
  "Questa è la versione di prova online di PixRick, uno studio di immagini AI pensato per girare sul tuo computer: veloce, leggero, senza limiti di generazioni.": "This is the online trial of PixRick, an AI image studio designed to run on your own computer: fast, lightweight, with no generation limits.",
621
  "Una demo sulle GPU di Hugging Face": "A demo on Hugging Face GPUs",
622
  "Per provarla online senza installare niente, PixRick si appoggia alle GPU gratuite di Hugging Face. Per questo ogni giorno hai un po' di tempo e un numero limitato di generazioni. Gratis, senza registrazione.": "To let you try it online without installing anything, PixRick runs on Hugging Face's free GPUs. That's why each day you get a little time and a limited number of generations. Free, no sign-up.",
623
- "Descrivi la scena: soggetto, ambiente, luce, stile. Il testo da scrivere nell'immagine va tra virgolette.": "Describe the scene: subject, place, light, style. Text to show inside the image goes in quotes."
 
 
624
  }
 
620
  "Questa è la versione di prova online di PixRick, uno studio di immagini AI pensato per girare sul tuo computer: veloce, leggero, senza limiti di generazioni.": "This is the online trial of PixRick, an AI image studio designed to run on your own computer: fast, lightweight, with no generation limits.",
621
  "Una demo sulle GPU di Hugging Face": "A demo on Hugging Face GPUs",
622
  "Per provarla online senza installare niente, PixRick si appoggia alle GPU gratuite di Hugging Face. Per questo ogni giorno hai un po' di tempo e un numero limitato di generazioni. Gratis, senza registrazione.": "To let you try it online without installing anything, PixRick runs on Hugging Face's free GPUs. That's why each day you get a little time and a limited number of generations. Free, no sign-up.",
623
+ "Descrivi la scena: soggetto, ambiente, luce, stile. Il testo da scrivere nell'immagine va tra virgolette.": "Describe the scene: subject, place, light, style. Text to show inside the image goes in quotes.",
624
+ "L'immagine è pronta ma il browser non è riuscito a salvarla. Riprova; se succede ancora, esci dalla navigazione privata.": "The image is ready but your browser couldn't save it. Try again; if it keeps happening, leave private browsing.",
625
+ "Immagine non salvata": "Image not saved"
626
  }
web/online.js CHANGED
@@ -103,6 +103,7 @@
103
  if (BLOCKED.test(text)) return { title: "Questa richiesta non si può fare", text, retry: false };
104
  if (BUSY.test(text)) return { title: "Il motore è sotto sforzo", text: "Tante persone stanno generando in questo momento. Riprova tra qualche secondo.", retry: true };
105
  if (LOST.test(text)) return { title: "Manca un'immagine", text, retry: false };
 
106
  return { title: "Non è andata", text: "Qualcosa si è inceppato mentre lavoravo. Riprova: di solito al secondo tentativo va.", retry: true };
107
  }
108
 
 
103
  if (BLOCKED.test(text)) return { title: "Questa richiesta non si può fare", text, retry: false };
104
  if (BUSY.test(text)) return { title: "Il motore è sotto sforzo", text: "Tante persone stanno generando in questo momento. Riprova tra qualche secondo.", retry: true };
105
  if (LOST.test(text)) return { title: "Manca un'immagine", text, retry: false };
106
+ if (/salvarla|couldn't save it/i.test(text)) return { title: "Immagine non salvata", text, retry: true };
107
  return { title: "Non è andata", text: "Qualcosa si è inceppato mentre lavoravo. Riprova: di solito al secondo tentativo va.", retry: true };
108
  }
109
 
web/sw.js CHANGED
@@ -21,13 +21,14 @@ function openDb() {
21
 
22
  async function fromStore(path) {
23
  const db = await openDb();
24
- const blob = await new Promise((resolve, reject) => {
25
  const r = db.transaction("blobs").objectStore("blobs").get(path);
26
  r.onsuccess = () => resolve(r.result);
27
  r.onerror = () => reject(r.error);
28
  });
29
  db.close();
30
- if (!blob) return new Response("Immagine non trovata", { status: 404 });
 
31
  return new Response(blob, {
32
  headers: { "Content-Type": blob.type || (path.endsWith(".webp") ? "image/webp" : "image/png"), "Cache-Control": "no-store" },
33
  });
 
21
 
22
  async function fromStore(path) {
23
  const db = await openDb();
24
+ const stored = await new Promise((resolve, reject) => {
25
  const r = db.transaction("blobs").objectStore("blobs").get(path);
26
  r.onsuccess = () => resolve(r.result);
27
  r.onerror = () => reject(r.error);
28
  });
29
  db.close();
30
+ if (!stored) return new Response("Immagine non trovata", { status: 404 });
31
+ const blob = stored instanceof Blob ? stored : new Blob([stored.data], { type: stored.type });
32
  return new Response(blob, {
33
  headers: { "Content-Type": blob.type || (path.endsWith(".webp") ? "image/webp" : "image/png"), "Cache-Control": "no-store" },
34
  });