Skip to content

Commit 6c6b008

Browse files
author
brecht.vermeersch
committed
add prefixer
1 parent 3981b98 commit 6c6b008

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

src/AzureBlobStorageAdapter.php

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace AzureOss\FlysystemAzureBlobStorage;
66

77
use AzureOss\Storage\Blob\BlobContainerClient;
8-
use AzureOss\Storage\Blob\BlobServiceClient;
98
use AzureOss\Storage\Blob\Models\Blob;
109
use AzureOss\Storage\Blob\Models\BlobProperties;
1110
use AzureOss\Storage\Blob\Models\UploadBlobOptions;
@@ -16,6 +15,7 @@
1615
use League\Flysystem\DirectoryAttributes;
1716
use League\Flysystem\FileAttributes;
1817
use League\Flysystem\FilesystemAdapter;
18+
use League\Flysystem\PathPrefixer;
1919
use League\Flysystem\UnableToCheckExistence;
2020
use League\Flysystem\UnableToCopyFile;
2121
use League\Flysystem\UnableToDeleteDirectory;
@@ -34,19 +34,22 @@
3434
class AzureBlobStorageAdapter implements FilesystemAdapter, ChecksumProvider, TemporaryUrlGenerator
3535
{
3636
private readonly MimeTypeDetector $mimeTypeDetector;
37+
private readonly PathPrefixer $prefixer;
3738

3839
public function __construct(
3940
private readonly BlobContainerClient $containerClient,
41+
string $prefix = "",
4042
?MimeTypeDetector $mimeTypeDetector = null,
4143
) {
44+
$this->prefixer = new PathPrefixer($prefix);
4245
$this->mimeTypeDetector = $mimeTypeDetector ?? new FinfoMimeTypeDetector();
4346
}
4447

4548
public function fileExists(string $path): bool
4649
{
4750
try {
4851
return $this->containerClient
49-
->getBlobClient($path)
52+
->getBlobClient($this->prefixer->prefixPath($path))
5053
->exists();
5154
} catch(\Throwable $e) {
5255
throw UnableToCheckExistence::forLocation($path, $e);
@@ -82,6 +85,7 @@ public function writeStream(string $path, $contents, Config $config): void
8285
private function upload(string $path, $contents): void
8386
{
8487
try {
88+
$path = $this->prefixer->prefixPath($path);
8589
$mimetype = $this->mimeTypeDetector->detectMimetype($path, $contents);
8690

8791
$options = new UploadBlobOptions(
@@ -100,7 +104,7 @@ public function read(string $path): string
100104
{
101105
try {
102106
$result = $this->containerClient
103-
->getBlobClient($path)
107+
->getBlobClient($this->prefixer->prefixPath($path))
104108
->downloadStreaming();
105109

106110
return $result->content->getContents();
@@ -113,7 +117,7 @@ public function readStream(string $path)
113117
{
114118
try {
115119
$result = $this->containerClient
116-
->getBlobClient($path)
120+
->getBlobClient($this->prefixer->prefixPath($path))
117121
->downloadStreaming();
118122

119123
$resource = $result->content->detach();
@@ -132,7 +136,7 @@ public function delete(string $path): void
132136
{
133137
try {
134138
$this->containerClient
135-
->getBlobClient($path)
139+
->getBlobClient($this->prefixer->prefixPath($path))
136140
->deleteIfExists();
137141
} catch (\Throwable $e) {
138142
throw UnableToDeleteFile::atLocation($path, previous: $e);
@@ -145,7 +149,7 @@ public function deleteDirectory(string $path): void
145149
foreach ($this->listContents($path, true) as $item) {
146150
if ($item instanceof FileAttributes) {
147151
$this->containerClient
148-
->getBlobClient($item->path())
152+
->getBlobClient($this->prefixer->prefixPath($item->path()))
149153
->delete();
150154
}
151155
}
@@ -198,6 +202,8 @@ public function fileSize(string $path): FileAttributes
198202

199203
private function fetchMetadata(string $path): FileAttributes
200204
{
205+
$path = $this->prefixer->prefixPath($path);
206+
201207
$properties = $this->containerClient
202208
->getBlobClient($path)
203209
->getProperties();
@@ -208,18 +214,18 @@ private function fetchMetadata(string $path): FileAttributes
208214
public function listContents(string $path, bool $deep): iterable
209215
{
210216
try {
211-
$prefix = $path === "" ? null : ltrim($path, "/") . "/";
217+
$prefix = $this->prefixer->prefixDirectoryPath($path);
212218

213219
if ($deep) {
214220
foreach ($this->containerClient->getBlobs($prefix) as $item) {
215-
yield $this->normalizeBlob($item->name, $item->properties);
221+
yield $this->normalizeBlob($this->prefixer->stripPrefix($item->name), $item->properties);
216222
}
217223
} else {
218224
foreach ($this->containerClient->getBlobsByHierarchy($prefix) as $item) {
219225
if ($item instanceof Blob) {
220-
yield $this->normalizeBlob($item->name, $item->properties);
226+
yield $this->normalizeBlob($this->prefixer->stripPrefix($item->name), $item->properties);
221227
} else {
222-
yield new DirectoryAttributes($item->name);
228+
yield new DirectoryAttributes($this->prefixer->stripPrefix($item->name));
223229
}
224230
}
225231
}
@@ -251,8 +257,8 @@ public function move(string $source, string $destination, Config $config): void
251257
public function copy(string $source, string $destination, Config $config): void
252258
{
253259
try {
254-
$sourceBlobClient = $this->containerClient->getBlobClient($source);
255-
$targetBlobClient = $this->containerClient->getBlobClient($destination);
260+
$sourceBlobClient = $this->containerClient->getBlobClient($this->prefixer->prefixPath($source));
261+
$targetBlobClient = $this->containerClient->getBlobClient($this->prefixer->prefixPath($destination));
256262

257263
$targetBlobClient->copyFromUri($sourceBlobClient->uri);
258264
} catch (\Throwable $e) {
@@ -267,10 +273,11 @@ public function temporaryUrl(string $path, \DateTimeInterface $expiresAt, Config
267273
->setPermissions("r");
268274

269275
$sas = $this->containerClient
270-
->getBlobClient($path)
276+
->getBlobClient($this->prefixer->prefixPath($path))
271277
->generateSasUri($sasBuilder);
272278

273279
return (string) $sas;
280+
274281
}
275282

276283
public function checksum(string $path, Config $config): string
@@ -283,7 +290,7 @@ public function checksum(string $path, Config $config): string
283290

284291
try {
285292
$properties = $this->containerClient
286-
->getBlobClient($path)
293+
->getBlobClient($this->prefixer->prefixPath($path))
287294
->getProperties();
288295

289296
return bin2hex(base64_decode($properties->contentMD5));

tests/AzureBlobStorageTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ protected static function createFilesystemAdapter(): FilesystemAdapter
2626

2727
return new AzureBlobStorageAdapter(
2828
self::createContainerClient(),
29+
'flysystem',
2930
);
3031
}
3132

0 commit comments

Comments
 (0)