Skip to content

Commit c6aa42b

Browse files
nadavofitocker
authored andcommitted
Support = and | characters within context values using escaping + test (#143)
1 parent 601604f commit c6aa42b

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

src/uploader.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ call_context_api = (context, command, public_ids = [], callback, options = {}) -
211211
type: options.type
212212
}
213213
if context?
214-
params.context = utils.encode_key_value(context)
214+
params.context = utils.encode_context(context)
215215
return [params]
216216

217217
call_api = (action, callback, options, get_params) ->

src/utils.coffee

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,15 @@ exports.encode_key_value = (arg) ->
250250
else
251251
arg
252252

253+
exports.encode_context = (arg) ->
254+
if _.isObject(arg)
255+
pairs = for k, v of arg
256+
v = v.replace /([=|])/g, (match)-> "\\#{match}"
257+
"#{k}=#{v}"
258+
pairs.join("|")
259+
else
260+
arg
261+
253262
exports.build_eager = (transformations) ->
254263
(for transformation in utils.build_array(transformations)
255264
transformation = _.clone(transformation)
@@ -415,7 +424,7 @@ exports.updateable_resource_params = (options, params = {}) ->
415424
params.auto_tagging = options.auto_tagging if options.auto_tagging?
416425
params.background_removal = options.background_removal if options.background_removal?
417426
params.categorization = options.categorization if options.categorization?
418-
params.context = utils.encode_key_value(options.context) if options.context?
427+
params.context = utils.encode_context(options.context) if options.context?
419428
params.custom_coordinates = utils.encode_double_array(options.custom_coordinates) if options.custom_coordinates?
420429
params.detection = options.detection if options.detection?
421430
params.face_coordinates = utils.encode_double_array(options.face_coordinates) if options.face_coordinates?

test/uploader_spec.coffee

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,15 @@ describe "uploader", ->
251251
expect(r1.context).to.be undefined
252252
done()
253253

254+
it "should upload with context containing reserved characters", (done)->
255+
context = {key1: 'value1', key2: 'valu\e2', key3: 'val=u|e3', key4: 'val\=ue'}
256+
cloudinary.v2.uploader.upload IMAGE_FILE, context: context, (error, result) ->
257+
cloudinary.v2.api.resource result.public_id, context: true, (error, result) ->
258+
expect(result.context.custom).to.eql(context)
259+
done()
260+
true
261+
true
262+
254263
it "should support timeouts", (done) ->
255264
# testing a 1ms timeout, nobody is that fast.
256265
cloudinary.v2.uploader.upload "http://cloudinary.com/images/old_logo.png", timeout: 1, tags: UPLOAD_TAGS, (error, result) ->

0 commit comments

Comments
 (0)