From 7350d4e4470b2b55158bf4f109a97a634ce86eac Mon Sep 17 00:00:00 2001 From: Everly Precia Suresh Date: Thu, 12 May 2022 14:22:41 +0000 Subject: [PATCH] better way to initialize storage libs --- app/executor.php | 37 ++++++--------------------------- app/init.php | 38 +++++++--------------------------- app/workers/deletes.php | 30 ++------------------------- src/Appwrite/Resque/Worker.php | 2 +- 4 files changed, 16 insertions(+), 91 deletions(-) diff --git a/app/executor.php b/app/executor.php index 3d827f0a24..6fc1aaf691 100644 --- a/app/executor.php +++ b/app/executor.php @@ -2,6 +2,7 @@ require_once __DIR__ . '/../vendor/autoload.php'; use Appwrite\Runtimes\Runtimes; +use Appwrite\Resque\Worker; use Swoole\ConnectionPool; use Swoole\Http\Request as SwooleRequest; use Swoole\Http\Response as SwooleResponse; @@ -113,34 +114,6 @@ function logError(Throwable $error, string $action, Utopia\Route $route = null) Console::error('[Error] Line: ' . $error->getLine()); }; -function getStorageDevice($root): Device { - switch (App::getEnv('_APP_STORAGE_DEVICE', Storage::DEVICE_LOCAL)) { - case Storage::DEVICE_LOCAL:default: - return new Local($root); - case Storage::DEVICE_S3: - $s3AccessKey = App::getEnv('_APP_STORAGE_S3_ACCESS_KEY', ''); - $s3SecretKey = App::getEnv('_APP_STORAGE_S3_SECRET', ''); - $s3Region = App::getEnv('_APP_STORAGE_S3_REGION', ''); - $s3Bucket = App::getEnv('_APP_STORAGE_S3_BUCKET', ''); - $s3Acl = 'private'; - return new S3($root, $s3AccessKey, $s3SecretKey, $s3Bucket, $s3Region, $s3Acl); - case Storage::DEVICE_DO_SPACES: - $doSpacesAccessKey = App::getEnv('_APP_STORAGE_DO_SPACES_ACCESS_KEY', ''); - $doSpacesSecretKey = App::getEnv('_APP_STORAGE_DO_SPACES_SECRET', ''); - $doSpacesRegion = App::getEnv('_APP_STORAGE_DO_SPACES_REGION', ''); - $doSpacesBucket = App::getEnv('_APP_STORAGE_DO_SPACES_BUCKET', ''); - $doSpacesAcl = 'private'; - return new DOSpaces($root, $doSpacesAccessKey, $doSpacesSecretKey, $doSpacesBucket, $doSpacesRegion, $doSpacesAcl); - case Storage::DEVICE_BACKBLAZE: - $backblazeAccessKey = App::getEnv('_APP_STORAGE_BACKBLAZE_ACCESS_KEY', ''); - $backblazeSecretKey = App::getEnv('_APP_STORAGE_BACKBLAZE_SECRET', ''); - $backblazeRegion = App::getEnv('_APP_STORAGE_BACKBLAZE_REGION', ''); - $backblazeBucket = App::getEnv('_APP_STORAGE_BACKBLAZE_BUCKET', ''); - $backblazeAcl = 'private'; - return new Backblaze($root, $backblazeAccessKey, $backblazeSecretKey, $backblazeBucket, $backblazeRegion, $backblazeAcl); - } -} - App::post('/v1/runtimes') ->desc("Create a new runtime server") ->param('runtimeId', '', new Text(64), 'Unique runtime ID.') @@ -181,7 +154,8 @@ App::post('/v1/runtimes') /** * Copy code files from source to a temporary location on the executor */ - $sourceDevice = getStorageDevice("/"); + $worker=new Worker(); + $sourceDevice = $worker->getDevice("/"); $localDevice = new Local(); $buffer = $sourceDevice->read($source); if(!$localDevice->write($tmpSource, $buffer)) { @@ -268,8 +242,9 @@ App::post('/v1/runtimes') if (!\file_exists($tmpBuild)) { throw new Exception('Something went wrong during the build process', 500); } - - $destinationDevice = getStorageDevice($destination); + + $worker=new Worker(); + $destinationDevice = $worker->getDevice($destination); $outputPath = $destinationDevice->getPath(\uniqid() . '.' . \pathinfo('code.tar.gz', PATHINFO_EXTENSION)); $buffer = $localDevice->read($tmpBuild); diff --git a/app/init.php b/app/init.php index a87f3ee3fc..e3b364b0e6 100644 --- a/app/init.php +++ b/app/init.php @@ -27,6 +27,7 @@ use Appwrite\Network\Validator\Email; use Appwrite\Network\Validator\IP; use Appwrite\Network\Validator\URL; use Appwrite\OpenSSL\OpenSSL; +use Appwrite\Resque\Worker; use Appwrite\Stats\Stats; use Appwrite\Utopia\View; use Utopia\App; @@ -807,45 +808,20 @@ App::setResource('deviceLocal', function() { }); App::setResource('deviceFiles', function($project) { - return getDevice(APP_STORAGE_UPLOADS . '/app-' . $project->getId()); + $worker=new Worker(); + return $worker->getDevice(APP_STORAGE_UPLOADS . '/app-' . $project->getId()); }, ['project']); App::setResource('deviceFunctions', function($project) { - return getDevice(APP_STORAGE_FUNCTIONS . '/app-' . $project->getId()); + $worker=new Worker(); + return $worker->getDevice(APP_STORAGE_FUNCTIONS . '/app-' . $project->getId()); }, ['project']); App::setResource('deviceBuilds', function($project) { - return getDevice(APP_STORAGE_BUILDS . '/app-' . $project->getId()); + $worker=new Worker(); + return $worker->getDevice(APP_STORAGE_BUILDS . '/app-' . $project->getId()); }, ['project']); -function getDevice($root): Device { - switch (App::getEnv('_APP_STORAGE_DEVICE', Storage::DEVICE_LOCAL)) { - case Storage::DEVICE_LOCAL:default: - return new Local($root); - case Storage::DEVICE_S3: - $s3AccessKey = App::getEnv('_APP_STORAGE_S3_ACCESS_KEY', ''); - $s3SecretKey = App::getEnv('_APP_STORAGE_S3_SECRET', ''); - $s3Region = App::getEnv('_APP_STORAGE_S3_REGION', ''); - $s3Bucket = App::getEnv('_APP_STORAGE_S3_BUCKET', ''); - $s3Acl = 'private'; - return new S3($root, $s3AccessKey, $s3SecretKey, $s3Bucket, $s3Region, $s3Acl); - case Storage::DEVICE_DO_SPACES: - $doSpacesAccessKey = App::getEnv('_APP_STORAGE_DO_SPACES_ACCESS_KEY', ''); - $doSpacesSecretKey = App::getEnv('_APP_STORAGE_DO_SPACES_SECRET', ''); - $doSpacesRegion = App::getEnv('_APP_STORAGE_DO_SPACES_REGION', ''); - $doSpacesBucket = App::getEnv('_APP_STORAGE_DO_SPACES_BUCKET', ''); - $doSpacesAcl = 'private'; - return new DOSpaces($root, $doSpacesAccessKey, $doSpacesSecretKey, $doSpacesBucket, $doSpacesRegion, $doSpacesAcl); - case Storage::DEVICE_BACKBLAZE: - $backblazeAccessKey = App::getEnv('_APP_STORAGE_BACKBLAZE_ACCESS_KEY', ''); - $backblazeSecretKey = App::getEnv('_APP_STORAGE_BACKBLAZE_SECRET', ''); - $backblazeRegion = App::getEnv('_APP_STORAGE_BACKBLAZE_REGION', ''); - $backblazeBucket = App::getEnv('_APP_STORAGE_BACKBLAZE_BUCKET', ''); - $backblazeAcl = 'private'; - return new Backblaze($root, $backblazeAccessKey, $backblazeSecretKey, $backblazeBucket, $backblazeRegion, $backblazeAcl); - } -} - App::setResource('mode', function($request) { /** @var Appwrite\Utopia\Request $request */ diff --git a/app/workers/deletes.php b/app/workers/deletes.php index 441aebf728..b365adea81 100644 --- a/app/workers/deletes.php +++ b/app/workers/deletes.php @@ -545,34 +545,8 @@ class DeletesV1 extends Worker $dbForProject = $this->getProjectDB($projectId); $dbForProject->deleteCollection('bucket_' . $document->getInternalId()); - $device = new Local(APP_STORAGE_UPLOADS.'/app-'.$projectId); - - switch (App::getEnv('_APP_STORAGE_DEVICE', Storage::DEVICE_LOCAL)) { - case Storage::DEVICE_S3: - $s3AccessKey = App::getEnv('_APP_STORAGE_S3_ACCESS_KEY', ''); - $s3SecretKey = App::getEnv('_APP_STORAGE_S3_SECRET', ''); - $s3Region = App::getEnv('_APP_STORAGE_S3_REGION', ''); - $s3Bucket = App::getEnv('_APP_STORAGE_S3_BUCKET', ''); - $s3Acl = 'private'; - $device = new S3(APP_STORAGE_UPLOADS . '/app-' . $projectId, $s3AccessKey, $s3SecretKey, $s3Bucket, $s3Region, $s3Acl); - break; - case Storage::DEVICE_DO_SPACES: - $doSpacesAccessKey = App::getEnv('_APP_STORAGE_DO_SPACES_ACCESS_KEY', ''); - $doSpacesSecretKey = App::getEnv('_APP_STORAGE_DO_SPACES_SECRET', ''); - $doSpacesRegion = App::getEnv('_APP_STORAGE_DO_SPACES_REGION', ''); - $doSpacesBucket = App::getEnv('_APP_STORAGE_DO_SPACES_BUCKET', ''); - $doSpacesAcl = 'private'; - $device = new DOSpaces(APP_STORAGE_UPLOADS . '/app-' . $projectId, $doSpacesAccessKey, $doSpacesSecretKey, $doSpacesBucket, $doSpacesRegion, $doSpacesAcl); - break; - case Storage::DEVICE_BACKBLAZE: - $backblazeAccessKey = App::getEnv('_APP_STORAGE_BACKBLAZE_ACCESS_KEY', ''); - $backblazeSecretKey = App::getEnv('_APP_STORAGE_BACKBLAZE_SECRET', ''); - $backblazeRegion = App::getEnv('_APP_STORAGE_BACKBLAZE_REGION', ''); - $backblazeBucket = App::getEnv('_APP_STORAGE_BACKBLAZE_BUCKET', ''); - $backblazeAcl = 'private'; - $device= new Backblaze(APP_STORAGE_UPLOADS . '/app-' . $projectId, $backblazeAccessKey, $backblazeSecretKey, $backblazeBucket, $backblazeRegion, $backblazeAcl); - break; - } + $worker=new Worker(); + $device=$worker->getDevice(APP_STORAGE_UPLOADS.'/app-'.$projectId); $device->deletePath($document->getId()); } diff --git a/src/Appwrite/Resque/Worker.php b/src/Appwrite/Resque/Worker.php index 34a37dbc5e..40fad72102 100644 --- a/src/Appwrite/Resque/Worker.php +++ b/src/Appwrite/Resque/Worker.php @@ -260,7 +260,7 @@ abstract class Worker * @param string $root path of the device * @return Device */ - private function getDevice($root): Device + public function getDevice($root): Device { switch (App::getEnv('_APP_STORAGE_DEVICE', Storage::DEVICE_LOCAL)) { case Storage::DEVICE_LOCAL:default: