Skip to content

Commit

Permalink
Sorts files during unzip
Browse files Browse the repository at this point in the history
Ensures that related content are unpacked accordingly
in a mixed content case
  • Loading branch information
ridoo committed Oct 13, 2023
1 parent a8e0021 commit 4d3040d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
3 changes: 2 additions & 1 deletion geonode/storage/data_retriever.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,8 @@ def _unzip(self, zip_name: str) -> Mapping:
available_choices = get_allowed_extensions()
not_main_files = ["xml", "sld", "zip", "kmz"]
base_file_choices = [x for x in available_choices if x not in not_main_files]
for _file in Path(self.temporary_folder).iterdir():
sorted_files = sorted(Path(self.temporary_folder).iterdir())
for _file in sorted_files:
if not zipfile.is_zipfile(str(_file)):
if any([_file.name.endswith(_ext) for _ext in base_file_choices]):
self.file_paths["base_file"] = Path(str(_file))
Expand Down
27 changes: 27 additions & 0 deletions geonode/storage/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,3 +608,30 @@ def test_zip_file_should_correctly_recognize_main_extension_with_shp(self):
self.assertIsNotNone(storage_manager.data_retriever.temporary_folder)
_files = storage_manager.get_retrieved_paths()
self.assertTrue("single_point.shp" in _files.get("base_file"))

def test_zip_file_should_correctly_relate_mixed_content(self):
zip_file = os.path.join(f"{self.project_root}", "tests/data/data_collection.zip")
storage_manager = self.sut(
remote_files={"base_file": zip_file}
)
storage_manager.clone_remote_files()
self.assertIsNotNone(storage_manager.data_retriever.temporary_folder)
_files = storage_manager.get_retrieved_paths()
base_file = _files.get("base_file")

def strip_extension(filename):
return filename.split(".")[0] if filename else filename

base_name = strip_extension(base_file)
available_files = [
_files.get("shp_file"),
_files.get("prj_file"),
_files.get("shx_file"),
_files.get("dbf_file")
]

def assert_same_base_name(base_name, file_name):
self.assertEqual(base_name, strip_extension(file_name))

for file in available_files:
assert_same_base_name(base_name, file)
Binary file added geonode/storage/tests/data/data_collection.zip
Binary file not shown.

0 comments on commit 4d3040d

Please sign in to comment.