From 396f5ba5b526ac04e0317ea47e8a96fa4bba19fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ja=CC=81n=20Hamra=CC=81k?= Date: Fri, 20 Jun 2025 15:24:38 +0200 Subject: [PATCH] Add getUrl() method --- src/SmbAdapter.php | 13 +++++++++++++ tests/SmbAdapterTest.php | 18 ++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/SmbAdapter.php b/src/SmbAdapter.php index bf06ab2..3a76ac4 100644 --- a/src/SmbAdapter.php +++ b/src/SmbAdapter.php @@ -390,6 +390,19 @@ public function copy(string $source, string $destination, Config $config): void fclose($sourceStream); } + /** + * Get the URL of a file. + * + * @param string $path + * @return string + */ + public function getUrl($path): string + { + $contents = $this->read($path); + $mimeType = $this->mimeTypeDetector->detectMimeType($path, $contents) ?? 'application/octet-stream'; + return 'data:' . $mimeType . ';base64,' . base64_encode($contents); + } + /** * Create the parent directories if they do not exist. * diff --git a/tests/SmbAdapterTest.php b/tests/SmbAdapterTest.php index 017b5ab..89f0b7a 100644 --- a/tests/SmbAdapterTest.php +++ b/tests/SmbAdapterTest.php @@ -120,4 +120,22 @@ public function moving_a_file(): void $this->assertEquals('contents to be copied', $adapter->read('destination.txt')); }); } + + /** + * @test + */ + public function test_get_url_returns_valid_data_url(): void + { + $this->runScenario(function () { + $adapter = $this->adapter(); + $filename = 'test.txt'; + $content = 'Hello, world!'; + $adapter->write($filename, $content, new Config()); + + $dataUrl = $adapter->getUrl($filename); + + $this->assertStringStartsWith('data:text/plain;base64,', $dataUrl); + $this->assertStringContainsString(base64_encode($content), $dataUrl); + }); + } }