From 7ac9a7fbaa852223df737a6913b16e31404723b8 Mon Sep 17 00:00:00 2001 From: kshtiijrajsharma Date: Thu, 7 Dec 2023 21:39:59 +0545 Subject: [PATCH 1/2] Adds dynamic folder path from filename for s3 upload --- API/api_worker.py | 24 +++++++++++++++++++----- API/raw_data.py | 9 +++++++++ 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/API/api_worker.py b/API/api_worker.py index b7d339ec..790864ec 100644 --- a/API/api_worker.py +++ b/API/api_worker.py @@ -48,21 +48,33 @@ def process_raw_data(self, params): ): logging.debug("Using STwithin Logic") params.use_st_within = True + params.file_name = ( format_file_name_str(params.file_name) if params.file_name else "Export" ) - exportname = f"{params.file_name}_{params.output_type}{f'_uid_{str(self.request.id)}' if params.uuid else ''}" - logging.info("Request %s received", exportname) + exportname = f"{params.file_name}_{params.output_type}{f'_uid_{str(self.request.id)}' if params.uuid else ''}" + params.file_name = params.file_name.split("/")[ + -1 + ] # get last item from list and consider it as a file rest is file path on s3 + exportname_parts = exportname.split("/") + file_parts = os.path.join(*exportname_parts) + logging.info( + "Request %s received with following %s file_path", + params.file_name, + file_parts, + ) geom_area, geom_dump, working_dir = RawData(params).extract_current_data( - exportname + file_parts ) inside_file_size = 0 if bind_zip: logging.debug("Zip Binding Started !") # saving file in temp directory instead of memory so that zipping file will not eat memory - upload_file_path = os.path.join(working_dir, os.pardir, f"{exportname}.zip") + upload_file_path = os.path.join( + working_dir, os.pardir, f"{exportname_parts[-1]}.zip" + ) zf = zipfile.ZipFile(upload_file_path, "w", zipfile.ZIP_DEFLATED) for file_path in pathlib.Path(working_dir).iterdir(): @@ -94,7 +106,9 @@ def process_raw_data(self, params): # check if download url will be generated from s3 or not from config if use_s3_to_upload: file_transfer_obj = S3FileTransfer() - upload_name = exportname if params.uuid else f"Recurring/{exportname}" + upload_name = file_parts if params.uuid else f"Recurring/{file_parts}" + logging.info(upload_name) + if exportname.startswith("hotosm_project"): # TM if not params.uuid: pattern = r"(hotosm_project_)(\d+)" diff --git a/API/raw_data.py b/API/raw_data.py index d976902f..4d54e6e2 100644 --- a/API/raw_data.py +++ b/API/raw_data.py @@ -403,6 +403,15 @@ def get_osm_current_snapshot_as_file( """ if not (user.role is UserRole.STAFF.value or user.role is UserRole.ADMIN.value): + if "/" in params.file_name: + raise HTTPException( + status_code=403, + detail=[ + { + "msg": "Insufficient Permission to use folder structure exports , Remove / from filename or get access" + } + ], + ) area_m2 = area(json.loads(params.geometry.json())) area_km2 = area_m2 * 1e-6 RAWDATA_CURRENT_POLYGON_AREA = int(EXPORT_MAX_AREA_SQKM) From d843078b9f385a9cd008cd1636922437cdb9d569 Mon Sep 17 00:00:00 2001 From: kshtiijrajsharma Date: Thu, 7 Dec 2023 21:46:08 +0545 Subject: [PATCH 2/2] only check if file_name is there on role check --- API/raw_data.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/API/raw_data.py b/API/raw_data.py index 4d54e6e2..5602e32a 100644 --- a/API/raw_data.py +++ b/API/raw_data.py @@ -403,15 +403,16 @@ def get_osm_current_snapshot_as_file( """ if not (user.role is UserRole.STAFF.value or user.role is UserRole.ADMIN.value): - if "/" in params.file_name: - raise HTTPException( - status_code=403, - detail=[ - { - "msg": "Insufficient Permission to use folder structure exports , Remove / from filename or get access" - } - ], - ) + if params.file_name: + if "/" in params.file_name: + raise HTTPException( + status_code=403, + detail=[ + { + "msg": "Insufficient Permission to use folder structure exports , Remove / from filename or get access" + } + ], + ) area_m2 = area(json.loads(params.geometry.json())) area_km2 = area_m2 * 1e-6 RAWDATA_CURRENT_POLYGON_AREA = int(EXPORT_MAX_AREA_SQKM)