Skip to content

Commit

Permalink
allow setting custom CDN host
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitry-mukhin committed Mar 21, 2014
1 parent 60b07fd commit 20dd653
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 12 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,23 @@ If you want to delete file, just call delete() method on Uploadcare\File object.
$file->delete();
```

## Custom User-Agent and CDN host

You can customize User-Agent reported during API requests (please do this if you're building a lib that is using uploadcare-php).
To do that pass a string with user agent name as third argument to Api constructor:

```php
$api = new Uploadcare\Api(UC_PUBLIC_KEY, UC_SECRET_KEY, "Awesome Lib/1.2.3");
```

You may also change default CDN host. You need to do this when you're using custom CNAME or you want to explicitly set your
[CDN provider](https://uploadcare.com/documentation/cdn/#alternative-domains).
To do that pass a string with domain name as fourth argument to Api constructor:

```php
$api = new Uploadcare\Api(UC_PUBLIC_KEY, UC_SECRET_KEY, null, "kx.ucarecdn.com");
```

## Tests

Inside "tests" directory you can find tests for PHP 5.3 and up.
Expand Down
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 1.1.3
- allow setting custom CDN host

## 1.1.2
- add preview operation

Expand Down
14 changes: 12 additions & 2 deletions src/Uploadcare/Api.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
namespace Uploadcare;

$uploadcare_version = '1.1.2';
$uploadcare_version = '1.1.3';
define('UPLOADCARE_LIB_VERSION', sprintf('%s/%s.%s', $uploadcare_version, PHP_MAJOR_VERSION, PHP_MINOR_VERSION));

class Api
Expand All @@ -27,6 +27,13 @@ class Api
*/
private $api_host = 'api.uploadcare.com';

/**
* Uploadcare CDN host
*
* @var string
*/
public $cdn_host = 'www.ucarecdn.com';

/**
* Widget instance.
*
Expand Down Expand Up @@ -63,12 +70,15 @@ class Api
* @param string $ua Custom User-Agent to report
* @return void
*/
public function __construct($public_key, $secret_key, $ua = false)
public function __construct($public_key, $secret_key, $ua = null, $cdn_host = null)
{
$this->public_key = $public_key;
$this->secret_key = $secret_key;
$this->widget = new Widget($this);
$this->uploader = new Uploader($this);
if($cdn_host) {
$this->cdn_host = $cdn_host;
}
if($ua) {
$this->ua = $ua;
} else {
Expand Down
13 changes: 3 additions & 10 deletions src/Uploadcare/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,10 @@
namespace Uploadcare;


class File {

class File
{
private $re_uuid_with_effects = '!/?(?P<uuid>[a-z0-9]{8}-(?:[a-z0-9]{4}-){3}[a-z0-9]{12})(?:/(?:-/(?P<effects>(?:[^/]+/)+)))?(?<filename>[^/]*)!';

/**
* Uploadcare cdn host
*
* @var string
*/
private $cdn_host = 'www.ucarecdn.com';

/**
* Uploadcare file id
*
Expand Down Expand Up @@ -160,7 +153,7 @@ public function delete()
*/
public function getUrl($postfix = null)
{
$url = sprintf('http://%s/%s/', $this->cdn_host, $this->uuid);
$url = sprintf('http://%s/%s/', $this->api->cdn_host, $this->uuid);
if($this->default_effects) {
$url = sprintf('%s-/%s', $url, $this->default_effects);
}
Expand Down
16 changes: 16 additions & 0 deletions tests/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,22 @@ public function testFile()
$this->assertEquals($file->preview(400, 400)->getUrl(), 'http://www.ucarecdn.com/3c99da1d-ef05-4d79-81d8-d4f208d98beb/-/preview/400x400/');
}

/**
* Let's check that user can set custom CDN host
*/
public function testCustomCDNHost()
{
$api = new Api(UC_PUBLIC_KEY, UC_SECRET_KEY, null, 'example.com');
$file = $api->getFile('3c99da1d-ef05-4d79-81d8-d4f208d98beb');

$this->assertEquals(get_class($file), 'Uploadcare\File');

$this->assertEquals($file->getUrl(), 'http://example.com/3c99da1d-ef05-4d79-81d8-d4f208d98beb/');
$this->assertEquals($file->resize(400, 400)->getUrl(), 'http://example.com/3c99da1d-ef05-4d79-81d8-d4f208d98beb/-/resize/400x400/');
$this->assertEquals($file->resize(400, false)->getUrl(), 'http://example.com/3c99da1d-ef05-4d79-81d8-d4f208d98beb/-/resize/400x/');
$this->assertEquals($file->resize(false, 400)->getUrl(), 'http://example.com/3c99da1d-ef05-4d79-81d8-d4f208d98beb/-/resize/x400/');
}

/**
* Test upload from URL
*/
Expand Down

0 comments on commit 20dd653

Please sign in to comment.