Skip to content

Commit

Permalink
param validators for encode processor
Browse files Browse the repository at this point in the history
  • Loading branch information
markevans committed May 15, 2021
1 parent 99c6784 commit bf14fb1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/dragonfly/image_magick/processors/encode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,23 @@ module Dragonfly
module ImageMagick
module Processors
class Encode
include ParamValidators

WHITELISTED_ARGS = %w(quality)

IS_IN_WHITELISTED_ARGS = ->(args_string) {
args_string.scan(/-\w+/).all? { |arg|
WHITELISTED_ARGS.include?(arg.sub("-", ""))
}
}

def update_url(attrs, format, args = "")
attrs.ext = format.to_s
end

def call(content, format, args = "")
validate!(format, &is_word)
validate!(args, &IS_IN_WHITELISTED_ARGS)
Commands.convert(content, args, "format" => format)
end
end
Expand Down
30 changes: 30 additions & 0 deletions spec/dragonfly/image_magick/processors/encode_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require "spec_helper"

describe Dragonfly::ImageMagick::Processors::Encode do
let (:app) { test_imagemagick_app }
let (:image) { Dragonfly::Content.new(app, SAMPLES_DIR.join("beach.png")) } # 280x355
let (:processor) { Dragonfly::ImageMagick::Processors::Encode.new }

it "encodes to a different format" do
processor.call(image, "jpeg")
image.should have_format("jpeg")
end

describe "param validations" do
it "validates the format param" do
expect {
processor.call(image, "jpeg -write bad.png")
}.to raise_error(Dragonfly::ParamValidators::InvalidParameter)
end

it "allows good args" do
processor.call(image, "jpeg", "-quality 10")
end

it "disallows bad args" do
expect {
processor.call(image, "jpeg", "-write bad.png")
}.to raise_error(Dragonfly::ParamValidators::InvalidParameter)
end
end
end

0 comments on commit bf14fb1

Please sign in to comment.