diff --git a/app/methods/event_file_methods.py b/app/methods/event_file_methods.py index 3daecd5..32a8be6 100644 --- a/app/methods/event_file_methods.py +++ b/app/methods/event_file_methods.py @@ -37,7 +37,7 @@ def create_event_file_obj(event_file_obj_new:Event_File_Base): # ### BEGIN ### API Event File Methods ### load_event_file_obj() ### def load_event_file_obj( event_file_id: int|str, - model_as_dict: bool = True, + model_as_dict: bool = False, # This was defaulted to True 2022-03-07 by_alias: bool = True, exclude_unset: bool = False, enabled: str = 'enabled', # enabled, disabled, all @@ -75,23 +75,16 @@ def load_event_file_obj( hosted_file_id = hosted_file_id, enabled = enabled, ): - event_file_obj.hosted_file = hosted_file_obj.dict(by_alias=by_alias, exclude_unset=exclude_unset) + event_file_obj.hosted_file = hosted_file_obj + # event_file_obj.hosted_file = hosted_file_obj.dict(by_alias=by_alias, exclude_unset=exclude_unset) else: - event_file_obj.hosted_file = None + event_file_obj.hosted_file = {} else: event_file_obj.hosted_file = None - # if inc_hosted_file: - # x_id = event_file_rec.get('x_id', None) - # if x_obj_result := load_x_obj(x_id=x_id): - # x_obj = x_obj_result - # event_file_obj.x = x_obj - # else: event_file_obj.x = None - - # model_as_dict = True + log.debug(event_file_obj) if model_as_dict: - log.debug(event_file_obj.dict(by_alias=by_alias, exclude_unset=exclude_unset)) # pylint: disable=no-member) return event_file_obj.dict(by_alias=by_alias, exclude_unset=exclude_unset) # pylint: disable=no-member else: return event_file_obj diff --git a/app/routers/event_file.py b/app/routers/event_file.py index f9c8713..17c7ac7 100644 --- a/app/routers/event_file.py +++ b/app/routers/event_file.py @@ -1,9 +1,10 @@ -import datetime +import datetime, os, pathlib from fastapi import APIRouter, Body, Depends, Header, HTTPException, Query, Response, status +from fastapi.responses import FileResponse from pydantic import BaseModel, EmailStr, Field from typing import Dict, List, Optional, Set, Union -from app.lib_general import log, logging +from app.lib_general import log, logging, common_route_params, Common_Route_Params from app.config import settings from app.db_sql import sql_insert, sql_update, sql_insert_or_update, sql_select, sql_delete, redis_lookup_id_random @@ -126,6 +127,52 @@ async def event_file_lookup( # ### END ### API Event File ### get_event_file_lookup() ### +# ### BEGIN ### API Event File ### download_event_file() ### +# Updated 2021-11-23 +@router.get('/{event_file_id}/download', response_model=Resp_Body_Base) +async def download_event_file( + event_file_id: str = Query(..., min_length=11, max_length=22), + filename: str = Query(None, min_length=4, max_length=100), + + commons: Common_Route_Params = Depends(common_route_params), + ): + log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL + log.debug(locals()) + + # ### SECTION ### Secondary data validation + if event_file_id := redis_lookup_id_random(record_id_random=event_file_id, table_name='event_file'): pass + else: return mk_resp(data=None, status_code=404, response=commons.response, status_message='The event_file ID was invalid or not found.') + + hosted_files_path = settings.PATH_HOSTED_FILES_ROOT + log.info(f'Hosted Files Path: {hosted_files_path}') + + if event_file_obj := load_event_file_obj( + event_file_id = event_file_id, + inc_hosted_file = True, + ): + pass + else: + return mk_resp(data=False, status_code=400, response=commons.response) # Bad Request + + + if not filename: + filename = event_file_obj.filename + log.info(f'Filename: {filename}') + dir_path = event_file_obj.hosted_file.directory_path + subdir_path = event_file_obj.hosted_file.subdirectory_path + hash_sha256 = event_file_obj.hosted_file.hash_sha256 + hash_filename = hash_sha256+'.file' + + full_subdirectory_path = os.path.join(hosted_files_path, subdir_path) + log.debug(full_subdirectory_path) + pathlib.Path(full_subdirectory_path).mkdir(parents=True, exist_ok=True) + file_path_w_subdir = os.path.join(full_subdirectory_path, hash_filename) + log.info(f'Full file path with subdirectory: {file_path_w_subdir}') + + return FileResponse(file_path_w_subdir, filename=filename) +# ### END ### API Hosted File ### download_tmp() ### + + # ### BEGIN ### API Event File ### get_event_file_obj() ### # Updated 2021-10-21 @router.get('/{event_file_id}', response_model=Resp_Body_Base) diff --git a/app/routers/hosted_file.py b/app/routers/hosted_file.py index b5bf40d..72b2068 100644 --- a/app/routers/hosted_file.py +++ b/app/routers/hosted_file.py @@ -1,4 +1,3 @@ -from __future__ import annotations # import datetime, hashlib, os, pathlib, shutil, time #from datetime import datetime, time, timedelta from fastapi import APIRouter, Body, Depends, File, Form, Header, HTTPException, Query, Response, status, UploadFile @@ -448,4 +447,4 @@ async def download_tmp( filename = 'text.xlsx' return FileResponse(full_dest_path, filename=filename) -# ### END ### API Hosted File ### download_tmp() ### \ No newline at end of file +# ### END ### API Hosted File ### download_tmp() ###