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

Add asset_manager_id to file attachments #2994

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

richardTowers
Copy link
Contributor

@richardTowers richardTowers commented Nov 21, 2024

Currently, the properties in the file attachment properties are insufficient to make a request to asset manager from the server to fetch the asset.

The API client wants to be called like this:

GdsApi.asset_manager.media(asset_maanger_ir, filename)

... but we don't have an asset_manager_ir, we only have url (which include the id we want), and a bunch of other fields which don't. We shouldn't have to parse URLs to find IDs.

We get away with this because in most cases where we're using file assets, we redirect the user to the full asset URL (so we don't need to fetch it on the server side).

The one example I'm aware of where we do need to request assets on the server side is CSV previews, and we've ended up with a pretty wild workaround in that case. Specifically, we route some requests to the assets domains (e.g. assets.publishing.service.gov.uk) to frontend. This way, the preview_url ends up being served by frontend itself. This means the URL gets parsed by Rails' routing logic, and we end up with the ID in a param which we can use to call asset manager: see https://github.com/alphagov/frontend/blob/8b075a3e26d775ad97b6b473ddaa22386e07cad2/app/controllers/csv_preview_controller.rb#L27

We'll need a corresponding commit in Whitehall (and maybe other publishing apps, if they have attachments?) to start sending this new field through to the API. It's optional, so this shouldn't break anything in the meantime.

EDIT: This description has been updated by @unoduetre to not include details which are no longer relevant.

⚠️ This repo is Continuously Deployed: make sure you follow the guidance ⚠️

richardTowers added a commit to alphagov/whitehall that referenced this pull request Nov 21, 2024
This needs to follow on from alphagov/publishing-api#2994

As described there, the lack of attachment_data_id in the data for file asset attachments in content store makes it hard to request the attachments on the server side. Instead, we have to redirect users to the assets, which has led to ugly workarounds like CSV previews being rendered by frontend but served on assets.publishing.service.gov.uk.

Adding attachment_data_id should allow the frontends to request attachments directly, using the API client:

    GdsApi.asset_manager.media(attachment_data_id, filename)

This should make it much more convenient to preview assets, or use them for other rendering purposes (e.g. showing a CSV as a line graph).
richardTowers added a commit to alphagov/whitehall that referenced this pull request Nov 27, 2024
This needs to follow on from alphagov/publishing-api#2994

As described there, the lack of attachment_data_id in the data for file asset attachments in content store makes it hard to request the attachments on the server side. Instead, we have to redirect users to the assets, which has led to ugly workarounds like CSV previews being rendered by frontend but served on assets.publishing.service.gov.uk.

Adding attachment_data_id should allow the frontends to request attachments directly, using the API client:

    GdsApi.asset_manager.media(attachment_data_id, filename)

This should make it much more convenient to preview assets, or use them for other rendering purposes (e.g. showing a CSV as a line graph).
Currently, the properties in the file attachment properties are
insufficient to make a request to asset manager from the server to fetch
the asset.

The API client wants to be called like this:

    GdsApi.asset_manager.media(asset_manager_id, filename)

... but we don't have an asset_manager_id, we only have url
(which include the id we want), and a bunch of other fields
which don't. We shouldn't have to parse URLs to find IDs.

We get away with this because in most cases where we're using file
assets, we redirect the user to the full asset URL (so we don't need to
fetch it on the server side).

The one example I'm aware of where we do need to request assets on the
server side is CSV previews, and we've ended up with a pretty wild
workaround in that case. Specifically, we route some requests to the
assets domains (e.g. assets.publishing.service.gov.uk) to frontend.
This way, the preview_url ends up being served by frontend itself. This
means the URL gets parsed by Rails' routing logic, and we end up with
the ID in a param which we can use to call asset manager: see
https://github.com/alphagov/frontend/blob/8b075a3e26d775ad97b6b473ddaa22386e07cad2/app/controllers/csv_preview_controller.rb#L27

We'll need a corresponding commit in Whitehall (and maybe other
publishing apps, if they have attachments?) to start sending this new
field through to the API. It's optional, so this shouldn't break
anything in the meantime.
@unoduetre unoduetre force-pushed the add-attachment-data-id branch from ffb8338 to f407252 Compare February 24, 2025 15:45
@unoduetre unoduetre force-pushed the add-attachment-data-id branch from f407252 to 43d53fb Compare February 24, 2025 16:19
@unoduetre unoduetre changed the title Add attachment_data_id to file attachments Add asset_manager_id to file attachments Feb 24, 2025
unoduetre pushed a commit to alphagov/whitehall that referenced this pull request Feb 24, 2025
This needs to follow on from alphagov/publishing-api#2994

As described there, the lack of asset_manager_id in the data for file asset attachments in content store makes it hard to request the attachments on the server side. Instead, we have to redirect users to the assets, which has led to ugly workarounds like CSV previews being rendered by frontend but served on assets.publishing.service.gov.uk.

Adding asset_manager_id should allow the frontends to request attachments directly, using the API client:

    GdsApi.asset_manager.media(asset_manager_id, filename)

This should make it much more convenient to preview assets.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants