Skip to content

Commit

Permalink
Allow upload of files with square brackets (#30)
Browse files Browse the repository at this point in the history
Close #1
  • Loading branch information
tagliala authored Jan 20, 2025
1 parent 7fe8339 commit 3f492f2
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 37 deletions.
27 changes: 8 additions & 19 deletions lib/colore/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,6 @@ def self.generate_doc_id
SecureRandom.uuid
end

# Returns the URI parser based on the Ruby version.
#
# If `URI::RFC2396_PARSER` is defined, it will return that parser.
# Otherwise, it will return `URI::DEFAULT_PARSER`.
#
# @return [URI::Parser] the URI parser to use
def self.uri_parser
@uri_parser ||=
if defined?(URI::RFC2396_PARSER)
URI::RFC2396_PARSER
else
URI::DEFAULT_PARSER
end
end

# Constructor.
#
# @param base_uri [String] The base URI of the Colore service that you wish to attach to
Expand Down Expand Up @@ -97,7 +82,7 @@ def create_document(doc_id:, filename:, content:, title: nil, author: nil, actio
response = nil
with_tempfile(content) do |io|
params[:file] = file_param(io)
response = send_request :put, "#{url_for_base doc_id}/#{base_filename}", params, :json
response = send_request :put, "#{url_for_base doc_id}/#{encode_param(base_filename)}", params, :json
end
response
end
Expand Down Expand Up @@ -129,10 +114,10 @@ def update_document(doc_id:, filename:, content: nil, author: nil, actions: nil,
if content
with_tempfile(content) do |io|
params[:file] = file_param(io)
response = send_request :post, "#{url_for_base(doc_id)}/#{base_filename}", params, :json
response = send_request :post, "#{url_for_base(doc_id)}/#{encode_param(base_filename)}", params, :json
end
else
response = send_request :post, "#{url_for_base(doc_id)}/#{base_filename}", params, :json
response = send_request :post, "#{url_for_base(doc_id)}/#{encode_param(base_filename)}", params, :json
end
response
end
Expand All @@ -144,7 +129,7 @@ def update_document(doc_id:, filename:, content: nil, author: nil, actions: nil,
#
# @return [Hash] a standard response
def update_title(doc_id:, title:)
send_request :post, "#{url_for_base(doc_id)}/title/#{self.class.uri_parser.escape title}", {}, :json
send_request :post, "#{url_for_base(doc_id)}/title/#{encode_param(title)}", {}, :json
end

# Requests a conversion of an existing document.
Expand Down Expand Up @@ -314,5 +299,9 @@ def with_tempfile(content)
def file_param(io)
Faraday::Multipart::FilePart.new(io, Marcel::MimeType.for(io))
end

def encode_param(param)
URI.encode_www_form_component(param).gsub('+', '%20')
end
end
end

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 3f492f2

Please sign in to comment.