Skip to content

Commit

Permalink
Adds dynamic folder path from filename for s3 upload
Browse files Browse the repository at this point in the history
  • Loading branch information
kshitijrajsharma committed Dec 7, 2023
1 parent 64ccb7b commit 7ac9a7f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
24 changes: 19 additions & 5 deletions API/api_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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+)"
Expand Down
9 changes: 9 additions & 0 deletions API/raw_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 7ac9a7f

Please sign in to comment.