Skip to content

Commit

Permalink
Merge pull request #595 from jamiehannaford/tempurl-fix
Browse files Browse the repository at this point in the history
Provide the option to override inherited URL types for temp URLs
  • Loading branch information
Jamie Hannaford committed May 7, 2015
2 parents 56dd9b1 + a488c83 commit 7e7f529
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
12 changes: 8 additions & 4 deletions lib/OpenCloud/ObjectStore/Resource/DataObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -451,14 +451,18 @@ public function createSymlinkFrom($source)
*
* @link http://docs.rackspace.com/files/api/v1/cf-devguide/content/TempURL-d1a4450.html
*
* @param $expires Expiration time in seconds
* @param $method What method can use this URL? (`GET' or `PUT')
* @param int $expires Expiration time in seconds
* @param string $method What method can use this URL? (`GET' or `PUT')
* @param bool $forcePublicUrl If set to TRUE, a public URL will always be used. The default is to use whatever
* URL type the user has set for the main service.
*
* @return string
*
* @throws \OpenCloud\Common\Exceptions\InvalidArgumentError
* @throws \OpenCloud\Common\Exceptions\ObjectError
*
*/
public function getTemporaryUrl($expires, $method)
public function getTemporaryUrl($expires, $method, $forcePublicUrl = false)
{
$method = strtoupper($method);
$expiry = time() + (int) $expires;
Expand All @@ -477,7 +481,7 @@ public function getTemporaryUrl($expires, $method)
}
// @codeCoverageIgnoreEnd

$url = $this->getUrl();
$url = ($forcePublicUrl === true) ? $this->getService()->getEndpoint()->getPublicUrl() : $this->getUrl();
$urlPath = urldecode($url->getPath());
$body = sprintf("%s\n%d\n%s", $method, $expiry, $urlPath);
$hash = hash_hmac('sha1', $body, $secret);
Expand Down
30 changes: 28 additions & 2 deletions tests/OpenCloud/Tests/ObjectStore/Resource/DataObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,47 @@ public function test_Copy()
}

/**
* @expectedException OpenCloud\Common\Exceptions\NoNameError
* @expectedException \OpenCloud\Common\Exceptions\NoNameError
*/
public function test_Copy_Fails()
{
$this->container->dataObject()->copy(null);
}

/**
* @expectedException OpenCloud\Common\Exceptions\InvalidArgumentError
* @expectedException \OpenCloud\Common\Exceptions\InvalidArgumentError
*/
public function test_Temp_Url_Fails_With_Incorrect_Method()
{
$this->container->dataObject('foobar')->getTemporaryUrl(1000, 'DELETE');
}

public function test_Temp_Url_Inherits_Url_Type()
{
$service = $this->getClient()->objectStoreService(null, 'IAD', 'internalURL');
$object = $service->getContainer('foo')->dataObject('bar');

$this->addMockSubscriber(new Response(204, ['X-Account-Meta-Temp-URL-Key' => 'secret']));

$tempUrl = $object->getTemporaryUrl(60, 'GET');

// Check that internal URLs are used
$this->assertContains('snet-storage', $tempUrl);
}

public function test_temp_urls_can_be_forced_to_use_public_urls()
{
$service = $this->getClient()->objectStoreService(null, 'IAD', 'internalURL');
$object = $service->getContainer('foo')->dataObject('bar');

$this->addMockSubscriber(new Response(204, ['X-Account-Meta-Temp-URL-Key' => 'secret']));

$tempUrl = $object->getTemporaryUrl(60, 'GET', true);

// Check that internal URLs are NOT used
$this->assertNotContains('snet-storage', $tempUrl);
}

public function test_Purge()
{
$object = $this->container->dataObject('foobar');
Expand Down

0 comments on commit 7e7f529

Please sign in to comment.