Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions app/controllers/admin/edition_images_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,39 @@ def create
flash.now.notice = "Images successfully uploaded"
else
# Remove images from @edition.images array, otherwise the view will render it in the 'Uploaded images' list
@images.each { |image| @edition.images.delete(image) }
@images.each { |image| image.id.nil? && @edition.images.delete(image) }
end

render :index
end

def create_image(image)
new_image = @edition.images.build
existing_image = @edition.images.joins(:image_data).where(["image_data.carrierwave_image = ?", image["image_data"]["file"].original_filename]).first

new_image.build_image_data(image["image_data"])
if existing_image.present?
existing_image.image_data.validate_on_image = existing_image

new_image.image_data.validate_on_image = new_image
sanitized_file = CarrierWave::SanitizedFile.new(image["image_data"]["file"].tempfile)

# so that auth_bypass_id is discoverable by AssetManagerStorage
new_image.image_data.images << new_image
# Prevent the new file from being renamed to the temporary name
sanitized_file.move_to(File.join(File.dirname(sanitized_file.path), existing_image.image_data.carrierwave_image))

new_image
existing_image.image_data.file.store!(sanitized_file)

# To force user to supply a new crop for reuploaded image
existing_image.image_data.crop_data = nil

existing_image
else
new_image = @edition.images.build
new_image.build_image_data(image["image_data"])

new_image.image_data.validate_on_image = new_image

new_image.image_data.images << new_image

new_image
end
end

def edit
Expand Down
9 changes: 8 additions & 1 deletion app/models/image_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ def dimensions_from_config
self.width ||= image_kind_config.valid_width
end

def file_is_not_blank
errors.add(:file, :blank) if file.blank? && errors[:file].blank?
end

def filename_is_unique
return if validate_on_image.blank? || file.blank?

Expand All @@ -101,6 +105,9 @@ def filename_is_unique
end

def image_changed?
changes["carrierwave_image"].present?
# if the dimensions have changed then
# an image with the same name has been
# reuploaded and we need to revalidate
changes["carrierwave_image"].present? || changes["dimensions"].present?
end
end
21 changes: 21 additions & 0 deletions app/uploaders/image_uploader.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class ImageUploader < WhitehallUploader
include ImageValidator
include CarrierWave::MiniMagick
include CarrierWave::Uploader::Dimension

process :store_dimensions

Expand All @@ -20,6 +21,26 @@ def extension_allowlist
super + %w[svg]
end

def height_range
return unless bitmap?(file)

if model.respond_to?(:image_kind_config)
model.image_kind_config.valid_height..
else
0..
end
end

def width_range
return unless bitmap?(file)

if model.respond_to?(:image_kind_config)
model.image_kind_config.valid_width..
else
0..
end
end

def store_dimensions
if file && bitmap?(file) && model
begin
Expand Down
8 changes: 0 additions & 8 deletions features/edition-images-javascript.feature
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,3 @@ Feature: Images tab on edit edition
When I click to edit the details of the image that needs to be cropped
When I update the image details and save
Then I should not see that the image requires cropping

@javascript
Scenario: Uploading a file with a duplicated filename
When a draft document with images exists
And I visit the images tab of the document with images
And I upload a 960x960 image
And I upload a 960x960 image
Then I should get 1 error message
12 changes: 0 additions & 12 deletions test/functional/admin/edition_images_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,6 @@ class Admin::EditionImagesControllerTest < ActionController::TestCase
assert_select ".govuk-error-summary li", "Image data file is too small. Select an image that is at least 960 pixels wide and at least 640 pixels tall"
end

view_test "#create shows a validation error if image has a duplicated filename" do
login_authorised_user
edition = create(:news_article)
file = upload_fixture("images/960x640_gif.gif")
create(:image, edition:, image_data: build(:image_data, file:))

post :create, params: { edition_id: edition.id, images: [{ image_data: { file: } }] }

assert_template "admin/edition_images/index"
assert_select ".govuk-error-summary li", "Image data file name is not unique. All your file names must be different. Do not use special characters to create another version of the same file name."
end

test "POST :create triggers a job be queued to store image and variants in Asset Manager" do
login_authorised_user

Expand Down