Skip to content

Bad URI when a file name has brackets #35

@DmitryBarskov

Description

@DmitryBarskov

When I try to add a watermark to a file with [ or ] in its name, I get #<URI::InvalidURIError: bad URI (is not URI?): "https://v2.convertapi.com/d/p2bempn0sjua63w36zyxjr2d2weq7o34/[dev]+sample.pdf">

ConvertApi.convert(
  "watermark-overlay",
  {
    File: "./[dev] sample.pdf",
    OverlayFile: ConvertApi::UploadIO.new(File.open("some-other.pdf")),
    Scale: "75",
    Opacity: "30",
    Style: "stamp"
  },
  from_format: "pdf"
).file.save("./sample.watermarked.pdf")
#<URI::InvalidURIError: bad URI (is not URI?): "https://v2.convertapi.com/d/p2bempn0sjua63w36zyxjr2d2weq7o34/[dev]+sample.pdf">

If I try to do it without saving, it will be OK, but the URI provided is not a valid URI nor URL:

ConvertApi.convert(
  "watermark-overlay",
  {
    File: "./[dev] sample.pdf",
    OverlayFile: ConvertApi::UploadIO.new(File.open(SaasAppSettings.instance.watermark_file_path)),
    Scale: "75",
    Opacity: "30",
    Style: "stamp"
  },
  from_format: "pdf"
).file

#<ConvertApi::ResultFile:0x0000ffff94106a28
# @info=
#  {"FileName"=>"[dev] sample.pdf",
#   "FileExt"=>"pdf",
#   "FileSize"=>121463,
#   "FileId"=>"y0xnqdx161tcmbghnf0x3yjniih0ckek",
#   "Url"=>"https://v2.convertapi.com/d/y0xnqdx161tcmbghnf0x3yjniih0ckek/[dev]%20sample.pdf"}> <= invalid!
# but it should be 'https://v2.convertapi.com/d/f82j2d0chmxv4xqqa45s3kql613al2aj/%5Bdev%5D+sample.pdf'

So I think it should be fixed on the server. The workaround I found is to patch the ConvertApi::ResultFile:

require "addressable/uri"

module ConvertApi
  class ResultFile
    def url
      Addressable::URI.parse(info["Url"]).normalize.to_s
    end
  end
end

which requires this gem https://github.com/sporkmonger/addressable.

Ruby version 3.2.9 (but I think it does not depend)
Library version 2.0.0 or 3.0.0 (tried on both, but I think it does not depend on library version, since the bug is in the API)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions