2# +==== BEGIN CatFeeder =================+
4# ..............(..../\\
5# ...............)..(.')
7# ...............\\(__)|
8# Inspired by Joan Stark
9# source https://www.asciiart.eu/
13# FILE: favicon_user.py
14# CREATION DATE: 05-01-2026
15# LAST Modified: 11:55:14 12-01-2026
17# This is the backend server in charge of making the actual website work.
19# COPYRIGHT: (c) Cat Feeder
20# PURPOSE: This is the class for handling user favicons.
22# +==== END CatFeeder =================+
25from typing
import TYPE_CHECKING, Optional, List, Union, Dict, Any
27from fastapi
import Response
28from display_tty
import Disp, initialise_logger
30from .
import favicon_constants
as FAV_CONST
31from .
import favicon_helpers
as FAV_HELPERS
32from .
import favicon_error_class
as FAV_ERR
34from ..utils
import CONST
35from ..core
import FinalSingleton
36from ..http_codes
import HCI, HttpDataTypes
37from ..core.runtime_manager
import RI, RuntimeManager
38from ..image_reducer
import IR_ERROR, FileFormat
42 from ..bucket
import Bucket
43 from ..image_reducer
import ImageReducer
44 from ..server_header
import ServerHeaders
45 from ..boilerplates.responses
import BoilerplateResponses
55 disp: Disp = initialise_logger(__qualname__,
False)
61 def __init__(self, success: int = 0, error: int = 84, debug: bool =
False) ->
None:
62 """Initialize the ImageReducer instance with dependencies.
65 debug: Enable debug logging when True. Defaults to False.
68 self.
disp.update_disp_debug(debug)
71 self.
disp.log_debug(
"Initialising...")
74 "BoilerplateResponses")
81 self.
disp.log_debug(
"Initialised")
83 def _no_user_id(self, title: str, token: Optional[str] =
None) -> Response:
84 self.
disp.log_error(
"There is no specified user id.")
87 def _no_data(self, title: str, token: Optional[str] =
None) -> Response:
88 self.
disp.log_error(
"There is no data available.")
91 message=
"No icon available",
98 content_type=HttpDataTypes.JSON,
103 """(This is a wrapper of the same function in the constants)
104 Convert an ImageReducer FileFormat to an HttpDataTypes value.
107 reducer_type (IR_CONST.FileFormat): The image reducer file format.
110 HttpDataTypes: The corresponding HTTP data type.
112 return FAV_CONST.reducer_type_to_data_type(reducer_type)
114 def list_user_favicons(self, user_id: Optional[int] =
None, *, title: str =
"list_user_favicon", token: Optional[str] =
None) -> Union[List[Dict[str, Any]], Response]:
115 table: str = FAV_CONST.FAVICON_USER_OWNED_TABLE
118 "There is no specified user to search for, returning []"
122 f
"Gathering the list of uploaded user icons from table '{table}' and user id='{user_id}'"
124 resp = self.
sql.get_data_from_table(
127 where=f
"user_id={user_id}"
129 if isinstance(resp, int):
131 f
"Failed to gather data for table '{table}' and user id='{user_id}'"
135 f
"Data gathered for table '{table}' and user id='{user_id}':\n{resp}"
139 def list_active_user_favicons(self, user_id: Optional[int] =
None, *, title: str =
"list_active_user_favicon", token: Optional[str] =
None) -> Union[List[Dict[str, Any]], Response]:
140 table: str = FAV_CONST.FAVICON_USER_ACTIVE_TABLE
143 "There is no specified user to search for, returning []"
147 f
"Gathering the list of uploaded user icons from table '{table}' and user id='{user_id}'"
149 resp = self.
sql.get_data_from_table(
152 where=f
"user_id={user_id}"
154 if isinstance(resp, int):
156 f
"Failed to gather data for table '{table}' and user id='{user_id}'"
160 f
"Data gathered for table '{table}' and user id='{user_id}':\n{resp}"
164 def get_user_favicon(self, user_id, favicon_id, *, title: str =
"list_user_favicon", token: Optional[str] =
None) -> Union[FAV_CONST.FaviconData, Response]:
165 table: str = FAV_CONST.FAVICON_USER_ACTIVE_TABLE
166 final_resp: FAV_CONST.FaviconData = FAV_CONST.FaviconData()
169 "There is no specified user to search for"
173 f
"Gathering the list of uploaded user icons from table '{table}' and user id='{user_id}'"
175 sql_resp = self.
sql.get_data_from_table(
178 where=[f
"user_id={user_id}", f
"icon_id={favicon_id}"]
180 if isinstance(sql_resp, int):
182 f
"Failed to gather data for table '{table}' and user id='{user_id}'"
185 if len(sql_resp) == 0:
187 sql_raw_data = self.
sql.get_data_from_table(
188 table=FAV_CONST.FAVICON_TABLE_MAIN,
190 where=f
"id={favicon_id}"
192 if isinstance(sql_raw_data, int):
194 f
"Failed to gather raw data for table '{FAV_CONST.FAVICON_TABLE_MAIN}' and icon id='{favicon_id}'"
197 if len(sql_raw_data) == 0:
199 final_resp.data = CONST.clean_dict(
200 sql_raw_data[0].copy(),
201 (FAV_CONST.FAVICON_IMAGE_PATH_KEY,
""),
204 if sql_raw_data[0][FAV_CONST.FAVICON_IMAGE_PATH_KEY]
is None or sql_raw_data[0][FAV_CONST.FAVICON_IMAGE_PATH_KEY] ==
"":
206 f
"There is no image path for icon id='{favicon_id}'"
209 img_path = sql_raw_data[0][FAV_CONST.FAVICON_IMAGE_PATH_KEY]
210 bucket_resp = self.
bucket.download_stream(
211 bucket_name=FAV_CONST.FAVICON_BUCKET_NAME,
214 if isinstance(bucket_resp, int):
216 f
"Failed to download image data from bucket '{FAV_CONST.FAVICON_BUCKET_NAME}' and path '{img_path}'"
219 final_resp.img = bucket_resp
224 "There is no ImageReducer instance available in the runtime manager"
231 f
"Data gathered for table '{table}' and user id='{user_id}':\n{final_resp}"
RuntimeManager server_header
Union[List[Dict[str, Any]], Response] list_active_user_favicons(self, Optional[int] user_id=None, *, str title="list_active_user_favicon", Optional[str] token=None)
RuntimeManager runtime_manager
Response _no_data(self, str title, Optional[str] token=None)
RuntimeManager boilerplate_response
Response _no_user_id(self, str title, Optional[str] token=None)
RuntimeManager image_reducer
Union[List[Dict[str, Any]], Response] list_user_favicons(self, Optional[int] user_id=None, *, str title="list_user_favicon", Optional[str] token=None)
None __init__(self, int success=0, int error=84, bool debug=False)
HttpDataTypes reducer_type_to_data_type(self, FileFormat reducer_type)
Union[FAV_CONST.FaviconData, Response] get_user_favicon(self, user_id, favicon_id, *, str title="list_user_favicon", Optional[str] token=None)