Skip to content

Commit

Permalink
[Fixes #12341] Assets: link generation (#12342)
Browse files Browse the repository at this point in the history
  • Loading branch information
etj authored Jun 20, 2024
1 parent d7127c1 commit 694a017
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion geonode/assets/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"))
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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}")

Expand Down

0 comments on commit 694a017

Please sign in to comment.