-
Notifications
You must be signed in to change notification settings - Fork 196
Bulk Uploading Edition Images [WHIT-1812][WHIT-2434] #10625
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
Conversation
accc163 to
e496119
Compare
9b3aad3 to
fa4e301
Compare
c1ce4da to
016aa8a
Compare
1470600 to
32dfabf
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is looking really good now. I think the controller code could be a little bit tidier, but to be honest if it merged as-is I wouldn't have major concerns.
app/models/concerns/replaceable.rb
Outdated
| end | ||
|
|
||
| def replace_with!(replacement) | ||
| # NOTE: we're doing this manually because carrierwave is setup such |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I reckon you can probably drop this comment, because Asset Manager handles virus checking these days, not Whitehall
| else | ||
| invalid_image = @edition.images.build | ||
| invalid_image.build_image_data({}) | ||
| [invalid_image] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apologies if I should have caught this on the first review, but this building an invalid image seems a bit weird. I can kind of see why we'd do it to save ourselves checking if the @images value is set but I think to be honest we'd be better off just doing the null checks.
Or perhaps empty checks - would @images = images_params.map { |image| create_image(image) } work?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a bit strange looking but it's to avoid having to make any changes to the ErrorSummaryComponent in this PR. Happy to make a follow-up PR in which I make those changes.
| # Remove @new_image from @edition.images array, otherwise the view will render it in the 'Uploaded images' list | ||
| @edition.images.delete(@new_image) | ||
| render :index | ||
| flash[:notice] = nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be nil by default, shouldn't it?
|
|
||
| new_image.image_data.validate_on_image = new_image | ||
|
|
||
| new_image.image_data.images << new_image |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think you should need this line, the association keys should be set automatically when you save the image on line 51
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems we do need it otherwise the comment is correct, AssetManagerStorage isn't able to get the auth_bypass_id.
| require "timeout" | ||
|
|
||
| class AttachmentData < ApplicationRecord | ||
| include Replaceable |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice to be able to share this behaviour across "data" classes, good work
20e5d29 to
a9ca8ed
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good! I have every confidence this is an improvement on the current code and I wouldn't block merging it.
But where did we arrive at with the decision around overwriting files of the same name? I thought we'd said we should raise a validation error in that scenario, forcing the publisher to delete the original if they want to upload a new file of the same name.
| def change | ||
| change_table :image_data, bulk: true do |t| | ||
| t.json "dimensions" | ||
| t.json "crop_data" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Strictly speaking I'd move this into its own, earlier, commit. (Not entirely sure of the sequencing of db migrate vs pod deployment, if we roll out both the DB changes and the code that references the new fields, in the same PR. Don't we need to ship the DB migration first as a separate PR?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I wasn't sure about what to do with regards to the migration. What you're saying about the separate PR for the migrations makes sense.
| config.storage = Storage::PreviewableStorage | ||
| end | ||
|
|
||
| def downloader |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where is this called?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is overloading the base downloader from CarrierWave::Uploader::Base. We need to override skip_ssrf_protection, without doing this an error will occur when EditionImagesController calls download! in test and development environments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can put a comment saying this in the image_uploader if that makes it clearer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, thanks!
| attach_file Rails.root.join("test/fixtures/horrible-image.64x96.jpg") | ||
| elsif width == 960 && height == 960 | ||
| attach_file Rails.root.join("test/fixtures/images/960x960_jpeg.jpg") | ||
| if running_javascript? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
Yep, that's what happens now! The overwriting files functionality was in a different commit, so I just dropped it. |
6d7590a to
9df74d9
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👍 nice catch
Allows images that are too big to be saved. When this happens the user will be asked to crop the image on the `index` page. This change means that images can be recropped, the cropping data can be used in other contexts and multiple images can be uploaded without validation errors if all/some of them are too big.
Users can now bulk upload images in a similar way to file attachments. The JS enhanced version of the `FileUpload` component is now used and the `EditionImagesController` has been changed to use an array of `images` for `create` instead of a single image.
As we save processed images and original images separately now, we need to ensure that the processed images are referenced by presenters instead of the original images. If we don't do this, then uncropped images will be used for embeddable images. To this end, `embed_version` has been added to `ImageKindConfig` and `embed_url` has been added to `Image`. If `embed_version` is defined, then the version specified will be used to generate the image url. Otherwise, the original url will returned. Tests referencing `image.url` within `govspeak` have been updated.
9df74d9 to
3b74dbb
Compare
What
crop_datainstead of transforming the uploaded image on saveembed_url(if specified withinImageConfig) withingovspeak_helperImageDatachangesDimensionsstruct and replace with calls todimensionsImageUploaderchangesImageCropperchangesImageComponentchangesEditionImagesControllerchangescropview, instead redirect to index on successful uploadImageCroppercomponent toedit(if image can be cropped)GovspeakHelperchangesembed_urlinstead ofurlfor eachimage(this is so the cropped version of images actually gets used)Visual Differences
The user is no longer redirected to
editon successful image upload. If the images uploaded require cropping then the user is prompted to crop the image inedit.Why
The advantages of having the cropping information be saved separately from the image is that:
This application is owned by the Whitehall Experience team. Please let us know in #govuk-whitehall-experience-tech when you raise any PRs.
Follow these steps if you are doing a Rails upgrade.