Skip to content
Merged
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
20 changes: 16 additions & 4 deletions lib/api_2captcha/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -227,16 +227,24 @@ def file_params(params)
image = params.delete("image")
hint_image = params.delete("hint_image")

image_content = get_image_content(image)
hint_image_content = get_image_content(hint_image) if hint_image
image_content = if base64_encoded?(image)
image
else
Base64.strict_encode64(get_image_content(image))
end

hint_image_content = if hint_image
base64_encoded?(hint_image) ? hint_image : Base64.strict_encode64(get_image_content(hint_image))
end

result_params = {
"method" => "base64",
"body" => Base64.strict_encode64(image_content),
"body" => image_content,
"filename" => File.basename(image),
"ext" => File.extname(image).delete(".")
}

result_params["imginstructions"] = Base64.strict_encode64(hint_image_content) if hint_image_content
result_params["imginstructions"] = hint_image_content if hint_image_content
params.merge(result_params)
end

Expand All @@ -246,6 +254,10 @@ def get_image_content(image)
image
end

def base64_encoded?(string)
string.is_a?(String) && string.match(/\A[A-Za-z0-9+\/=]+\z/) && (string.length % 4).zero?
end

def download_image(url)
response = URI.open(url)
if response.status[0] != '200'
Expand Down