-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from bedita/feat/tus-cache-cleanup
Cache cleanup command
- Loading branch information
Showing
1 changed file
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/** | ||
* BEdita, API-first content management framework | ||
* Copyright 2022 Atlas Srl, Chialab Srl | ||
* | ||
* This file is part of BEdita: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published | ||
* by the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details. | ||
*/ | ||
|
||
namespace BEdita\Tus\Command; | ||
|
||
use Cake\Command\Command; | ||
use Cake\Console\Arguments; | ||
use Cake\Console\ConsoleIo; | ||
use Cake\Core\Configure; | ||
use Cake\Utility\Hash; | ||
use Carbon\Carbon; | ||
use TusPhp\Config; | ||
use TusPhp\Tus\Server; | ||
|
||
/** | ||
* Remove expired items (finished or unfinished) from TUS cache. | ||
*/ | ||
class CleanExpiredCommand extends Command | ||
{ | ||
/** | ||
* @inheritDoc | ||
*/ | ||
public function execute(Arguments $args, ConsoleIo $io) | ||
{ | ||
$io->out([ | ||
'<info>Cleaning server resources</info>', | ||
'<info>=========================</info>', | ||
'', | ||
]); | ||
|
||
$server = $this->tusServer(); | ||
$deleted = $server->handleExpiration(); | ||
|
||
if (empty($deleted)) { | ||
$io->out('<comment>Nothing to delete.</comment>'); | ||
} else { | ||
foreach ($deleted as $key => $item) { | ||
$io->out('<comment>' . ($key + 1) . ". Deleted {$item['name']} </comment>"); | ||
} | ||
} | ||
|
||
$io->out('Done'); | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* Create custom TUS server, overriding `isExpired` method | ||
* | ||
* @return \TusPhp\Tus\Server | ||
*/ | ||
protected function tusServer(): Server | ||
{ | ||
Config::set(Configure::read('Tus.server')); | ||
|
||
return new class (Configure::read('Tus.cache')) extends Server | ||
{ | ||
/** | ||
* Check if content is expired - even if upload is completed | ||
* | ||
* @param array|null $contents Cache content | ||
* @return bool | ||
*/ | ||
protected function isExpired($contents): bool | ||
{ | ||
$expiresAt = Hash::get((array)$contents, 'expires_at'); | ||
|
||
return empty($expiresAt) || Carbon::parse($expiresAt)->lt(Carbon::now()); | ||
} | ||
|
||
/** | ||
* Set cache. | ||
* | ||
* @param mixed $cache Cache configuration | ||
* @return self | ||
*/ | ||
public function setCache($cache): self | ||
{ | ||
parent::setCache($cache); | ||
$this->cache->setPrefix('tus:server:'); | ||
|
||
return $this; | ||
} | ||
}; | ||
} | ||
} |