Skip to content

中文版 AI 姿势参考图库 · 1439 组 / 2878 张图片 / 完整汉化 · 零依赖静态画廊

fix: acknowledge storage transactions and persist image references

H-Bole48778459e051
diff --git a/src/lib/idbStorage.ts b/src/lib/idbStorage.ts
index b657522..8693226 100644
--- a/src/lib/idbStorage.ts
+++ b/src/lib/idbStorage.ts
@@ -16,18 +16,20 @@ function openDb(): Promise<IDBDatabase> {
})
}
-export async function idbGet<T>(key: string): Promise<T | undefined> {
+export async function idbRead<T>(key: string): Promise<T | undefined> {
+ const db = await openDb()
try {
- const db = await openDb()
return await new Promise<T | undefined>((resolve, reject) => {
const tx = db.transaction(STORE_NAME, 'readonly')
const req = tx.objectStore(STORE_NAME).get(key)
req.onsuccess = () => resolve(req.result as T | undefined)
req.onerror = () => reject(req.error)
+ tx.onabort = () => reject(tx.error)
})
- } catch {
- return undefined
- }
+ } finally { db.close() }
+}
+export async function idbGet<T>(key: string): Promise<T | undefined> {
+ try { return await idbRead<T>(key) } catch { return undefined }
}
export async function idbSet<T>(key: string, value: T): Promise<void> {
@@ -43,3 +45,16 @@ export async function idbSet<T>(key: string, value: T): Promise<void> {
// ignore storage errors in private browsing
}
}
+
+// 确认保存成功的操作必须等待事务提交。
+export async function idbWrite<T>(key: string, value: T): Promise<void> {
+ const db = await openDb()
+ try {
+ await new Promise<void>((resolve, reject) => {
+ const tx = db.transaction(STORE_NAME, 'readwrite')
+ tx.objectStore(STORE_NAME).put(value, key)
+ tx.oncomplete = () => resolve()
+ tx.onerror = tx.onabort = () => reject(tx.error || new Error('浏览器存储写入失败'))
+ })
+ } finally { db.close() }
+}
diff --git a/src/lib/persistentImages.ts b/src/lib/persistentImages.ts
new file mode 100644
index 0000000..a4b8c89
--- /dev/null
+++ b/src/lib/persistentImages.ts
@@ -0,0 +1,28 @@
+import type { RecipeState } from '@/types/gallery'
+import { referenceImageUrl } from './recipeImages'
+
+export async function durableImage(url: string): Promise<string> {
+ if (url.startsWith('data:image/')) return url
+ const response = await fetch(referenceImageUrl(url))
+ if (!response.ok) throw new Error('图片下载失败')
+ const blob = await response.blob()
+ if (!blob.type.startsWith('image/')) throw new Error('引用不是图片')
+ return new Promise((resolve, reject) => {
+ const reader = new FileReader()
+ reader.onload = () => resolve(String(reader.result))
+ reader.onerror = () => reject(new Error('图片读取失败'))
+ reader.readAsDataURL(blob)
+ })
+}
+export async function durableRecipe(recipe: RecipeState) {
+ const next = structuredClone(recipe)
+ for (const item of [next.portrait, next.costume, next.pose]) {
+ if (item?.source === 'upload') {
+ item.原图 = await durableImage(item.原图)
+ item.缩略图 = item.原图
+ if ('预览图' in item) item.预览图 = item.原图
+ }
+ }
+ for (const image of [...(next.extraImages || []), ...Object.values(next.workflowImages || {})]) image.url = await durableImage(image.url)
+ return next
+}