|
32 | 32 | use League\Flysystem\UrlGeneration\TemporaryUrlGenerator; |
33 | 33 | use League\MimeTypeDetection\FinfoMimeTypeDetector; |
34 | 34 | use League\MimeTypeDetection\MimeTypeDetector; |
| 35 | +use AzureOss\FlysystemAzureBlobStorage\Support\ConfigArrayParser; |
35 | 36 |
|
36 | 37 | final class AzureBlobStorageAdapter implements FilesystemAdapter, ChecksumProvider, TemporaryUrlGenerator, PublicUrlGenerator |
37 | 38 | { |
@@ -85,35 +86,96 @@ public function directoryExists(string $path): bool |
85 | 86 |
|
86 | 87 | public function write(string $path, string $contents, Config $config): void |
87 | 88 | { |
88 | | - $this->upload($path, $contents); |
| 89 | + $this->upload($path, $contents, $config); |
89 | 90 | } |
90 | 91 |
|
91 | 92 | public function writeStream(string $path, $contents, Config $config): void |
92 | 93 | { |
93 | | - $this->upload($path, $contents); |
| 94 | + $this->upload($path, $contents, $config); |
94 | 95 | } |
95 | 96 |
|
96 | 97 | /** |
97 | 98 | * @param string|resource $contents |
98 | 99 | */ |
99 | | - private function upload(string $path, $contents): void |
| 100 | + private function upload(string $path, $contents, ?Config $config = null): void |
100 | 101 | { |
101 | 102 | try { |
102 | | - $path = $this->prefixer->prefixPath($path); |
103 | | - $mimetype = $this->mimeTypeDetector->detectMimetype($path, $contents); |
| 103 | + $options = $this->buildUploadOptionsFromConfig($config); |
104 | 104 |
|
105 | | - $options = new UploadBlobOptions( |
106 | | - contentType: $mimetype, |
107 | | - ); |
| 105 | + if ($options->httpHeaders->contentType === "" && is_string($contents)) { |
| 106 | + $options->httpHeaders->contentType = $this->mimeTypeDetector->detectMimeTypeFromBuffer($contents) ?? ""; |
| 107 | + } |
108 | 108 |
|
109 | 109 | $this->containerClient |
110 | | - ->getBlobClient($path) |
| 110 | + ->getBlobClient($this->prefixer->prefixPath($path)) |
111 | 111 | ->upload($contents, $options); |
112 | 112 | } catch (\Throwable $e) { |
113 | 113 | throw UnableToWriteFile::atLocation($path, previous: $e); |
114 | 114 | } |
115 | 115 | } |
116 | 116 |
|
| 117 | + private function buildUploadOptionsFromConfig(?Config $config): UploadBlobOptions |
| 118 | + { |
| 119 | + $options = new UploadBlobOptions(); |
| 120 | + |
| 121 | + if ($config === null) { |
| 122 | + return $options; |
| 123 | + } |
| 124 | + |
| 125 | + $data = $config->toArray(); |
| 126 | + |
| 127 | + $initialTransferSize = ConfigArrayParser::parseIntFromArray($data, 'initialTransferSize'); |
| 128 | + if ($initialTransferSize !== null) { |
| 129 | + $options->initialTransferSize = $initialTransferSize; |
| 130 | + } |
| 131 | + |
| 132 | + $maximumTransferSize = ConfigArrayParser::parseIntFromArray($data, 'maximumTransferSize'); |
| 133 | + if ($maximumTransferSize !== null) { |
| 134 | + $options->maximumTransferSize = $maximumTransferSize; |
| 135 | + } |
| 136 | + |
| 137 | + $maximumConcurrency = ConfigArrayParser::parseIntFromArray($data, 'maximumConcurrency'); |
| 138 | + if ($maximumConcurrency !== null) { |
| 139 | + $options->maximumConcurrency = $maximumConcurrency; |
| 140 | + } |
| 141 | + |
| 142 | + $headers = ConfigArrayParser::parseArrayFromArray($data, 'httpHeaders'); |
| 143 | + if ($headers !== null) { |
| 144 | + $cacheControl = ConfigArrayParser::parseStringFromArray($headers, 'cacheControl', 'httpHeaders.'); |
| 145 | + if ($cacheControl !== null) { |
| 146 | + $options->httpHeaders->cacheControl = $cacheControl; |
| 147 | + } |
| 148 | + |
| 149 | + $contentDisposition = ConfigArrayParser::parseStringFromArray($headers, 'contentDisposition', 'httpHeaders.'); |
| 150 | + if ($contentDisposition !== null) { |
| 151 | + $options->httpHeaders->contentDisposition = $contentDisposition; |
| 152 | + } |
| 153 | + |
| 154 | + $contentEncoding = ConfigArrayParser::parseStringFromArray($headers, 'contentEncoding', 'httpHeaders.'); |
| 155 | + if ($contentEncoding !== null) { |
| 156 | + $options->httpHeaders->contentEncoding = $contentEncoding; |
| 157 | + } |
| 158 | + |
| 159 | + $contentHash = ConfigArrayParser::parseStringFromArray($headers, 'contentHash', 'httpHeaders.'); |
| 160 | + if ($contentHash !== null) { |
| 161 | + $options->httpHeaders->contentHash = $contentHash; |
| 162 | + } |
| 163 | + |
| 164 | + $contentLanguage = ConfigArrayParser::parseStringFromArray($headers, 'contentLanguage', 'httpHeaders.'); |
| 165 | + if ($contentLanguage !== null) { |
| 166 | + $options->httpHeaders->contentLanguage = $contentLanguage; |
| 167 | + } |
| 168 | + |
| 169 | + $contentType = ConfigArrayParser::parseStringFromArray($headers, 'contentType', 'httpHeaders.'); |
| 170 | + if ($contentType !== null) { |
| 171 | + $options->httpHeaders->contentType = $contentType; |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + return $options; |
| 176 | + } |
| 177 | + |
| 178 | + |
117 | 179 | public function read(string $path): string |
118 | 180 | { |
119 | 181 | try { |
|
0 commit comments