Store mp3 size in database

Persist file size at creation and backfill missing sizes on list responses so the UI can display sizes reliably.
This commit is contained in:
dsyoon
2026-01-30 20:51:18 +09:00
parent 7ee89d0629
commit 1d92f2f4fa
2 changed files with 54 additions and 15 deletions

View File

@@ -29,10 +29,17 @@ def init_db():
id SERIAL PRIMARY KEY,
text TEXT NOT NULL,
filename TEXT,
size_bytes BIGINT,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
"""
)
cur.execute(
"""
ALTER TABLE tts_items
ADD COLUMN IF NOT EXISTS size_bytes BIGINT;
"""
)
cur.execute(
"""
CREATE INDEX IF NOT EXISTS tts_items_created_at_idx
@@ -72,12 +79,26 @@ def update_filename(tts_id: int, filename: str) -> None:
conn.commit()
def update_size_bytes(tts_id: int, size_bytes: int) -> None:
with get_conn() as conn:
with conn.cursor() as cur:
cur.execute(
"""
UPDATE tts_items
SET size_bytes = %s
WHERE id = %s;
""",
(size_bytes, tts_id),
)
conn.commit()
def list_items() -> List[Dict[str, Any]]:
with get_conn() as conn:
with conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as cur:
cur.execute(
"""
SELECT id, created_at, filename
SELECT id, created_at, filename, size_bytes
FROM tts_items
ORDER BY created_at DESC;
"""
@@ -91,7 +112,7 @@ def get_item(tts_id: int) -> Optional[Dict[str, Any]]:
with conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as cur:
cur.execute(
"""
SELECT id, text, filename, created_at
SELECT id, text, filename, size_bytes, created_at
FROM tts_items
WHERE id = %s;
""",