From 73ad0d8d9920eacbf9c90aedc1ea7ce3738eb4b5 Mon Sep 17 00:00:00 2001 From: etj Date: Wed, 19 Jun 2024 12:16:14 +0200 Subject: [PATCH] [Fixes #12341] Assets: link generation --- geonode/assets/local.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/geonode/assets/local.py b/geonode/assets/local.py index e22a5d7cdf9..4a82a6d4c7a 100644 --- a/geonode/assets/local.py +++ b/geonode/assets/local.py @@ -20,7 +20,22 @@ ) +class DefaultLocalLinkUrlHandler: + def get_link_url(self, asset: LocalAsset): + return build_absolute_uri(reverse("assets-link", args=(asset.pk,))) + + +class IndexLocalLinkUrlHandler: + def get_link_url(self, asset: LocalAsset): + return build_absolute_uri(reverse("assets-link", args=(asset.pk,))) + f"/{os.path.basename(asset.location[0])}" + + class LocalAssetHandler(AssetHandlerInterface): + + link_url_handlers = { + # 'sample_index_type': IndexLocalLinkUrlHandler() + } + @staticmethod def handled_asset_class(): return LocalAsset @@ -31,6 +46,9 @@ def get_download_handler(self, asset=None): def get_storage_manager(self, asset): return _asset_storage_manager + def get_link_url_handler(self, asset): + return self.link_url_handlers.get(asset.type, None) or DefaultLocalLinkUrlHandler() + def _create_asset_dir(self): return os.path.normpath( mkdtemp(dir=settings.ASSETS_ROOT, prefix=datetime.datetime.now().strftime("%Y%m%d%H%M%S")) @@ -126,7 +144,7 @@ def create_download_url(self, asset) -> str: return build_absolute_uri(reverse("assets-download", args=(asset.pk,))) def create_link_url(self, asset) -> str: - return build_absolute_uri(reverse("assets-link", args=(asset.pk,))) + f"/{os.path.basename(asset.location[0])}" + return self.get_link_url_handler(asset).get_link_url(asset) @classmethod def _is_file_managed(cls, file) -> bool: @@ -175,6 +193,10 @@ def _get_managed_dir(cls, asset): base_common = base managed_dir = os.path.join(assets_root, base_common) + + if not os.path.exists(managed_dir): + raise ValueError(f"Common dir '{managed_dir}' does not exist - Asset {asset.pk}") + if not os.path.isdir(managed_dir): raise ValueError(f"Common dir '{managed_dir}' does not seem to be a directory - Asset {asset.pk}")