diff --git a/core/app/models/concerns/spree/active_storage_adapter/attachment.rb b/core/app/models/concerns/spree/active_storage_adapter/attachment.rb index 2b9f6b16d9a..d78587cea70 100644 --- a/core/app/models/concerns/spree/active_storage_adapter/attachment.rb +++ b/core/app/models/concerns/spree/active_storage_adapter/attachment.rb @@ -9,11 +9,9 @@ module ActiveStorageAdapter class Attachment delegate_missing_to :@attachment - DEFAULT_SIZE = '100%' - def initialize(attachment, styles: {}) @attachment = attachment - @styles = styles + @styles = normalize_styles(styles) end def exists? @@ -21,20 +19,18 @@ def exists? end def filename - blob.filename.to_s + blob&.filename.to_s end def url(style = nil) - variant(style).url + variant(style)&.url end def variant(style = nil) - size = style_to_size(style&.to_sym) + size = style_to_size(style) @attachment.variant( - resize: size, - strip: true, - 'auto-orient': true, - colorspace: 'sRGB', + resize_to_limit: size, + strip: true ).processed end @@ -61,8 +57,12 @@ def metadata @attachment.metadata end + def normalize_styles(styles) + styles.transform_values { |v| v.split('x') } + end + def style_to_size(style) - @styles.fetch(style) { DEFAULT_SIZE } + @styles.fetch(style&.to_sym) { [width, height] } end end end