diff --git a/tests/test_upload.py b/tests/test_upload.py index 5355e784f7060e0249a5d969eca178e37f103d34..510ad32710097dd9195885c626883a0b1b1397a3 100755 --- a/tests/test_upload.py +++ b/tests/test_upload.py @@ -21,7 +21,7 @@ IMAGE_HREF = ( COL_ID = "collection-for-theia-dumper-tests" items_ids = ["item_1", "item_2"] RASTER_FILE1 = "/tmp/raster1.tif" -RASTER_FILE2 = "/tmp/raster2.tif" +RASTER_FILE2 = "/tmp/folder/raster2.tif" handler = stac.StacUploadTransactionsHandler( stac_endpoint=STAC_EP, @@ -33,6 +33,7 @@ handler = stac.StacUploadTransactionsHandler( with open(RASTER_FILE1, "wb") as f: r = requests.get(IMAGE_HREF, timeout=5) f.write(r.content) +os.makedirs(os.path.dirname(RASTER_FILE2), exist_ok=True) shutil.copyfile(RASTER_FILE1, RASTER_FILE2) COL_BBOX = [0.0, 0.0, 0.0, 0.0] diff --git a/theia_dumper/__init__.py b/theia_dumper/__init__.py index ec8539461cdb7667033272a0675ef109ae8da6c9..9824c4c6fe8788bc6df47ffc17e3d475478458ab 100644 --- a/theia_dumper/__init__.py +++ b/theia_dumper/__init__.py @@ -1,3 +1,3 @@ """Theia dumper package.""" -__version__ = "0.1.0" +__version__ = "0.1.1" diff --git a/theia_dumper/stac.py b/theia_dumper/stac.py index 6ea96ee05e08c198c3cecdbebb048c0781dede24..e33c557fbaae4a636573cff01cf0fe130639a889 100644 --- a/theia_dumper/stac.py +++ b/theia_dumper/stac.py @@ -28,8 +28,10 @@ class UnconsistentCollectionIDs(Exception): """Inconsistent STAC collection exception.""" -def _check_naming_is_compliant(s: str, allow_dot=False): +def _check_naming_is_compliant(s: str, allow_dot=False, allow_slash=False): _s = re.sub(r"[-|_]", r"", s) + if allow_slash: + _s = re.sub(r"\/", r"", _s) if allow_dot: _s = re.sub(r"\.", r"", _s) if not _s.isalnum(): @@ -121,7 +123,10 @@ def load_stac_obj(obj_pth: str) -> Collection | ItemCollection | Item: def get_assets_root_dir(items: List[Item]) -> str: - """Get the common prefix of all items assets paths.""" + """Get the common prefix of all items assets paths. + + If the the common prefix is not a folder (/tmp/test1/a.tif, /tmp/test2/b.tif), returns /tmp. + """ prefix = os.path.commonprefix( [asset.href for item in items for asset in item.assets.values()] ) @@ -239,21 +244,28 @@ class StacUploadTransactionsHandler(StacTransactionsHandler): assets_overwrite: bool def publish_item(self, item: Item, assets_root_dir: str): - """Publish an item and all its assets.""" + """Publish an item and all its assets. + + Args: + item: Stac item to publish + assets_root_dir: Common path to all files, defined as assets root dir + """ col_id = item.collection_id - target_root_dir = urljoin(self.storage_endpoint, self.storage_bucket) + tgt_bucket_root_url = urljoin(self.storage_endpoint, self.storage_bucket) + "/" _check_naming_is_compliant(self.storage_bucket) _check_naming_is_compliant(item.id) - - # Upload assets files for _, asset in item.assets.items(): local_filename = asset.href logger.debug("Local file: %s", local_filename) - target_url = local_filename.replace(assets_root_dir, target_root_dir) + target_url = local_filename.replace(assets_root_dir, tgt_bucket_root_url) + + # Check that url part after storage bucket is compliant _check_naming_is_compliant( - target_url.replace(target_root_dir + "/", ""), allow_dot=True + target_url.replace(tgt_bucket_root_url, ""), + allow_dot=True, + allow_slash=True, ) logger.debug("Target file: %s", target_url)