fix: resolve gallery labels from current plans and preserve render snapshots
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
import hashlib
|
||||
import json
|
||||
from io import BytesIO
|
||||
from pathlib import Path
|
||||
from uuid import uuid4
|
||||
|
||||
from PIL import Image
|
||||
@@ -234,21 +235,46 @@ class Libraries:
|
||||
"versions": self.versions(picture_id),
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def render_parameters(plan):
|
||||
return plan.model_dump(exclude={"name", "source_id"})
|
||||
|
||||
def versions(self, picture_id):
|
||||
return [
|
||||
{
|
||||
**self.service.assets.info(r["asset_id"]),
|
||||
"source_image_id": r["source_image_id"],
|
||||
"plan_id": r["plan_id"],
|
||||
"revision": r["revision"],
|
||||
"plan": json.loads(r["plan"]),
|
||||
"terminal_uses": self.terminal_uses(r["plan_id"], r["asset_id"]),
|
||||
}
|
||||
for r in self.db.all(
|
||||
"SELECT * FROM library_versions WHERE source_image_id=? ORDER BY created_at DESC,asset_id",
|
||||
(picture_id,),
|
||||
result = []
|
||||
rows = self.db.all(
|
||||
"SELECT v.*,p.payload AS current_plan,p.archived,a.name AS source_name "
|
||||
"FROM library_versions v JOIN library_plans p ON p.id=v.plan_id "
|
||||
"JOIN source_images s ON s.id=v.source_image_id JOIN assets a ON a.id=s.id "
|
||||
"WHERE v.source_image_id=? ORDER BY v.created_at DESC,v.asset_id DESC",
|
||||
(picture_id,),
|
||||
)
|
||||
for row in rows:
|
||||
current = GenerationPlan.model_validate_json(row["current_plan"])
|
||||
snapshot = GenerationPlan.model_validate_json(row["plan"])
|
||||
asset = self.service.assets.info(row["asset_id"])
|
||||
display_name = (
|
||||
Path(Path(row["source_name"]).stem + "-" + current.name).name[:110]
|
||||
+ ".jpg"
|
||||
)
|
||||
]
|
||||
result.append(
|
||||
{
|
||||
**asset,
|
||||
"name": display_name,
|
||||
"stored_name": asset["name"],
|
||||
"source_image_id": row["source_image_id"],
|
||||
"plan_id": row["plan_id"],
|
||||
"plan_name": current.name,
|
||||
"plan_archived": bool(row["archived"]),
|
||||
"matches_current_plan": self.render_parameters(current)
|
||||
== self.render_parameters(snapshot),
|
||||
"revision": row["revision"],
|
||||
"plan": json.loads(row["plan"]),
|
||||
"terminal_uses": self.terminal_uses(
|
||||
row["plan_id"], row["asset_id"]
|
||||
),
|
||||
}
|
||||
)
|
||||
return result
|
||||
|
||||
def terminal_uses(self, plan_id, asset_id):
|
||||
return self.db.all(
|
||||
@@ -295,17 +321,26 @@ class Libraries:
|
||||
"Picture and generation plan must share a source library",
|
||||
409,
|
||||
)
|
||||
revision = hashlib.sha256(encode(plan.model_dump()).encode()).hexdigest()
|
||||
parameters = self.render_parameters(plan)
|
||||
revision = hashlib.sha256(encode(parameters).encode()).hexdigest()
|
||||
row = self.db.one(
|
||||
"SELECT asset_id FROM library_versions WHERE source_image_id=? AND plan_id=? AND revision=?",
|
||||
(picture_id, plan_id, revision),
|
||||
)
|
||||
existing = self.versions(picture_id)
|
||||
if row:
|
||||
return next(
|
||||
v
|
||||
for v in self.versions(picture_id)
|
||||
if v["asset_id"] == row["asset_id"]
|
||||
)
|
||||
return next(v for v in existing if v["asset_id"] == row["asset_id"])
|
||||
# Older revisions hashed the display name too. Reuse matching pixels'
|
||||
# settings without rewriting historical snapshots or receipt references.
|
||||
for version in existing:
|
||||
if (
|
||||
version["plan_id"] == plan_id
|
||||
and self.render_parameters(
|
||||
GenerationPlan.model_validate(version["plan"])
|
||||
)
|
||||
== parameters
|
||||
):
|
||||
return version
|
||||
data = self.service.assets.path(picture_id).read_bytes()
|
||||
asset = self.service.assets.put(
|
||||
self.render(data, picture["caption"], plan),
|
||||
|
||||
Reference in New Issue
Block a user