diff --git a/src/AzureStorageBlobAdapter.php b/src/AzureStorageBlobAdapter.php index 40f7d0f..4fd30e3 100644 --- a/src/AzureStorageBlobAdapter.php +++ b/src/AzureStorageBlobAdapter.php @@ -36,13 +36,48 @@ public function url($path) return $this->adapter->publicUrl($path, new Config); } - /** @phpstan-ignore-next-line */ + /** + * Get a temporary URL for the file at the given path. + * + * @param string $path + * @param \DateTimeInterface $expiration + * @return string + */ + /** @phpstan-ignore-next-line */ public function temporaryUrl($path, $expiration, array $options = []) { return $this->adapter->temporaryUrl( $path, $expiration, - new Config(['permissions' => 'r']) + new Config(array_merge(['permissions' => 'r'], $options)) ); } + + /** + * Get a temporary upload URL for the file at the given path. + * + * @param string $path + * @param \DateTimeInterface $expiration + * @return array{url: string, headers: array} + */ + /** @phpstan-ignore-next-line */ + public function temporaryUploadUrl($path, $expiration, array $options = []) + { + $url = $this->adapter->temporaryUrl( + $path, + $expiration, + new Config(array_merge(['permissions' => 'cw'], $options)) + ); + $contentType = isset($options['content-type']) && is_string($options['content-type']) + ? $options['content-type'] + : 'application/octet-stream'; + + return [ + 'url' => $url, + 'headers' => [ + 'x-ms-blob-type' => 'BlockBlob', + 'Content-Type' => $contentType, + ], + ]; + } } diff --git a/tests/AzureStorageBlobAdapterTest.php b/tests/AzureStorageBlobAdapterTest.php index 3f672f6..e7829b6 100644 --- a/tests/AzureStorageBlobAdapterTest.php +++ b/tests/AzureStorageBlobAdapterTest.php @@ -88,7 +88,27 @@ public function driver_works(): void self::assertFalse($driver->exists('file2.txt')); self::assertTrue($driver->exists('file3.txt')); - self::assertCount(2, $driver->allFiles()); + // Test temporary upload URL functionality + // Generate a temporary upload URL + $uploadData = $driver->temporaryUploadUrl('temp-upload-test.txt', now()->addMinutes(5), [ + 'content-type' => 'text/plain', + ]); + + // Upload content directly to the URL + $content = 'This content was uploaded directly to a temporary URL'; + $response = Http::withHeaders($uploadData['headers']) + ->withBody($content, 'text/plain') + ->put($uploadData['url']); + + // Verify the upload was successful + self::assertTrue($response->successful()); + + // Verify the file exists and has the correct content + self::assertTrue($driver->exists('temp-upload-test.txt')); + self::assertEquals($content, $driver->get('temp-upload-test.txt')); + + // Count files and clean up + self::assertCount(3, $driver->allFiles()); // file.txt, file3.txt, and temp-upload-test.txt $driver->deleteDirectory('');