misc/migration related

This commit is contained in:
2025-06-08 08:53:18 -04:00
parent 68408c4796
commit 4cdd6d0c99
13 changed files with 90 additions and 28 deletions

View File

@ -1,6 +1,7 @@
import os
import logging
import io
import traceback
import math
from typing import Optional
import aiosqlite as sqlite3
@ -14,7 +15,7 @@ class MemeUtil:
def __init__(self, constants) -> None:
self.constants = constants
self.meme_db_path = os.path.join("/mnt/data/share", "sqlite_dbs", "meme.db")
self.meme_db_path = os.path.join("/usr/local/share", "sqlite_dbs", "meme.db")
def is_png(self, buffer: bytes | io.BytesIO) -> bool:
"""
@ -72,8 +73,17 @@ class MemeUtil:
buffer = io.BytesIO(result["image"])
is_png = self.is_png(buffer)
if not is_png:
logging.debug("Converting %s, not detected as PNG", meme_id)
logging.debug(
"Converting meme_id: %s, not detected as PNG", meme_id
)
ret_image = self.convert_to_png(buffer)
converted = await self.replace_with_converted_png(
meme_id, ret_image
)
if converted:
logging.info("Converted meme_id: %s", meme_id)
else:
logging.info("Failed to convert meme_id: %s", meme_id)
else:
ret_image = result["image"]
return ret_image
@ -148,3 +158,34 @@ class MemeUtil:
return None
pages = math.ceil(count / rows_per_page)
return pages
async def replace_with_converted_png(self, meme_id: int, meme_image: bytes) -> bool:
"""
Replace stored image with converted PNG
Args:
meme_id (int)
meme_image (bytes)
Returns:
bool
"""
update_query: str = "UPDATE memes SET image = ?, file_ext = 'PNG' WHERE id = ?"
params: tuple = (
meme_image,
meme_id,
)
try:
async with sqlite3.connect(self.meme_db_path, timeout=5) as db_conn:
update = await db_conn.execute_insert(update_query, params)
if not update:
logging.info(
"replace_with_converted_png: Failed -> Update: %s\nFor meme_id: %s",
update,
meme_id,
)
return False
else:
return True
except Exception as e:
logging.info("replace_with_converted_png: %s", str(e))
traceback.print_exc()
return False