diff --git a/lib/OpenCloud/ObjectStore/Resource/DataObject.php b/lib/OpenCloud/ObjectStore/Resource/DataObject.php index de302b4cd..c4841c89f 100644 --- a/lib/OpenCloud/ObjectStore/Resource/DataObject.php +++ b/lib/OpenCloud/ObjectStore/Resource/DataObject.php @@ -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; @@ -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); diff --git a/tests/OpenCloud/Tests/ObjectStore/Resource/DataObjectTest.php b/tests/OpenCloud/Tests/ObjectStore/Resource/DataObjectTest.php index 4bebaf346..3dab21171 100644 --- a/tests/OpenCloud/Tests/ObjectStore/Resource/DataObjectTest.php +++ b/tests/OpenCloud/Tests/ObjectStore/Resource/DataObjectTest.php @@ -72,7 +72,7 @@ public function test_Copy() } /** - * @expectedException OpenCloud\Common\Exceptions\NoNameError + * @expectedException \OpenCloud\Common\Exceptions\NoNameError */ public function test_Copy_Fails() { @@ -80,13 +80,39 @@ public function test_Copy_Fails() } /** - * @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');