|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace AzureOss\FlysystemAzureBlobStorage; |
| 6 | + |
| 7 | +use AzureOss\Storage\Blob\ContainerClient; |
| 8 | +use AzureOss\Storage\Blob\Options\ListBlobsOptions; |
| 9 | +use AzureOss\Storage\Blob\Options\UploadBlockBlobOptions; |
| 10 | +use DateTimeInterface; |
| 11 | +use League\Flysystem\CalculateChecksumFromStream; |
| 12 | +use League\Flysystem\ChecksumProvider; |
| 13 | +use League\Flysystem\Config; |
| 14 | +use League\Flysystem\DirectoryAttributes; |
| 15 | +use League\Flysystem\FileAttributes; |
| 16 | +use League\Flysystem\FilesystemAdapter; |
| 17 | +use League\Flysystem\UnableToCheckExistence; |
| 18 | +use League\Flysystem\UnableToCopyFile; |
| 19 | +use League\Flysystem\UnableToDeleteDirectory; |
| 20 | +use League\Flysystem\UnableToDeleteFile; |
| 21 | +use League\Flysystem\UnableToListContents; |
| 22 | +use League\Flysystem\UnableToMoveFile; |
| 23 | +use League\Flysystem\UnableToReadFile; |
| 24 | +use League\Flysystem\UnableToRetrieveMetadata; |
| 25 | +use League\Flysystem\UnableToSetVisibility; |
| 26 | +use League\Flysystem\UnableToWriteFile; |
| 27 | +use League\Flysystem\UrlGeneration\PublicUrlGenerator; |
| 28 | +use League\Flysystem\UrlGeneration\TemporaryUrlGenerator; |
| 29 | +use League\MimeTypeDetection\FinfoMimeTypeDetector; |
| 30 | +use League\MimeTypeDetection\MimeTypeDetector; |
| 31 | + |
| 32 | +class AzureBlobStorageAdapter implements FilesystemAdapter, ChecksumProvider, PublicUrlGenerator, TemporaryUrlGenerator |
| 33 | +{ |
| 34 | + use CalculateChecksumFromStream; |
| 35 | + |
| 36 | + private readonly MimeTypeDetector $mimeTypeDetector; |
| 37 | + |
| 38 | + public function __construct( |
| 39 | + private readonly ContainerClient $containerClient, |
| 40 | + ?MimeTypeDetector $mimeTypeDetector = null, |
| 41 | + ) { |
| 42 | + $this->mimeTypeDetector = $mimeTypeDetector ?? new FinfoMimeTypeDetector(); |
| 43 | + } |
| 44 | + |
| 45 | + public function fileExists(string $path): bool |
| 46 | + { |
| 47 | + try { |
| 48 | + return $this->containerClient->getBlobClient($path)->exists(); |
| 49 | + } catch(\Throwable $e) { |
| 50 | + throw UnableToCheckExistence::forLocation($path, $e); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + public function directoryExists(string $path): bool |
| 55 | + { |
| 56 | + try { |
| 57 | + $options = new ListBlobsOptions( |
| 58 | + prefix: $this->getPrefix($path), |
| 59 | + maxResults: 1, |
| 60 | + delimiter: "/" |
| 61 | + ); |
| 62 | + |
| 63 | + $response = $this->containerClient->listBlobs($options); |
| 64 | + |
| 65 | + return count($response->blobs) > 0; |
| 66 | + } catch (\Throwable $e) { |
| 67 | + throw UnableToCheckExistence::forLocation($path, $e); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + public function write(string $path, string $contents, Config $config): void |
| 72 | + { |
| 73 | + $this->upload($path, $contents); |
| 74 | + } |
| 75 | + |
| 76 | + public function writeStream(string $path, $contents, Config $config): void |
| 77 | + { |
| 78 | + $this->upload($path, $contents); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * @param string|resource $contents |
| 83 | + */ |
| 84 | + private function upload(string $path, $contents): void |
| 85 | + { |
| 86 | + try { |
| 87 | + $mimetype = $this->mimeTypeDetector->detectMimetype($path, $contents); |
| 88 | + |
| 89 | + $options = new UploadBlockBlobOptions( |
| 90 | + contentType: $mimetype, |
| 91 | + ); |
| 92 | + |
| 93 | + $this->containerClient->getBlockBlobClient($path)->upload($contents, $options); |
| 94 | + } catch (\Throwable $e) { |
| 95 | + throw UnableToWriteFile::atLocation($path, previous: $e); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + public function read(string $path): string |
| 100 | + { |
| 101 | + try { |
| 102 | + $response = $this->containerClient->getBlobClient($path)->get(); |
| 103 | + |
| 104 | + return $response->content->getContents(); |
| 105 | + } catch (\Throwable $e) { |
| 106 | + throw UnableToReadFile::fromLocation($path, previous: $e); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + public function readStream(string $path) |
| 111 | + { |
| 112 | + try { |
| 113 | + $response = $this->containerClient->getBlobClient($path)->get(); |
| 114 | + $resource = $response->content->detach(); |
| 115 | + |
| 116 | + if($resource === null) { |
| 117 | + throw new \Exception("Should not happen"); |
| 118 | + } |
| 119 | + |
| 120 | + return $resource; |
| 121 | + } catch (\Throwable $e) { |
| 122 | + throw UnableToReadFile::fromLocation($path, previous: $e); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + public function delete(string $path): void |
| 127 | + { |
| 128 | + try { |
| 129 | + $this->containerClient->getBlobClient($path)->deleteIfExists(); |
| 130 | + } catch (\Throwable $e) { |
| 131 | + throw UnableToDeleteFile::atLocation($path, previous: $e); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + public function deleteDirectory(string $path): void |
| 136 | + { |
| 137 | + try { |
| 138 | + foreach ($this->listContents($path, true) as $item) { |
| 139 | + if ($item instanceof FileAttributes) { |
| 140 | + $this->containerClient->getBlobClient($item->path())->delete(); |
| 141 | + } |
| 142 | + } |
| 143 | + } catch (\Throwable $e) { |
| 144 | + throw UnableToDeleteDirectory::atLocation($path, previous: $e); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + public function createDirectory(string $path, Config $config): void |
| 149 | + { |
| 150 | + // Azure does not support this operation. |
| 151 | + } |
| 152 | + |
| 153 | + public function setVisibility(string $path, string $visibility): void |
| 154 | + { |
| 155 | + throw UnableToSetVisibility::atLocation($path, "Azure does not support this operation."); |
| 156 | + } |
| 157 | + |
| 158 | + public function visibility(string $path): FileAttributes |
| 159 | + { |
| 160 | + throw UnableToRetrieveMetadata::visibility($path, "Azure does not support this operation."); |
| 161 | + } |
| 162 | + |
| 163 | + public function mimeType(string $path): FileAttributes |
| 164 | + { |
| 165 | + try { |
| 166 | + return $this->fetchMetadata($path); |
| 167 | + } catch (\Throwable $e) { |
| 168 | + throw UnableToRetrieveMetadata::mimeType($path, previous: $e); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + public function lastModified(string $path): FileAttributes |
| 173 | + { |
| 174 | + try { |
| 175 | + return $this->fetchMetadata($path); |
| 176 | + } catch (\Throwable $e) { |
| 177 | + throw UnableToRetrieveMetadata::lastModified($path, previous: $e); |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + public function fileSize(string $path): FileAttributes |
| 182 | + { |
| 183 | + try { |
| 184 | + return $this->fetchMetadata($path); |
| 185 | + } catch (\Throwable $e) { |
| 186 | + throw UnableToRetrieveMetadata::lastModified($path, previous: $e); |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + private function fetchMetadata(string $path): FileAttributes |
| 191 | + { |
| 192 | + $response = $this->containerClient->getBlobClient($path)->getProperties(); |
| 193 | + |
| 194 | + return new FileAttributes( |
| 195 | + $path, |
| 196 | + fileSize: $response->contentLength, |
| 197 | + lastModified: $response->lastModified->getTimestamp(), |
| 198 | + mimeType: $response->contentType |
| 199 | + ); |
| 200 | + } |
| 201 | + |
| 202 | + public function listContents(string $path, bool $deep): iterable |
| 203 | + { |
| 204 | + try { |
| 205 | + do { |
| 206 | + $nextMarker = ""; |
| 207 | + |
| 208 | + $options = new ListBlobsOptions( |
| 209 | + prefix: $this->getPrefix($path), |
| 210 | + marker: $nextMarker, |
| 211 | + delimiter: $deep ? null : "/", |
| 212 | + ); |
| 213 | + |
| 214 | + $response = $this->containerClient->listBlobs($options); |
| 215 | + |
| 216 | + foreach($response->blobPrefixes as $blobPrefix) { |
| 217 | + yield new DirectoryAttributes($blobPrefix->name); |
| 218 | + } |
| 219 | + |
| 220 | + foreach ($response->blobs as $blob) { |
| 221 | + yield new FileAttributes( |
| 222 | + $blob->name, |
| 223 | + fileSize: $blob->properties->contentLength, |
| 224 | + lastModified: $blob->properties->lastModified->getTimestamp(), |
| 225 | + mimeType: $blob->properties->contentType, |
| 226 | + ); |
| 227 | + } |
| 228 | + |
| 229 | + $nextMarker = $response->nextMarker; |
| 230 | + } while ($nextMarker !== ""); |
| 231 | + } catch (\Throwable $e) { |
| 232 | + throw UnableToListContents::atLocation($path, $deep, new \Exception()); |
| 233 | + } |
| 234 | + } |
| 235 | + |
| 236 | + public function move(string $source, string $destination, Config $config): void |
| 237 | + { |
| 238 | + try { |
| 239 | + $this->copy($source, $destination, $config); |
| 240 | + $this->delete($source); |
| 241 | + } catch (\Throwable $e) { |
| 242 | + throw UnableToMoveFile::fromLocationTo($source, $destination, $e); |
| 243 | + } |
| 244 | + } |
| 245 | + |
| 246 | + public function copy(string $source, string $destination, Config $config): void |
| 247 | + { |
| 248 | + try { |
| 249 | + $this->containerClient->getBlobClient($source)->copy($this->containerClient->containerName, $destination); |
| 250 | + } catch (\Throwable $e) { |
| 251 | + throw UnableToCopyFile::fromLocationTo($source, $destination, $e); |
| 252 | + } |
| 253 | + } |
| 254 | + |
| 255 | + private function getPrefix(string $path): ?string |
| 256 | + { |
| 257 | + return $path === "" ? null : rtrim($path, "/") . "/"; |
| 258 | + } |
| 259 | + |
| 260 | + public function checksum(string $path, Config $config): string |
| 261 | + { |
| 262 | + return $this->calculateChecksumFromStream($path, $config); |
| 263 | + } |
| 264 | + |
| 265 | + public function publicUrl(string $path, Config $config): string |
| 266 | + { |
| 267 | + return "TODO"; |
| 268 | + } |
| 269 | + |
| 270 | + public function temporaryUrl(string $path, DateTimeInterface $expiresAt, Config $config): string |
| 271 | + { |
| 272 | + return "TODO"; |
| 273 | + } |
| 274 | +} |
0 commit comments