|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace VoicesOfWynn\Models\Storage; |
| 4 | + |
| 5 | +use MicrosoftAzure\Storage\Blob\BlobRestProxy; |
| 6 | +use MicrosoftAzure\Storage\Blob\Models\CreateBlockBlobOptions; |
| 7 | +use MicrosoftAzure\Storage\Blob\Models\ListBlobsOptions; |
| 8 | +use MicrosoftAzure\Storage\Common\Exceptions\ServiceException; |
| 9 | + |
| 10 | +class AzureBlobStorage implements StorageInterface { |
| 11 | + private BlobRestProxy $client; |
| 12 | + private string $containerName; |
| 13 | + private string $baseUrl; |
| 14 | + |
| 15 | + // MIME type mapping |
| 16 | + private const MIME_TYPES = [ |
| 17 | + 'ogg' => 'audio/ogg', |
| 18 | + 'jpg' => 'image/jpeg', |
| 19 | + 'jpeg' => 'image/jpeg', |
| 20 | + 'png' => 'image/png', |
| 21 | + ]; |
| 22 | + |
| 23 | + public function __construct(string $connectionString, string $containerName) { |
| 24 | + $this->client = BlobRestProxy::createBlobService($connectionString); |
| 25 | + $this->containerName = $containerName; |
| 26 | + |
| 27 | + // Extract account name from connection string to build base URL |
| 28 | + preg_match('/AccountName=([^;]+)/', $connectionString, $matches); |
| 29 | + $accountName = $matches[1] ?? 'vowstorage'; |
| 30 | + $this->baseUrl = "https://{$accountName}.blob.core.windows.net/{$containerName}/"; |
| 31 | + } |
| 32 | + |
| 33 | + public function upload(string $sourcePath, string $destinationPath, ?string $contentType = null): bool { |
| 34 | + try { |
| 35 | + $content = file_get_contents($sourcePath); |
| 36 | + if ($content === false) { |
| 37 | + throw new StorageException("Cannot read source file", 'upload', $sourcePath); |
| 38 | + } |
| 39 | + |
| 40 | + $options = new CreateBlockBlobOptions(); |
| 41 | + |
| 42 | + // Set content type |
| 43 | + if ($contentType === null) { |
| 44 | + $ext = strtolower(pathinfo($destinationPath, PATHINFO_EXTENSION)); |
| 45 | + $contentType = self::MIME_TYPES[$ext] ?? 'application/octet-stream'; |
| 46 | + } |
| 47 | + $options->setContentType($contentType); |
| 48 | + |
| 49 | + // Set cache control based on content type |
| 50 | + if (str_starts_with($contentType, 'image/')) { |
| 51 | + $options->setCacheControl('public, max-age=31536000'); // 1 year for images |
| 52 | + } else { |
| 53 | + $options->setCacheControl('public, max-age=3600'); // 1 hour for audio |
| 54 | + } |
| 55 | + |
| 56 | + $this->client->createBlockBlob($this->containerName, $destinationPath, $content, $options); |
| 57 | + return true; |
| 58 | + } catch (ServiceException $e) { |
| 59 | + throw new StorageException("Azure upload failed: " . $e->getMessage(), 'upload', $destinationPath, $e); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + public function delete(string $path): bool { |
| 64 | + try { |
| 65 | + $this->client->deleteBlob($this->containerName, $path); |
| 66 | + return true; |
| 67 | + } catch (ServiceException $e) { |
| 68 | + if ($e->getCode() === 404) { |
| 69 | + return true; // File doesn't exist = success |
| 70 | + } |
| 71 | + throw new StorageException("Azure delete failed: " . $e->getMessage(), 'delete', $path, $e); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + public function deleteByPrefix(string $prefix): array { |
| 76 | + try { |
| 77 | + $options = new ListBlobsOptions(); |
| 78 | + $options->setPrefix($prefix); |
| 79 | + |
| 80 | + $deleted = []; |
| 81 | + $result = $this->client->listBlobs($this->containerName, $options); |
| 82 | + |
| 83 | + foreach ($result->getBlobs() as $blob) { |
| 84 | + $blobName = $blob->getName(); |
| 85 | + $this->client->deleteBlob($this->containerName, $blobName); |
| 86 | + $deleted[] = $blobName; |
| 87 | + } |
| 88 | + return $deleted; |
| 89 | + } catch (ServiceException $e) { |
| 90 | + throw new StorageException("Azure deleteByPrefix failed: " . $e->getMessage(), 'deleteByPrefix', $prefix, $e); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + public function rename(string $oldPath, string $newPath): bool { |
| 95 | + try { |
| 96 | + // Azure doesn't support rename - must copy then delete |
| 97 | + $this->client->copyBlob($this->containerName, $newPath, $this->containerName, $oldPath); |
| 98 | + |
| 99 | + // Wait for the copy to complete before deleting |
| 100 | + $this->waitForCopyCompletion($newPath); |
| 101 | + |
| 102 | + $this->client->deleteBlob($this->containerName, $oldPath); |
| 103 | + return true; |
| 104 | + } catch (ServiceException $e) { |
| 105 | + throw new StorageException("Azure rename failed: " . $e->getMessage(), 'rename', $oldPath, $e); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + public function copy(string $sourcePath, string $destinationPath): bool { |
| 110 | + try { |
| 111 | + $this->client->copyBlob($this->containerName, $destinationPath, $this->containerName, $sourcePath); |
| 112 | + return true; |
| 113 | + } catch (ServiceException $e) { |
| 114 | + throw new StorageException("Azure copy failed: " . $e->getMessage(), 'copy', $sourcePath, $e); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + public function exists(string $path): bool { |
| 119 | + try { |
| 120 | + $this->client->getBlobMetadata($this->containerName, $path); |
| 121 | + return true; |
| 122 | + } catch (ServiceException $e) { |
| 123 | + if ($e->getCode() === 404) { |
| 124 | + return false; |
| 125 | + } |
| 126 | + throw new StorageException("Azure exists check failed: " . $e->getMessage(), 'exists', $path, $e); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + public function getUrl(string $path, bool $cacheBust = false): string { |
| 131 | + // Encode each path segment to handle special characters while preserving directory separators |
| 132 | + $segments = explode('/', $path); |
| 133 | + $encodedSegments = array_map('rawurlencode', $segments); |
| 134 | + $encodedPath = implode('/', $encodedSegments); |
| 135 | + |
| 136 | + $url = $this->baseUrl . $encodedPath; |
| 137 | + if ($cacheBust) { |
| 138 | + $url .= '?v=' . time(); |
| 139 | + } |
| 140 | + return $url; |
| 141 | + } |
| 142 | + |
| 143 | + public function getBaseUrl(): string { |
| 144 | + return $this->baseUrl; |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Waits for an asynchronous blob copy operation to complete. |
| 149 | + * Polls the blob properties until the copy status is 'success' or fails. |
| 150 | + * |
| 151 | + * @param string $blobPath Path to the destination blob being copied |
| 152 | + * @throws StorageException If copy fails or times out |
| 153 | + */ |
| 154 | + private function waitForCopyCompletion(string $blobPath): void { |
| 155 | + $maxAttempts = 60; // Maximum 60 attempts |
| 156 | + $sleepSeconds = 1; // Wait 1 second between polls |
| 157 | + |
| 158 | + for ($i = 0; $i < $maxAttempts; $i++) { |
| 159 | + try { |
| 160 | + $properties = $this->client->getBlobProperties($this->containerName, $blobPath); |
| 161 | + $copyStatus = $properties->getProperties()->getCopyState(); |
| 162 | + |
| 163 | + if ($copyStatus === null) { |
| 164 | + // No copy operation found - might be a same-container instant copy |
| 165 | + return; |
| 166 | + } |
| 167 | + |
| 168 | + $status = $copyStatus->getStatus(); |
| 169 | + |
| 170 | + if ($status === 'success') { |
| 171 | + return; |
| 172 | + } |
| 173 | + |
| 174 | + if ($status === 'failed' || $status === 'aborted') { |
| 175 | + throw new StorageException( |
| 176 | + "Blob copy failed with status: {$status}", |
| 177 | + 'waitForCopyCompletion', |
| 178 | + $blobPath |
| 179 | + ); |
| 180 | + } |
| 181 | + |
| 182 | + // Status is 'pending' - wait and retry |
| 183 | + sleep($sleepSeconds); |
| 184 | + |
| 185 | + } catch (ServiceException $e) { |
| 186 | + throw new StorageException( |
| 187 | + "Failed to check copy status: " . $e->getMessage(), |
| 188 | + 'waitForCopyCompletion', |
| 189 | + $blobPath, |
| 190 | + $e |
| 191 | + ); |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + throw new StorageException( |
| 196 | + "Blob copy timed out after {$maxAttempts} seconds", |
| 197 | + 'waitForCopyCompletion', |
| 198 | + $blobPath |
| 199 | + ); |
| 200 | + } |
| 201 | +} |
0 commit comments