Skip to content

Commit 3173a94

Browse files
crcommonsrjmackay
authored andcommitted
Encode image URLs properly even when using the CDN #1635
1 parent cbb5698 commit 3173a94

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

application/classes/Ushahidi/Formatter/Media.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ protected function add_metadata(Array $data, Entity $media)
4040

4141
protected function get_field_name($field)
4242
{
43+
4344
$remap = [
4445
'o_filename' => 'original_file_url',
4546
'o_size' => 'original_file_size',
@@ -55,11 +56,20 @@ protected function get_field_name($field)
5556
}
5657

5758
protected function format_o_filename($value)
58-
{
59+
{
60+
5961
if ($cdnBaseUrl = Kohana::$config->load('cdn.baseurl')) {
60-
return $cdnBaseUrl . $value;
62+
63+
//removes path from image file name, encodes the filename, and joins the path and filename together
64+
$url_path = explode("/", $value);
65+
$filename = rawurlencode(array_pop($url_path));
66+
array_push($url_path, $filename);
67+
return $cdnBaseUrl . implode("/", $url_path);
68+
6169
} else {
62-
return URL::site(Media::uri($this->get_relative_path() . $value), Request::current());
70+
71+
//URL::site / Mesdia::uri encodes the image path properly
72+
return URL::site((Media::uri($this->get_relative_path() . $value)), Request::current());
6373
}
6474
}
6575

@@ -78,6 +88,7 @@ private function get_relative_path()
7888
*/
7989
private function resized_url($width, $height, $filename)
8090
{
91+
8192
// Format demensions appropriately depending on the value of the height
8293
if ($height != NULL)
8394
{

tests/datasets/ushahidi/Base.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1644,6 +1644,17 @@ media:
16441644
o_size: 1024
16451645
created: 1381815821
16461646
updated: 1381815819
1647+
-
1648+
id: 4
1649+
user_id: 1
1650+
caption: "ihub"
1651+
mime: "image/jpeg"
1652+
o_filename: "some junk name.jpg"
1653+
o_width: 400
1654+
o_height: 500
1655+
o_size: 1024
1656+
created: 1381815821
1657+
updated: 1381815819
16471658
config:
16481659
-
16491660
group_name: site

tests/integration/media.feature

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ Feature: Testing the Media API
8585
Then the response is JSON
8686
And the response has a "count" property
8787
And the type of the "count" property is "numeric"
88-
And the "count" property equals "3"
88+
And the "count" property equals "4"
8989
Then the guzzle status code should be 200
9090

9191
@resetFixture
@@ -124,3 +124,14 @@ Feature: Testing the Media API
124124
#Then the "errors.1.message" property equals "File type not supported. Please upload an image file."
125125
Then the "errors.1.message" property equals "The file size should be less than 1 MB"
126126
Then the guzzle status code should be 422
127+
128+
@cdnEnabled
129+
Scenario: Media URLs are encoded correctly
130+
Given that I want to find a "Media"
131+
And that its "id" is "4"
132+
When I request "/media"
133+
Then the response is JSON
134+
And the response has a "original_file_url" property
135+
And the type of the "original_file_url" property is "string"
136+
And the "original_file_url" property contains "/some%20junk%20name.jpg"
137+
Then the guzzle status code should be 200

0 commit comments

Comments
 (0)