2
2
3
3
namespace FullscreenInteractive \SilverStripe \AzureStorage \Adapter ;
4
4
5
- use FullscreenInteractive \ SilverStripe \ AzureStorage \ Service \ BlobService ;
6
- use InvalidArgumentException ;
7
- use League \ Flysystem \ AzureBlobStorage \ AzureBlobStorageAdapter ;
5
+ use DateTime ;
6
+ use MicrosoftAzure \ Storage \ Blob \ BlobSharedAccessSignatureHelper ;
7
+ use MicrosoftAzure \ Storage \ Common \ Internal \ Resources ;
8
8
use SilverStripe \Assets \Flysystem \ProtectedAdapter as SilverstripeProtectedAdapter ;
9
9
use SilverStripe \Control \Controller ;
10
10
@@ -15,22 +15,7 @@ class ProtectedAdapter extends AzureBlobStorageAdapter implements SilverstripePr
15
15
*
16
16
* @var int|string
17
17
*/
18
- protected $ expiry = 300 ;
19
-
20
- public function __construct ($ connectionUrl = '' , $ containerName = '' )
21
- {
22
- if (!$ connectionUrl ) {
23
- throw new InvalidArgumentException ("AZURE_CONNECTION_URL environment variable not set " );
24
- }
25
-
26
- if (!$ containerName ) {
27
- throw new InvalidArgumentException ("AZURE_PROTECTED_CONTAINER_NAME environment variable not set " );
28
- }
29
-
30
- $ client = BlobService::clientForConnection ($ connectionUrl );
31
-
32
- parent ::__construct ($ client , $ containerName );
33
- }
18
+ protected $ expiry = 3600 ;
34
19
35
20
/**
36
21
* @return int|string
@@ -47,7 +32,7 @@ public function getExpiry()
47
32
* @param int|string $expiry
48
33
* @return $this
49
34
*/
50
- public function setExpiry ($ expiry )
35
+ public function setExpiry ($ expiry ): self
51
36
{
52
37
$ this ->expiry = $ expiry ;
53
38
return $ this ;
@@ -61,18 +46,50 @@ public function setExpiry($expiry)
61
46
public function getProtectedUrl ($ path )
62
47
{
63
48
if ($ meta = $ this ->getMetadata ($ path )) {
64
- return Controller::join_links (ASSETS_DIR , $ meta ['path ' ]);
49
+ $ token = '? ' . $ this ->presignToken ($ meta ['path ' ]);
50
+ return Controller::join_links ($ this ->assetDomain , $ meta ['path ' ], $ token );
65
51
}
66
52
67
53
return '' ;
68
54
}
69
55
70
- public function getVisibility ($ path )
56
+ public function getVisibility ($ path ): array
71
57
{
72
58
// Save an API call
73
59
return [
74
- 'path ' => $ path ,
60
+ 'path ' => $ path ,
75
61
'visibility ' => self ::VISIBILITY_PRIVATE
76
62
];
77
63
}
64
+
65
+ /**
66
+ * Build a presigned token with an expiry
67
+ *
68
+ * @param string $path
69
+ * @return string
70
+ */
71
+ protected function presignToken (string $ path ): string
72
+ {
73
+ $ sasHelper = new BlobSharedAccessSignatureHelper (
74
+ $ this ->settings ->getName (),
75
+ $ this ->settings ->getKey ()
76
+ );
77
+
78
+ // Get expiry string
79
+ $ expiry = $ this ->getExpiry ();
80
+ if (is_int ($ expiry )) {
81
+ $ expiry = "+ {$ expiry } seconds " ;
82
+ }
83
+ $ validFrom = new DateTime ();
84
+ $ expiry = (new DateTime ())->modify ($ expiry );
85
+
86
+ // Build token
87
+ return $ sasHelper ->generateBlobServiceSharedAccessSignatureToken (
88
+ Resources::RESOURCE_TYPE_BLOB ,
89
+ $ this ->container . '/ ' . $ path ,
90
+ 'r ' ,
91
+ $ expiry ,
92
+ $ validFrom
93
+ );
94
+ }
78
95
}
0 commit comments