docbrown/app/services/images/optimizer.rb
Jeremy Friesen 5534a8fa18
Addressing rubocop violations (#16156)
```shell
❯ bundle exec rubocop -A
Inspecting 1856 files

Offenses:

app/controllers/admin/settings/mandatory_settings_controller.rb:17:57: C: [Corrected] Rails/CompactBlank: Use compact_blank instead.
            settings_model.public_send("#{key}=", value.reject(&:blank?)) if value.present?
                                                        ^^^^^^^^^^^^^^^^
app/controllers/users_controller.rb:66:58: C: [Corrected] Rails/CompactBlank: Use compact_blank instead.
      Honeycomb.add_field("error", @user.errors.messages.reject { |_, v| v.empty? })
                                                         ^^^^^^^^^^^^^^^^^^^^^^^^^^
app/controllers/users_controller.rb:280:58: C: [Corrected] Rails/CompactBlank: Use compact_blank instead.
      Honeycomb.add_field("error", @user.errors.messages.reject { |_, v| v.empty? })
                                                         ^^^^^^^^^^^^^^^^^^^^^^^^^^
app/models/settings/base.rb:111:54: C: [Corrected] Rails/CompactBlank: Use compact_blank instead.
          value.split(separator || SEPARATOR_REGEXP).reject(&:empty?).map(&:strip)
                                                     ^^^^^^^^^^^^^^^^
app/services/articles/feeds/weighted_query_strategy.rb:269:121: C: Layout/LineLength: Line is too long. [126/120] (https://rubystyle.guide#max-line-length)
      def initialize(user: nil, number_of_articles: 50, page: 1, tag: nil, strategy: AbExperiment::ORIGINAL_VARIANT, **config)
                                                                                                                        ^^^^^^
app/services/images/optimizer.rb:27:50: C: [Corrected] Rails/CompactBlank: Use compact_blank instead.
      options = DEFAULT_CL_OPTIONS.merge(kwargs).reject { |_, v| v.blank? }
                                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^
app/services/images/optimizer.rb:46:68: C: [Corrected] Rails/CompactBlank: Use compact_blank instead.
      options = DEFAULT_IMGPROXY_OPTIONS.merge(translated_options).reject { |_, v| v.blank? }
                                                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^
app/services/settings/upsert.rb:30:55: C: [Corrected] Rails/CompactBlank: Use compact_blank instead.
          settings_class.public_send("#{key}=", value.reject(&:blank?))
                                                      ^^^^^^^^^^^^^^^^

1856 files inspected, 8 offenses detected, 7 offenses corrected
```

After this commit:

```shell
❯ bundle exec rubocop
Inspecting 1856 files

1856 files inspected, no offenses detected
```

1856 files inspected, no offenses detected
2022-01-17 17:21:06 -05:00

85 lines
2.2 KiB
Ruby

module Images
module Optimizer
def self.call(img_src, **kwargs)
return img_src if img_src.blank? || img_src.starts_with?("/")
if imgproxy_enabled?
imgproxy(img_src, **kwargs)
elsif cloudinary_enabled?
cloudinary(img_src, **kwargs)
else
img_src
end
end
DEFAULT_CL_OPTIONS = {
type: "fetch",
height: nil,
width: nil,
crop: "limit",
quality: "auto",
flags: "progressive",
fetch_format: "auto",
sign_url: true
}.freeze
def self.cloudinary(img_src, **kwargs)
options = DEFAULT_CL_OPTIONS.merge(kwargs).compact_blank
if img_src&.include?(".gif")
options[:quality] = 66
end
ActionController::Base.helpers.cl_image_path(img_src, options)
end
DEFAULT_IMGPROXY_OPTIONS = {
height: nil,
width: nil,
max_bytes: 500_000, # Keep everything under half of one MB.
auto_rotate: true,
resizing_type: nil
}.freeze
def self.imgproxy(img_src, **kwargs)
translated_options = translate_cloudinary_options(kwargs)
options = DEFAULT_IMGPROXY_OPTIONS.merge(translated_options).compact_blank
Imgproxy.config.endpoint ||= get_imgproxy_endpoint
Imgproxy.url_for(img_src, options)
end
def self.translate_cloudinary_options(options)
if options[:crop] == "fill"
options[:resizing_type] = "fill"
end
options[:crop] = nil
options[:fetch_format] = nil
options
end
def self.imgproxy_enabled?
Imgproxy.config.key.present? && Imgproxy.config.salt.present?
end
def self.cloudinary_enabled?
config = Cloudinary.config
config.cloud_name.present? && config.api_key.present? && config.api_secret.present?
end
def self.get_imgproxy_endpoint
if Rails.env.production?
# Use /images with the same domain on Production as
# our default configuration
URL.url("images")
# ie. https://forem.dev/images
else
# On other environments, rely on ApplicationConfig for a
# more flexible configuration
# ie. default imgproxy endpoint is localhost:8080
ApplicationConfig["IMGPROXY_ENDPOINT"] || "http://localhost:8080"
end
end
end
end