Skip to content

Commit 7e7f529

Browse files
author
Jamie Hannaford
committed
Merge pull request rackspace#595 from jamiehannaford/tempurl-fix
Provide the option to override inherited URL types for temp URLs
2 parents 56dd9b1 + a488c83 commit 7e7f529

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

lib/OpenCloud/ObjectStore/Resource/DataObject.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -451,14 +451,18 @@ public function createSymlinkFrom($source)
451451
*
452452
* @link http://docs.rackspace.com/files/api/v1/cf-devguide/content/TempURL-d1a4450.html
453453
*
454-
* @param $expires Expiration time in seconds
455-
* @param $method What method can use this URL? (`GET' or `PUT')
454+
* @param int $expires Expiration time in seconds
455+
* @param string $method What method can use this URL? (`GET' or `PUT')
456+
* @param bool $forcePublicUrl If set to TRUE, a public URL will always be used. The default is to use whatever
457+
* URL type the user has set for the main service.
458+
*
456459
* @return string
460+
*
457461
* @throws \OpenCloud\Common\Exceptions\InvalidArgumentError
458462
* @throws \OpenCloud\Common\Exceptions\ObjectError
459463
*
460464
*/
461-
public function getTemporaryUrl($expires, $method)
465+
public function getTemporaryUrl($expires, $method, $forcePublicUrl = false)
462466
{
463467
$method = strtoupper($method);
464468
$expiry = time() + (int) $expires;
@@ -477,7 +481,7 @@ public function getTemporaryUrl($expires, $method)
477481
}
478482
// @codeCoverageIgnoreEnd
479483

480-
$url = $this->getUrl();
484+
$url = ($forcePublicUrl === true) ? $this->getService()->getEndpoint()->getPublicUrl() : $this->getUrl();
481485
$urlPath = urldecode($url->getPath());
482486
$body = sprintf("%s\n%d\n%s", $method, $expiry, $urlPath);
483487
$hash = hash_hmac('sha1', $body, $secret);

tests/OpenCloud/Tests/ObjectStore/Resource/DataObjectTest.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,47 @@ public function test_Copy()
7272
}
7373

7474
/**
75-
* @expectedException OpenCloud\Common\Exceptions\NoNameError
75+
* @expectedException \OpenCloud\Common\Exceptions\NoNameError
7676
*/
7777
public function test_Copy_Fails()
7878
{
7979
$this->container->dataObject()->copy(null);
8080
}
8181

8282
/**
83-
* @expectedException OpenCloud\Common\Exceptions\InvalidArgumentError
83+
* @expectedException \OpenCloud\Common\Exceptions\InvalidArgumentError
8484
*/
8585
public function test_Temp_Url_Fails_With_Incorrect_Method()
8686
{
8787
$this->container->dataObject('foobar')->getTemporaryUrl(1000, 'DELETE');
8888
}
8989

90+
public function test_Temp_Url_Inherits_Url_Type()
91+
{
92+
$service = $this->getClient()->objectStoreService(null, 'IAD', 'internalURL');
93+
$object = $service->getContainer('foo')->dataObject('bar');
94+
95+
$this->addMockSubscriber(new Response(204, ['X-Account-Meta-Temp-URL-Key' => 'secret']));
96+
97+
$tempUrl = $object->getTemporaryUrl(60, 'GET');
98+
99+
// Check that internal URLs are used
100+
$this->assertContains('snet-storage', $tempUrl);
101+
}
102+
103+
public function test_temp_urls_can_be_forced_to_use_public_urls()
104+
{
105+
$service = $this->getClient()->objectStoreService(null, 'IAD', 'internalURL');
106+
$object = $service->getContainer('foo')->dataObject('bar');
107+
108+
$this->addMockSubscriber(new Response(204, ['X-Account-Meta-Temp-URL-Key' => 'secret']));
109+
110+
$tempUrl = $object->getTemporaryUrl(60, 'GET', true);
111+
112+
// Check that internal URLs are NOT used
113+
$this->assertNotContains('snet-storage', $tempUrl);
114+
}
115+
90116
public function test_Purge()
91117
{
92118
$object = $this->container->dataObject('foobar');

0 commit comments

Comments
 (0)