Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature : Dynamic Folder Upload #176

Merged
merged 2 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
10 changes: 10 additions & 0 deletions API/raw_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +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 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)
Expand Down
Loading