"""Asset Logic.""" from typing import Any from video.config import VIDEO_BUCKET_NAME from video.constants import job_io_fields from video.constants.thumbnails import CUSTOM_THUMBNAIL_PATH_PREFIX from video.exceptions import VideoAssetNotFound from video.models.s3 import presigned_url from video.models.sql import queries from video.models.sql.classes import product_video def get_product_assets( product_ids: list[int], asset_types: list[str] | None = None ) -> list[dict[str, Any]]: """Get the assets associated with many products. Args: product_ids (list): ID's of the products. asset_types (list): Asset type (video_master, H264_HD, etc.). Returns: list: The assets. Raises: VideoAssetNotFound: if no assets are found for the given product ids. """ if asset_types is None: asset_types = [] assets = queries.get_product_assets(product_ids, asset_types=asset_types) if not assets: raise VideoAssetNotFound() return assets def get_custom_thumbnail_s3_key(product_id: int) -> str | None: """Get the S3 key for the custom thumbnail associated with a product. Args: product_id (int): ID of the product. Returns: str: the S3 key. """ product_data = product_video.get(product_id) if not product_data: return None path = product_data.get("custom_thumbnail_path") if not path: return None return "{}{}".format(CUSTOM_THUMBNAIL_PATH_PREFIX, path) def generate_presigned_urls(keys: dict[str, Any]) -> dict[str, str | None]: """Generate presigned URLs for the provided S3 keys. Args: keys (dict): The S3 keys. Returns: dict: Containing the presigned URLs. """ prores_mezzanine_key = keys[job_io_fields.PRORES_MEZZANINE_OUTPUT_S3_KEY] h264_mezzanine_key = keys[job_io_fields.H264_MEZZANINE_OUTPUT_S3_KEY] thumbnail_key = keys[job_io_fields.THUMBNAIL_S3_KEY] custom_thumbnail_key = keys["custom_thumbnail_s3_key"] return { "prores_mezzanine_url": presigned_url.create_presigned_url( VIDEO_BUCKET_NAME, prores_mezzanine_key ), "h264_mezzanine_url": presigned_url.create_presigned_url( VIDEO_BUCKET_NAME, h264_mezzanine_key ), "thumbnail_url": presigned_url.create_presigned_url( VIDEO_BUCKET_NAME, thumbnail_key ), "custom_thumbnail_url": presigned_url.create_presigned_url( VIDEO_BUCKET_NAME, custom_thumbnail_key ) if custom_thumbnail_key else None, }