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 attachment_data_id to file attachments #9641

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

richardTowers
Copy link
Contributor

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).

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

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).
def attachment_data_id
return unless csv? && attachable.is_a?(Edition) && attachment_data.all_asset_variants_uploaded?

attachment_data.id
Copy link
Contributor

@catalinailie catalinailie Feb 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure attachment_data.id is the ID we want. GdsApi.asset_manager expects the asset's ID, and that one, in whitehall, is Asset.asset_manager_id.

I think this is the reason why we have this scenario with preview_url formed in whitehall using attachment_data.id and then overwritten in government-frontend with the URL that has this asset_manager_id in it (formed here I think, but I might be wrong).

Copy link
Contributor

@unoduetre unoduetre Feb 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you're right. It should be asset_manager_id of the relevant asset (an object of the Asset class). The issue is that there can be more than one asset per attachment (an object of the Attachment class). (The path to assets is attachment.attachment_data.assets) But the solution is to get the asset with the variant "original", which means the original file as uploaded. The combination of assetable_id, assetable_type and variant is unique (there is a validation for that). assetable_type in this case is "AttachmentData", assetable_id is the id of the object of the AttachmentData class (the id from the database). And the variant should be specified as "original". In this way we can get the unique asset_manager_id.

There is a method called get_asset in AssetManagerStorage but it's currently private. But we can make it public. asset_manager_id can be retrieved this way: attachment.attachment_data.file.file.send(:get_asset).asset_manager_id
Of course in the final solution we should create a couple more methods, so there are no deep dependencies present in the final code.

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.

3 participants