Updated delete method to support recrusive deletes
This commit is contained in:
parent
41ecda5ea3
commit
419fabd80a
|
@ -27,6 +27,7 @@ $services = include __DIR__.'/config/services.php'; // List of services
|
|||
$webhook = new Event('v1-webhooks', 'WebhooksV1');
|
||||
$audit = new Event('v1-audits', 'AuditsV1');
|
||||
$usage = new Event('v1-usage', 'UsageV1');
|
||||
$deletes = new Event('v1-deletes', 'DeletesV1');
|
||||
|
||||
/**
|
||||
* 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'))) {
|
||||
$webhook->trigger();
|
||||
|
@ -228,6 +229,10 @@ $utopia->shutdown(function () use ($response, $request, $webhook, $audit, $usage
|
|||
$audit->trigger();
|
||||
}
|
||||
|
||||
if (!empty($deletes->getParam('document'))) {
|
||||
$deletes->trigger();
|
||||
}
|
||||
|
||||
$route = $utopia->match($request);
|
||||
|
||||
if($project->getId()
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
global $utopia, $request, $response, $register, $user, $consoleDB, $projectDB;
|
||||
global $utopia, $request, $response, $register, $user, $consoleDB, $projectDB, $deletes;
|
||||
|
||||
use Utopia\Exception;
|
||||
use Utopia\Response;
|
||||
|
@ -402,7 +402,7 @@ $utopia->delete('/v1/projects/:projectId')
|
|||
->param('projectId', '', function () { return new UID(); }, 'Project unique ID.')
|
||||
->param('password', '', function () { return new UID(); }, 'Your user password for confirmation.')
|
||||
->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
|
||||
throw new Exception('Invalid credentials', 401);
|
||||
}
|
||||
|
@ -413,6 +413,8 @@ $utopia->delete('/v1/projects/:projectId')
|
|||
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)
|
||||
$list = $project->getAttribute('webhooks', []);
|
||||
|
||||
|
|
57
app/workers/deletes.php
Normal file
57
app/workers/deletes.php
Normal 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
|
||||
}
|
||||
}
|
|
@ -116,6 +116,23 @@ stdout_logfile_maxbytes=5000000
|
|||
stderr_logfile=/dev/stderr
|
||||
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]
|
||||
command=php /usr/share/nginx/html/vendor/bin/resque
|
||||
autostart=true
|
||||
|
|
|
@ -95,10 +95,11 @@ abstract class Device
|
|||
* @see http://php.net/manual/en/function.filesize.php
|
||||
*
|
||||
* @param string $path
|
||||
* @param bool $recrusive
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
abstract public function delete(string $path):bool;
|
||||
abstract public function delete(string $path, bool $recrusive):bool;
|
||||
|
||||
/**
|
||||
* Returns given file path its size.
|
||||
|
|
|
@ -151,12 +151,26 @@ class Local extends Device
|
|||
* @see http://php.net/manual/en/function.filesize.php
|
||||
*
|
||||
* @param string $path
|
||||
* @param bool $recrusive
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function delete(string $path):bool
|
||||
public function delete(string $path, bool $recrusive = false):bool
|
||||
{
|
||||
return unlink($path);
|
||||
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 false;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue