"""Logic for transcoding creating module.""" from typing import Any from owsresponse import response from transcoding import config from transcoding.constants import error from transcoding.logic import transcoding_order from transcoding.models import ( preset as preset_model, transcoding_rule as transcoding_rule, ) from transcoding.utils import transcoding_rule_constructor def create_transcoding_by_preset( input_bucket: str, input_key: str, preset_alias: str, status_topic_alias: str | None = None, source_asset_metadata: dict[str, Any] | None = None, ) -> response.Response: """Create transcoding order with all transcoding jobs by preset. Args: input_bucket (str): Input S3 bucket name. input_key (str): Input key. preset_alias (str): Preset alias. status_topic_alias (str): Alias of sns topic for status reporting. source_asset_metadata (dict): Metadata extracted from input file Returns: response.Response: Created transcoding order info or error. """ preset_response = preset_model.get_preset_by_alias(preset_alias) if not preset_response: return preset_response preset = preset_response.message preset_id = preset["preset_id"] sns_topic = status_topic_alias or preset["sns_topic"] # set alias or None output_bucket = preset["output_bucket"] rules_response = transcoding_rule.get_transcoding_rule_by_input_parameters_filter( preset_id=preset_id, input_params_filters=(source_asset_metadata or {}) ) if not rules_response: return rules_response if not rules_response.message: error_message = error.ERROR_MESSAGE_TRANSCODING_RULES_NOT_FOUND return response.create_not_found_response(error_message) source_rules = rules_response.message return create_transcoding( input_bucket=input_bucket, input_key=input_key, source_rules=source_rules, output_bucket=output_bucket, preset_id=preset_id, status_topic_alias=sns_topic, source_metadata=source_asset_metadata, ) def create_transcoding( input_bucket: str, input_key: str, source_rules: list[dict[str, Any]], output_bucket: str | None = None, preset_id: int | None = None, status_topic_alias: str | None = None, source_metadata: dict[str, Any] | None = None, ) -> response.Response: """Start transcoding workflow for input s3 object. Args: input_bucket (str): Input S3 bucket name. input_key (str): Input key. source_rules (list): List of transcoding rules. output_bucket (str): Output S3 bucket name. preset_id (int): Id of transcoding preset (if exists). status_topic_alias (str): Alias of sns topic for status reporting. source_metadata (dict): Metadata of source file. Returns: response.Response: Created transcoding order info or error """ if not output_bucket: output_bucket = config.TRANSCODED_ASSETS_BUCKET_NAME prepared_rules = transcoding_rule_constructor.construct_rules( transcoding_rules=source_rules, source_asset_metadata=(source_metadata or {}) ) return transcoding_order.create( input_bucket=input_bucket, input_filename=input_key, output_bucket=output_bucket, transcoding_rules=prepared_rules, sns_topic_alias=status_topic_alias, preset_id=preset_id, )