From 7890e18992150c085785bb35cc405c5ad8895480 Mon Sep 17 00:00:00 2001 From: Madhesh V <83573352+Madhesh-V@users.noreply.github.com> Date: Mon, 9 Oct 2023 10:27:09 +0530 Subject: [PATCH 1/4] Create Swiftstack.php --- src/Storage/Device/Swiftstack.php | 143 ++++++++++++++++++++++++++++++ 1 file changed, 143 insertions(+) create mode 100644 src/Storage/Device/Swiftstack.php diff --git a/src/Storage/Device/Swiftstack.php b/src/Storage/Device/Swiftstack.php new file mode 100644 index 00000000..acfd6ffd --- /dev/null +++ b/src/Storage/Device/Swiftstack.php @@ -0,0 +1,143 @@ +endpoint = $endpoint; // Store the SwiftStack endpoint URL + $this->username = $username; // Store the username + $this->password = $password; // Store the password + $this->container = $container; // Store the SwiftStack container + // You may also want to initialize SwiftStack SDK or make necessary API setup here + // For example, initialize SwiftStack SDK with the provided credentials + // $swiftstack = new SwiftStack($endpoint, $username, $password); + + } + + /** + * Upload a file to SwiftStack. + * + * @param string $path The path to the file in SwiftStack. + * @param string $content The content of the file to upload. + * + * @return bool True if the upload is successful, false otherwise. + */ + public function upload(string $path, string $content): bool + { + // Implement SwiftStack upload logic here + // Check if SwiftStack credentials are set + if (empty($this->endpoint) || empty($this->username) || empty($this->password) || empty($this->container)) { + throw new \RuntimeException('SwiftStack credentials are not set. Call setSwiftStackCredentials() first.'); + } + + // You can use SwiftStack SDK or make API requests to upload the file + // Replace the following placeholder code with your actual SwiftStack upload logic + + // Example: Using SwiftStack SDK (replace with actual SwiftStack SDK code) + // $swiftstack = new SwiftStack($this->endpoint, $this->username, $this->password); + // $container = $swiftstack->container($this->container); + // $container->create(); + // $object = $container->object($path); + // $object->setContent($content); + // If the upload is successful, return true + // If there's an error during the upload, return false + + return true; + } + + /** + * Download a file from SwiftStack. + * + * @param string $path The path to the file in SwiftStack. + * + * @return string|false The content of the downloaded file, or false on failure. + */ + public function download(string $path): string|false + { + // Implement SwiftStack download logic + // Check if SwiftStack credentials are set + if (empty($this->endpoint) || empty($this->username) || empty($this->password) || empty($this->container)) { + throw new \RuntimeException('SwiftStack credentials are not set. Call setSwiftStackCredentials() first.'); + } + + // Implement SwiftStack download logic here + // Use the SwiftStack SDK or make API requests to download the file + + // Example: Using SwiftStack SDK (replace with actual SwiftStack SDK code) + /* + try { + $client = new \SwiftStack\Client($this->endpoint); + $client->setCredentials($this->username, $this->password); + $client->setContainer($this->container); + + // Download the file content from SwiftStack + $fileContent = $client->downloadObject($path); + + // Return the downloaded content + return $fileContent; + } catch (\Exception $e) { + // Handle any exceptions or errors here + // You may want to log the error and return false in case of failure + return false; + } + */ + // If the download is successful, return the file content + // If there's an error during the download, return false + return 'File content'; + } + + /** + * Delete a file from SwiftStack. + * + * @param string $path The path to the file in SwiftStack. + * + * @return bool True if the deletion is successful, false otherwise. + */ + public function delete(string $path): bool + { + // Implement SwiftStack delete logic + // Check if SwiftStack credentials are set + if (empty($this->endpoint) || empty($this->username) || empty($this->password) || empty($this->container)) { + throw new \RuntimeException('SwiftStack credentials are not set. Call setSwiftStackCredentials() first.'); + } + + // Implement SwiftStack delete logic here + // Use the SwiftStack SDK or make API requests to delete the file + + // Example: Using SwiftStack SDK (replace with actual SwiftStack SDK code) + /* + try { + $client = new \SwiftStack\Client($this->endpoint); + $client->setCredentials($this->username, $this->password); + $client->setContainer($this->container); + + // Delete the file from SwiftStack + $client->deleteObject($path); + + // Return true to indicate a successful deletion + return true; + } catch (\Exception $e) { + // Handle any exceptions or errors here + // You may want to log the error and return false in case of failure + return false; + } + */ + return true; + } + +} From 258893b31a9b7cda120842f69224e96c824ae485 Mon Sep 17 00:00:00 2001 From: Madhesh V <83573352+Madhesh-V@users.noreply.github.com> Date: Mon, 9 Oct 2023 11:32:34 +0530 Subject: [PATCH 2/4] Create SwiftstackTest.php --- tests/Storage/Device/SwiftstackTest.php | 34 +++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 tests/Storage/Device/SwiftstackTest.php diff --git a/tests/Storage/Device/SwiftstackTest.php b/tests/Storage/Device/SwiftstackTest.php new file mode 100644 index 00000000..5220c03d --- /dev/null +++ b/tests/Storage/Device/SwiftstackTest.php @@ -0,0 +1,34 @@ +root = '/root'; + $key = $_SERVER['SWIFTSTACK_ACCESS_KEY'] ?? ''; + $secret = $_SERVER['SWIFTSTACK_SECRET'] ?? ''; + $bucket = 'utopia-storage-tests'; + + $this->object = new Swiftstack($this->root, $key, $secret, $bucket, Swiftstack::EU_CENTRAL_1, Swiftstack::ACL_PRIVATE); + } + + protected function getAdapterName(): string + { + return 'Swiftstack Storage'; + } + + protected function getAdapterType(): string + { + return $this->object->getType(); + } + + protected function getAdapterDescription(): string + { + return 'Swiftstack Storage'; + } +} From 7bee8901712ddd7ef04571bd2001adb33d7d3b03 Mon Sep 17 00:00:00 2001 From: Madhesh V <83573352+Madhesh-V@users.noreply.github.com> Date: Mon, 9 Oct 2023 11:41:35 +0530 Subject: [PATCH 3/4] Update SwiftstackTest.php --- tests/Storage/Device/SwiftstackTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Storage/Device/SwiftstackTest.php b/tests/Storage/Device/SwiftstackTest.php index 5220c03d..9d90196c 100644 --- a/tests/Storage/Device/SwiftstackTest.php +++ b/tests/Storage/Device/SwiftstackTest.php @@ -5,7 +5,7 @@ use Utopia\Storage\Device\Swiftstack; use Utopia\Tests\Storage\S3Base; -class Swiftstack extends S3Base +class SwiftstackTest extends S3Base { protected function init(): void { From 1833fc93c653ab4b5614b4f7107671ad9e657ef6 Mon Sep 17 00:00:00 2001 From: Madhesh V <83573352+Madhesh-V@users.noreply.github.com> Date: Mon, 9 Oct 2023 21:31:47 +0530 Subject: [PATCH 4/4] Update Swiftstack.php --- src/Storage/Device/Swiftstack.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Storage/Device/Swiftstack.php b/src/Storage/Device/Swiftstack.php index acfd6ffd..28a4d430 100644 --- a/src/Storage/Device/Swiftstack.php +++ b/src/Storage/Device/Swiftstack.php @@ -4,7 +4,7 @@ use Utopia\Storage\Storage; -class SwiftStack extends Local +class SwiftStack extends S3 { /** * Set SwiftStack credentials.