diff --git a/app/routers/api_v3_actions_hosted_file.py b/app/routers/api_v3_actions_hosted_file.py index 8cca3f0..f95b3d3 100644 --- a/app/routers/api_v3_actions_hosted_file.py +++ b/app/routers/api_v3_actions_hosted_file.py @@ -14,9 +14,10 @@ log = logging.getLogger(__name__) from app.config import settings from app.db_sql import redis_lookup_id_random, sql_select, sql_update, sql_delete, get_id_random from app.methods.hosted_file_methods import ( - create_hosted_file_obj, load_hosted_file_obj, save_file, + create_hosted_file_obj, load_hosted_file_obj, save_file, create_hosted_file_link, delete_hosted_file_link, get_hosted_file_link_rec_list ) +from app.methods.lib_media import convert_file_method from app.lib_general_v3 import ( AccountContext, get_account_context, get_account_context_optional, SerializationParams, DelayParams @@ -447,3 +448,36 @@ async def delete_file_action( "record_removed": record_removed, "method": method }, status_message="Deletion process complete.") + + +# ### BEGIN ### API V3 Hosted File Action ### convert_file() ### +@router.get('/{hosted_file_id}/convert_file', response_model=Resp_Body_Base) +async def convert_file( + hosted_file_id: str = Path(min_length=11, max_length=22), + link_to_type: str = Query(...), + link_to_id: str = Query(...), + filename_no_ext: str = Query('automated_hosted_file_conversion'), + to_type: str = Query('webp'), + account: AccountContext = Depends(get_account_context), + ): + """ + Convert a hosted file to another format (e.g. PDF → webp image). + Runs pdf2image server-side and saves the result as a new hosted_file record + linked to the same parent object via link_to_type / link_to_id. + """ + lid_int = redis_lookup_id_random(record_id_random=link_to_id, table_name=link_to_type) + if not lid_int: + raise HTTPException(status_code=404, detail=f"Linked object not found: {link_to_type}:{link_to_id}") + result = await convert_file_method( + hosted_file_id=hosted_file_id, + link_to_type=link_to_type, + link_to_id=lid_int, + account_id=account.account_id, + account_id_random=account.account_id_random, + filename_no_ext=filename_no_ext, + to_type=to_type + ) + if result: + return mk_resp(data=result) + return mk_resp(data=None, status_code=400, status_message="Conversion failed.") +# ### END ### API V3 Hosted File Action ### convert_file() ###