Skip to content

Commit 83588c9

Browse files
committed
Add information about CORs, protected assets should be in their own blob
1 parent 115f9dc commit 83588c9

File tree

5 files changed

+63
-12
lines changed

5 files changed

+63
-12
lines changed

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@ local filesystem. This leverages Flysystem in Silverstripe 4.
1111
The module requires a few environment variables to be set
1212

1313
* `AZURE_CONNECTION_URL`: The connection URL as from the dashboard
14-
* `AZURE_CONTAINER_NAME`: The name of the container to store assets in.
14+
* `AZURE_CONTAINER_NAME`: The name of the container to store public assets in.
15+
* `AZURE_PROTECTED_CONTAINER_NAME`: The name of the container for protected
16+
assets
17+
18+
By default the module will serve public files from the URL provided in
19+
`AZURE_CONNECTION_URL` (e.g silverstripe-assets.blob.core.windows.net) unless
20+
`AZURE_PUBLIC_BLOB_DOMAIN` is set. Protected assets are always served from the
21+
local domain and routed through the permission checking.
1522

1623
## Installation
1724

@@ -32,4 +39,7 @@ directly accessed.
3239

3340
The module supports this by streaming the contents of protected files down to the browser
3441
via the web server (as opposed to linking directly) by default. To ensure that
35-
protected assets can't be accessed, ensure you setup an appropriate policy.
42+
protected assets can't be accessed, ensure you setup an appropriate policy.
43+
44+
## CORS
45+

_config/assets.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ SilverStripe\Core\Injector\Injector:
1414
constructor:
1515
connectionUrl: '`AZURE_CONNECTION_URL`'
1616
containerName: '`AZURE_CONTAINER_NAME`'
17+
assetDomain: '`AZURE_CONTAINER_NAME`'
1718
League\Flysystem\Cached\Storage\Memory.public:
1819
class: League\Flysystem\Cached\Storage\Memory
1920
League\Flysystem\Cached\Storage\Adapter.public:
@@ -30,7 +31,7 @@ SilverStripe\Core\Injector\Injector:
3031
FullscreenInteractive\SilverStripe\AzureStorage\Adapter\ProtectedAdapter:
3132
constructor:
3233
connectionUrl: '`AZURE_CONNECTION_URL`'
33-
containerName: '`AZURE_CONTAINER_NAME`'
34+
containerName: '`AZURE_PROTECTED_CONTAINER_NAME`'
3435
League\Flysystem\Cached\Storage\Adapter.protected:
3536
class: League\Flysystem\Cached\Storage\Adapter
3637
constructor:

src/Adapter/ProtectedAdapter.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
namespace FullscreenInteractive\SilverStripe\AzureStorage\Adapter;
44

5-
use Exception;
5+
use FullscreenInteractive\SilverStripe\AzureStorage\Service\BlobService;
66
use InvalidArgumentException;
77
use League\Flysystem\AzureBlobStorage\AzureBlobStorageAdapter;
8-
use MicrosoftAzure\Storage\Blob\BlobRestProxy;
98
use SilverStripe\Assets\Flysystem\ProtectedAdapter as SilverstripeProtectedAdapter;
109
use SilverStripe\Control\Controller;
1110

@@ -28,7 +27,7 @@ public function __construct($connectionUrl = '', $containerName = '')
2827
throw new InvalidArgumentException("AZURE_CONTAINER_NAME environment variable not set");
2928
}
3029

31-
$client = BlobRestProxy::createBlobService($connectionUrl);
30+
$client = BlobService::clientForConnection($connectionUrl);
3231

3332
parent::__construct($client, $containerName);
3433
}

src/Adapter/PublicAdapter.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22

33
namespace FullscreenInteractive\SilverStripe\AzureStorage\Adapter;
44

5+
use FullscreenInteractive\SilverStripe\AzureStorage\Service\BlobService;
56
use InvalidArgumentException;
67
use League\Flysystem\AzureBlobStorage\AzureBlobStorageAdapter;
7-
use MicrosoftAzure\Storage\Blob\BlobRestProxy;
88
use SilverStripe\Assets\Flysystem\PublicAdapter as SilverstripePublicAdapter;
99
use SilverStripe\Control\Controller;
1010

1111
class PublicAdapter extends AzureBlobStorageAdapter implements SilverstripePublicAdapter
1212
{
13-
public function __construct($connectionUrl = '', $containerName = '')
13+
private $assetDomain;
14+
15+
public function __construct($connectionUrl = '', $containerName = '', $assetDomain = '')
1416
{
1517
if (!$connectionUrl) {
1618
throw new InvalidArgumentException("AZURE_CONNECTION_URL environment variable not set");
@@ -20,7 +22,15 @@ public function __construct($connectionUrl = '', $containerName = '')
2022
throw new InvalidArgumentException("AZURE_CONTAINER_NAME environment variable not set");
2123
}
2224

23-
$client = BlobRestProxy::createBlobService($connectionUrl);
25+
$client = BlobService::clientForConnection($connectionUrl);
26+
27+
if ($assetDomain) {
28+
$this->assetDomain = $assetDomain;
29+
} else {
30+
$this->assetDomain = (string) BlobService::getClient()
31+
->getPsrPrimaryUri()
32+
->withPath($containerName);
33+
}
2434

2535
parent::__construct($client, $containerName);
2636
}
@@ -32,10 +42,14 @@ public function __construct($connectionUrl = '', $containerName = '')
3242
*/
3343
public function getPublicUrl($path)
3444
{
35-
if ($meta = $this->getMetadata($path)) {
36-
return Controller::join_links(ASSETS_DIR, $meta['path']);
45+
$parts = explode('/', $path);
46+
47+
if (isset($parts[0]) && $parts[0] === ASSETS_DIR) {
48+
array_shift($parts);
3749
}
3850

39-
return '';
51+
$path = implode('/', $parts);
52+
53+
return Controller::join_links($this->assetDomain, $path);
4054
}
4155
}

src/Service/BlobService.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace FullscreenInteractive\SilverStripe\AzureStorage\Service;
4+
5+
use MicrosoftAzure\Storage\Blob\BlobRestProxy;
6+
7+
class BlobService
8+
{
9+
private static $client;
10+
11+
private static $connectionUrl;
12+
13+
public static function clientForConnection($connectionUrl)
14+
{
15+
if (!self::$connectionUrl || self::$connectionUrl !== $connectionUrl) {
16+
self::$connectionUrl = $connectionUrl;
17+
self::$client = BlobRestProxy::createBlobService($connectionUrl);
18+
}
19+
20+
return self::$client;
21+
}
22+
23+
public static function getClient()
24+
{
25+
return self::$client;
26+
}
27+
}

0 commit comments

Comments
 (0)