1
0
Fork 0
mirror of synced 2024-09-30 09:18:14 +13:00

Updated delete method to support recrusive deletes

This commit is contained in:
Eldad Fux 2020-05-26 20:11:20 +03:00
parent 41ecda5ea3
commit 419fabd80a
6 changed files with 103 additions and 7 deletions

View file

@ -27,6 +27,7 @@ $services = include __DIR__.'/config/services.php'; // List of services
$webhook = new Event('v1-webhooks', 'WebhooksV1'); $webhook = new Event('v1-webhooks', 'WebhooksV1');
$audit = new Event('v1-audits', 'AuditsV1'); $audit = new Event('v1-audits', 'AuditsV1');
$usage = new Event('v1-usage', 'UsageV1'); $usage = new Event('v1-usage', 'UsageV1');
$deletes = new Event('v1-deletes', 'DeletesV1');
/** /**
* Get All verified client URLs for both console and current projects * Get All verified client URLs for both console and current projects
@ -215,10 +216,10 @@ $utopia->init(function () use ($utopia, $request, $response, &$user, $project, $
; ;
}); });
$utopia->shutdown(function () use ($response, $request, $webhook, $audit, $usage, $mode, $project, $utopia) { $utopia->shutdown(function () use ($response, $request, $webhook, $audit, $usage, $deletes, $mode, $project, $utopia) {
/* /*
* Trigger Events for background jobs * Trigger events for background workers
*/ */
if (!empty($webhook->getParam('event'))) { if (!empty($webhook->getParam('event'))) {
$webhook->trigger(); $webhook->trigger();
@ -228,6 +229,10 @@ $utopia->shutdown(function () use ($response, $request, $webhook, $audit, $usage
$audit->trigger(); $audit->trigger();
} }
if (!empty($deletes->getParam('document'))) {
$deletes->trigger();
}
$route = $utopia->match($request); $route = $utopia->match($request);
if($project->getId() if($project->getId()

View file

@ -1,6 +1,6 @@
<?php <?php
global $utopia, $request, $response, $register, $user, $consoleDB, $projectDB; global $utopia, $request, $response, $register, $user, $consoleDB, $projectDB, $deletes;
use Utopia\Exception; use Utopia\Exception;
use Utopia\Response; use Utopia\Response;
@ -402,7 +402,7 @@ $utopia->delete('/v1/projects/:projectId')
->param('projectId', '', function () { return new UID(); }, 'Project unique ID.') ->param('projectId', '', function () { return new UID(); }, 'Project unique ID.')
->param('password', '', function () { return new UID(); }, 'Your user password for confirmation.') ->param('password', '', function () { return new UID(); }, 'Your user password for confirmation.')
->action( ->action(
function ($projectId, $password) use ($response, $consoleDB, $user) { function ($projectId, $password) use ($response, $consoleDB, $user, $deletes) {
if (!Auth::passwordVerify($password, $user->getAttribute('password'))) { // Double check user password if (!Auth::passwordVerify($password, $user->getAttribute('password'))) { // Double check user password
throw new Exception('Invalid credentials', 401); throw new Exception('Invalid credentials', 401);
} }
@ -413,6 +413,8 @@ $utopia->delete('/v1/projects/:projectId')
throw new Exception('Project not found', 404); throw new Exception('Project not found', 404);
} }
$deletes->setParam('document', $project->getArrayCopy());
foreach(['keys', 'webhooks', 'tasks', 'platforms', 'domains'] as $key) { // Delete all children (keys, webhooks, tasks [stop tasks?], platforms) foreach(['keys', 'webhooks', 'tasks', 'platforms', 'domains'] as $key) { // Delete all children (keys, webhooks, tasks [stop tasks?], platforms)
$list = $project->getAttribute('webhooks', []); $list = $project->getAttribute('webhooks', []);

57
app/workers/deletes.php Normal file
View file

@ -0,0 +1,57 @@
<?php
require_once __DIR__.'/../init.php';
cli_set_process_title('Deletes V1 Worker');
echo APP_NAME.' deletes worker v1 has started';
use Appwrite\Database\Database;
use Appwrite\Database\Document;
use Appwrite\Storage\Devices\Local;
class DeletesV1
{
public $args = [];
public function setUp()
{
}
public function perform()
{
global $consoleDB, $request;
$document = $this->args['document'];
$document = new Document($document);
switch ($document->getCollection()) {
case Database::SYSTEM_COLLECTION_PROJECTS:
$this->deleteProject($document);
break;
default:
break;
}
}
public function tearDown()
{
// ... Remove environment for this job
}
protected function deleteProject(Document $document)
{
global $consoleDB;
// Delete all DBs
$consoleDB->deleteNamespace($document->getId());
$uploads = new Local(APP_STORAGE_UPLOADS.'/app-'.$document->getId());
$cache = new Local(APP_STORAGE_CACHE.'/app-'.$document->getId());
// Optimize DB?
// Delete all storage files
// Delete all storage cache
}
}

View file

@ -116,6 +116,23 @@ stdout_logfile_maxbytes=5000000
stderr_logfile=/dev/stderr stderr_logfile=/dev/stderr
stderr_logfile_maxbytes = 0 stderr_logfile_maxbytes = 0
[program:v1-deletes]
command=php /usr/share/nginx/html/vendor/bin/resque
autostart=true
autorestart=true
priority=10
environment=QUEUE='v1-deletes',APP_INCLUDE='/usr/share/nginx/html/app/workers/deletes.php',REDIS_BACKEND='%(ENV__APP_REDIS_HOST)s:%(ENV__APP_REDIS_PORT)s'
stdout_events_enabled=true
stderr_events_enabled=true
stopsignal=QUIT
startretries=10
;stdout_logfile=/dev/stdout
;stdout_logfile_maxbytes=0
stdout_logfile=/var/log/appwrite.log
stdout_logfile_maxbytes=5000000
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes = 0
[program:v1-certificates] [program:v1-certificates]
command=php /usr/share/nginx/html/vendor/bin/resque command=php /usr/share/nginx/html/vendor/bin/resque
autostart=true autostart=true

View file

@ -95,10 +95,11 @@ abstract class Device
* @see http://php.net/manual/en/function.filesize.php * @see http://php.net/manual/en/function.filesize.php
* *
* @param string $path * @param string $path
* @param bool $recrusive
* *
* @return bool * @return bool
*/ */
abstract public function delete(string $path):bool; abstract public function delete(string $path, bool $recrusive):bool;
/** /**
* Returns given file path its size. * Returns given file path its size.

View file

@ -151,14 +151,28 @@ class Local extends Device
* @see http://php.net/manual/en/function.filesize.php * @see http://php.net/manual/en/function.filesize.php
* *
* @param string $path * @param string $path
* @param bool $recrusive
* *
* @return bool * @return bool
*/ */
public function delete(string $path):bool public function delete(string $path, bool $recrusive = false):bool
{ {
if(is_dir($path) && $recrusive) {
$files = glob($path.'*', GLOB_MARK); // GLOB_MARK adds a slash to directories returned
foreach($files as $file) {
$this->delete($file);
}
rmdir($path);
}
elseif(is_file($path)) {
return unlink($path); return unlink($path);
} }
return false;
}
/** /**
* Returns given file path its size. * Returns given file path its size.
* *