diff --git a/.env b/.env index b6e3f7a2e..3406ba100 100644 --- a/.env +++ b/.env @@ -35,4 +35,7 @@ _APP_FUNCTIONS_CPUS=1 _APP_FUNCTIONS_MEMORY=128 _APP_FUNCTIONS_MEMORY_SWAP=128 _APP_MAINTENANCE_INTERVAL=86400 -_APP_USAGE_STATS=enabled \ No newline at end of file +_APP_MAINTENANCE_RETENTION_EXECUTION=1209600 +_APP_MAINTENANCE_RETENTION_ABUSE=86400 +_APP_MAINTENANCE_RETENTION_AUDIT=1209600 +_APP_USAGE_STATS=enabled diff --git a/Dockerfile b/Dockerfile index 0017d68dc..c7316bd1d 100755 --- a/Dockerfile +++ b/Dockerfile @@ -100,7 +100,11 @@ ENV _APP_SERVER=swoole \ _APP_SETUP=self-hosted \ _APP_VERSION=$VERSION \ _APP_USAGE_STATS=enabled \ + # 14 Days = 1209600 s + _APP_MAINTENANCE_RETENTION_EXECUTION=1209600 \ + _APP_MAINTENANCE_RETENTION_AUDIT=1209600 \ # 1 Day = 86400 s + _APP_MAINTENANCE_RETENTION_ABUSE=86400 \ _APP_MAINTENANCE_INTERVAL=86400 #ENV _APP_SMTP_SECURE '' #ENV _APP_SMTP_USERNAME '' diff --git a/app/config/variables.php b/app/config/variables.php index 410232846..8f00a78b0 100644 --- a/app/config/variables.php +++ b/app/config/variables.php @@ -374,6 +374,30 @@ return [ 'required' => false, 'question' => '', ], + [ + 'name' => '_APP_MAINTENANCE_RETENTION_EXECUTION', + 'description' => 'The maximum duration (in seconds) upto which to retain execution logs. The default value is 1209600 seconds (14 days).', + 'introduction' => '0.7.0', + 'default' => '1209600', + 'required' => false, + 'question' => '', + ], + [ + 'name' => '_APP_MAINTENANCE_RETENTION_AUDIT', + 'description' => 'IThe maximum duration (in seconds) upto which to retain audit logs. The default value is 1209600 seconds (14 days).', + 'introduction' => '0.7.0', + 'default' => '1209600', + 'required' => false, + 'question' => '', + ], + [ + 'name' => '_APP_MAINTENANCE_RETENTION_ABUSE', + 'description' => 'The maximum duration (in seconds) upto which to retain abuse logs. The default value is 86400 seconds (1 day).', + 'introduction' => '0.7.0', + 'default' => '86400', + 'required' => false, + 'question' => '', + ] ], ], ], diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index 0572b8137..733a20ddd 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -4,11 +4,11 @@ use Appwrite\Database\Database; use Appwrite\Database\Document; use Appwrite\Database\Validator\Authorization; use Appwrite\Database\Validator\UID; -use Appwrite\Storage\Storage; -use Appwrite\Storage\Validator\File; -use Appwrite\Storage\Validator\FileSize; -use Appwrite\Storage\Validator\FileType; -use Appwrite\Storage\Validator\Upload; +use Utopia\Storage\Storage; +use Utopia\Storage\Validator\File; +use Utopia\Storage\Validator\FileSize; +use Utopia\Storage\Validator\FileType; +use Utopia\Storage\Validator\Upload; use Appwrite\Utopia\Response; use Appwrite\Task\Validator\Cron; use Utopia\App; diff --git a/app/controllers/api/health.php b/app/controllers/api/health.php index 21e9f2c94..696247f3d 100644 --- a/app/controllers/api/health.php +++ b/app/controllers/api/health.php @@ -2,8 +2,8 @@ use Utopia\App; use Utopia\Exception; -use Appwrite\Storage\Device\Local; -use Appwrite\Storage\Storage; +use Utopia\Storage\Device\Local; +use Utopia\Storage\Storage; use Appwrite\ClamAV\Network; use Appwrite\Event\Event; diff --git a/app/controllers/api/storage.php b/app/controllers/api/storage.php index b70fe9f95..18e023347 100644 --- a/app/controllers/api/storage.php +++ b/app/controllers/api/storage.php @@ -13,11 +13,11 @@ use Appwrite\ClamAV\Network; use Appwrite\Database\Database; use Appwrite\Database\Document; use Appwrite\Database\Validator\UID; -use Appwrite\Storage\Storage; -use Appwrite\Storage\Validator\File; -use Appwrite\Storage\Validator\FileSize; -use Appwrite\Storage\Validator\Upload; -use Appwrite\Storage\Compression\Algorithms\GZIP; +use Utopia\Storage\Storage; +use Utopia\Storage\Validator\File; +use Utopia\Storage\Validator\FileSize; +use Utopia\Storage\Validator\Upload; +use Utopia\Storage\Compression\Algorithms\GZIP; use Appwrite\Resize\Resize; use Appwrite\OpenSSL\OpenSSL; use Appwrite\Utopia\Response; diff --git a/app/controllers/general.php b/app/controllers/general.php index 445563301..f5a3c09c9 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -14,8 +14,8 @@ use Appwrite\Database\Database; use Appwrite\Database\Document; use Appwrite\Database\Validator\Authorization; use Appwrite\Network\Validator\Origin; -use Appwrite\Storage\Device\Local; -use Appwrite\Storage\Storage; +use Utopia\Storage\Device\Local; +use Utopia\Storage\Storage; use Appwrite\Utopia\Response\Filters\V06; use Utopia\CLI\Console; @@ -83,9 +83,6 @@ App::init(function ($utopia, $request, $response, $console, $project, $user, $lo : '.'.$request->getHostname() ); - Storage::setDevice('files', new Local(APP_STORAGE_UPLOADS.'/app-'.$project->getId())); - Storage::setDevice('functions', new Local(APP_STORAGE_FUNCTIONS.'/app-'.$project->getId())); - /* * Response format */ diff --git a/app/controllers/mock.php b/app/controllers/mock.php index ca26867c8..4c9f3faf9 100644 --- a/app/controllers/mock.php +++ b/app/controllers/mock.php @@ -8,7 +8,7 @@ use Utopia\Validator\Numeric; use Utopia\Validator\Text; use Utopia\Validator\ArrayList; use Utopia\Validator\Host; -use Appwrite\Storage\Validator\File; +use Utopia\Storage\Validator\File; App::get('/v1/mock/tests/foo') ->desc('Mock a get request for SDK tests') diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index bc2707f24..8dc0c097c 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -6,8 +6,8 @@ use Utopia\App; use Utopia\Exception; use Utopia\Abuse\Abuse; use Utopia\Abuse\Adapters\TimeLimit; -use Appwrite\Storage\Device\Local; -use Appwrite\Storage\Storage; +use Utopia\Storage\Device\Local; +use Utopia\Storage\Storage; App::init(function ($utopia, $request, $response, $project, $user, $register, $events, $audits, $usage, $deletes) { /** @var Utopia\App $utopia */ diff --git a/app/controllers/web/console.php b/app/controllers/web/console.php index b8fc5a621..6b3e105aa 100644 --- a/app/controllers/web/console.php +++ b/app/controllers/web/console.php @@ -7,7 +7,7 @@ use Utopia\Domains\Domain; use Appwrite\Database\Database; use Appwrite\Database\Validator\Authorization; use Appwrite\Database\Validator\UID; -use Appwrite\Storage\Storage; +use Utopia\Storage\Storage; App::init(function ($layout) { /** @var Utopia\View $layout */ diff --git a/app/tasks/doctor.php b/app/tasks/doctor.php index e3985c8e7..2918d4f1e 100644 --- a/app/tasks/doctor.php +++ b/app/tasks/doctor.php @@ -3,8 +3,8 @@ global $cli; use Appwrite\ClamAV\Network; -use Appwrite\Storage\Device\Local; -use Appwrite\Storage\Storage; +use Utopia\Storage\Device\Local; +use Utopia\Storage\Storage; use Utopia\App; use Utopia\CLI\Console; use Utopia\Domains\Domain; diff --git a/app/tasks/maintenance.php b/app/tasks/maintenance.php index 34c34cfb8..78a31e6dd 100644 --- a/app/tasks/maintenance.php +++ b/app/tasks/maintenance.php @@ -12,10 +12,11 @@ Console::title('Maintenance V1'); Console::success(APP_NAME.' maintenance process v1 has started'); -function notifyDeleteExecutionLogs() +function notifyDeleteExecutionLogs(int $interval) { Resque::enqueue(Event::DELETE_QUEUE_NAME, Event::DELETE_CLASS_NAME, [ - 'type' => DELETE_TYPE_EXECUTIONS + 'type' => DELETE_TYPE_EXECUTIONS, + 'timestamp' => time() - $interval ]); } @@ -41,14 +42,15 @@ $cli ->action(function () { // # of days in seconds (1 day = 86400s) $interval = (int) App::getEnv('_APP_MAINTENANCE_INTERVAL', '86400'); + $executionLogsRetention = (int) App::getEnv('_APP_MAINTENANCE_RETENTION_EXECUTION', '1209600'); + $auditLogRetention = (int) App::getEnv('_APP_MAINTENANCE_RETENTION_AUDIT', '1209600'); + $abuseLogsRetention = (int) App::getEnv('_APP_MAINTENANCE_RETENTION_ABUSE', '86400'); - Console::loop(function() use ($interval){ + Console::loop(function() use ($interval, $executionLogsRetention, $abuseLogsRetention, $auditLogRetention){ $time = date('d-m-Y H:i:s', time()); Console::info("[{$time}] Notifying deletes workers every {$interval} seconds"); - notifyDeleteExecutionLogs(); - notifyDeleteAbuseLogs($interval); - notifyDeleteAuditLogs($interval); - + notifyDeleteExecutionLogs($executionLogsRetention); + notifyDeleteAbuseLogs($abuseLogsRetention); + notifyDeleteAuditLogs($auditLogRetention); }, $interval); - }); \ No newline at end of file diff --git a/app/views/install/compose.phtml b/app/views/install/compose.phtml index 098d4d407..231d2f857 100644 --- a/app/views/install/compose.phtml +++ b/app/views/install/compose.phtml @@ -279,11 +279,10 @@ services: - _APP_REDIS_HOST - _APP_REDIS_PORT - _APP_MAINTENANCE_INTERVAL - - _APP_DB_HOST - - _APP_DB_PORT - - _APP_DB_SCHEMA - - _APP_DB_USER - - _APP_DB_PASS + - _APP_MAINTENANCE_RETENTION_EXECUTION + - _APP_MAINTENANCE_RETENTION_ABUSE + - _APP_MAINTENANCE_RETENTION_AUDIT + appwrite-schedule: image: appwrite/appwrite: diff --git a/app/workers/deletes.php b/app/workers/deletes.php index cf9aa2c45..2aa1fddc9 100644 --- a/app/workers/deletes.php +++ b/app/workers/deletes.php @@ -5,7 +5,7 @@ use Appwrite\Database\Adapter\MySQL as MySQLAdapter; use Appwrite\Database\Adapter\Redis as RedisAdapter; use Appwrite\Database\Document; use Appwrite\Database\Validator\Authorization; -use Appwrite\Storage\Device\Local; +use Utopia\Storage\Device\Local; use Utopia\Abuse\Abuse; use Utopia\Abuse\Adapters\TimeLimit; use Utopia\CLI\Console; @@ -59,7 +59,7 @@ class DeletesV1 break; case DELETE_TYPE_EXECUTIONS: - $this->deleteExecutionLogs(); + $this->deleteExecutionLogs($this->args['timestamp']); break; case DELETE_TYPE_AUDIT: @@ -121,16 +121,17 @@ class DeletesV1 ], $this->getProjectDB($projectId)); } - protected function deleteExecutionLogs() + protected function deleteExecutionLogs($timestamp) { - $this->deleteForProjectIds(function($projectId) { + $this->deleteForProjectIds(function($projectId) use ($timestamp) { if (!($projectDB = $this->getProjectDB($projectId))) { throw new Exception('Failed to get projectDB for project '.$projectId); } // Delete Executions $this->deleteByGroup([ - '$collection='.Database::SYSTEM_COLLECTION_EXECUTIONS + '$collection='.Database::SYSTEM_COLLECTION_EXECUTIONS, + 'dateCreated<'.$timestamp ], $projectDB); }); } diff --git a/composer.json b/composer.json index 6b277f0d8..c29aa77be 100644 --- a/composer.json +++ b/composer.json @@ -46,6 +46,7 @@ "utopia-php/domains": "0.2.*", "utopia-php/swoole": "0.2.*", "utopia-php/system": "0.3.*", + "utopia-php/storage": "0.1.*", "resque/php-resque": "1.3.6", "matomo/device-detector": "3.13.0", diff --git a/composer.lock b/composer.lock index 5032ca969..f1b8773fb 100644 --- a/composer.lock +++ b/composer.lock @@ -1695,6 +1695,58 @@ }, "time": "2020-10-24T08:51:37+00:00" }, + { + "name": "utopia-php/storage", + "version": "0.1.0", + "source": { + "type": "git", + "url": "https://github.com/utopia-php/storage.git", + "reference": "00e9045cee6bd1ec1d1f27329c329494ef420c19" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/utopia-php/storage/zipball/00e9045cee6bd1ec1d1f27329c329494ef420c19", + "reference": "00e9045cee6bd1ec1d1f27329c329494ef420c19", + "shasum": "" + }, + "require": { + "php": ">=7.4", + "utopia-php/framework": "0.10.0" + }, + "require-dev": { + "phpunit/phpunit": "^9.3", + "vimeo/psalm": "4.0.1" + }, + "type": "library", + "autoload": { + "psr-4": { + "Utopia\\Storage\\": "src/Storage" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Eldad Fux", + "email": "eldad@appwrite.io" + } + ], + "description": "A simple Storage library to manage application storage", + "keywords": [ + "framework", + "php", + "storage", + "upf", + "utopia" + ], + "support": { + "issues": "https://github.com/utopia-php/storage/issues", + "source": "https://github.com/utopia-php/storage/tree/0.1.0" + }, + "time": "2021-01-22T07:27:20+00:00" + }, { "name": "utopia-php/swoole", "version": "0.2.0", diff --git a/docker-compose.yml b/docker-compose.yml index 1ced7fcea..6366e2ad2 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -330,6 +330,9 @@ services: - _APP_REDIS_HOST - _APP_REDIS_PORT - _APP_MAINTENANCE_INTERVAL + - _APP_MAINTENANCE_RETENTION_EXECUTION + - _APP_MAINTENANCE_RETENTION_ABUSE + - _APP_MAINTENANCE_RETENTION_AUDIT appwrite-schedule: entrypoint: schedule diff --git a/src/Appwrite/Specification/Format/OpenAPI3.php b/src/Appwrite/Specification/Format/OpenAPI3.php index 90f054d0a..117c7c93b 100644 --- a/src/Appwrite/Specification/Format/OpenAPI3.php +++ b/src/Appwrite/Specification/Format/OpenAPI3.php @@ -218,7 +218,7 @@ class OpenAPI3 extends Format $node['schema']['x-example'] = '{}'; //$node['schema']['format'] = 'json'; break; - case 'Appwrite\Storage\Validator\File': + case 'Utopia\Storage\Validator\File': $consumes = ['multipart/form-data']; $node['schema']['type'] = 'string'; $node['schema']['format'] = 'binary'; diff --git a/src/Appwrite/Specification/Format/Swagger2.php b/src/Appwrite/Specification/Format/Swagger2.php index 56becd236..fbe8ded4e 100644 --- a/src/Appwrite/Specification/Format/Swagger2.php +++ b/src/Appwrite/Specification/Format/Swagger2.php @@ -210,7 +210,7 @@ class Swagger2 extends Format $node['x-example'] = '{}'; //$node['format'] = 'json'; break; - case 'Appwrite\Storage\Validator\File': + case 'Utopia\Storage\Validator\File': $consumes = ['multipart/form-data']; $node['type'] = 'file'; break; diff --git a/src/Appwrite/Storage/Compression/Algorithms/GZIP.php b/src/Appwrite/Storage/Compression/Algorithms/GZIP.php deleted file mode 100644 index df4e0236b..000000000 --- a/src/Appwrite/Storage/Compression/Algorithms/GZIP.php +++ /dev/null @@ -1,45 +0,0 @@ -root = $root; - } - - /** - * @return string - */ - public function getName():string - { - return 'Local Storage'; - } - - /** - * @return string - */ - public function getDescription():string - { - return 'Adapter for Local storage that is in the physical or virtual machine or mounted to it.'; - } - - /** - * @return string - */ - public function getRoot():string - { - return $this->root; - } - - /** - * @param string $filename - * - * @return string - */ - public function getPath($filename):string - { - $path = ''; - - for ($i = 0; $i < 4; ++$i) { - $path = ($i < \strlen($filename)) ? $path.DIRECTORY_SEPARATOR.$filename[$i] : $path.DIRECTORY_SEPARATOR.'x'; - } - - return $this->getRoot().$path.DIRECTORY_SEPARATOR.$filename; - } - - /** - * Upload. - * - * Upload a file to desired destination in the selected disk. - * - * @param string $target - * @param string $filename - * - * @throws \Exception - * - * @return bool - */ - public function upload($source, $path):bool - { - if (!\file_exists(\dirname($path))) { // Checks if directory path to file exists - if (!@\mkdir(\dirname($path), 0755, true)) { - throw new Exception('Can\'t create directory: '.\dirname($path)); - } - } - - if (\move_uploaded_file($source, $path)) { - return true; - } - - return false; - } - - /** - * Read file by given path. - * - * @param string $path - * - * @return string - */ - public function read(string $path):string - { - return \file_get_contents($path); - } - - /** - * Write file by given path. - * - * @param string $path - * @param string $data - * - * @return bool - */ - public function write(string $path, string $data): bool - { - if (!\file_exists(\dirname($path))) { // Checks if directory path to file exists - if (!@\mkdir(\dirname($path), 0755, true)) { - throw new Exception('Can\'t create directory '.\dirname($path)); - } - } - - return (bool)\file_put_contents($path, $data); - } - - /** - * Move file from given source to given path, Return true on success and false on failure. - * - * @see http://php.net/manual/en/function.filesize.php - * - * @param string $source - * @param string $target - * - * @return bool - */ - public function move(string $source, string $target):bool - { - if (!\file_exists(\dirname($target))) { // Checks if directory path to file exists - if (!@\mkdir(\dirname($target), 0755, true)) { - throw new Exception('Can\'t create directory '.\dirname($target)); - } - } - - if (\rename($source, $target)) { - return true; - } - - return false; - } - - /** - * Delete file in given path, Return true on success and false on failure. - * - * @see http://php.net/manual/en/function.filesize.php - * - * @param string $path - * @param bool $recursive - * - * @return bool - */ - public function delete(string $path, bool $recursive = false):bool - { - if (\is_dir($path) && $recursive) { - $files = \glob($path.'*', GLOB_MARK); // GLOB_MARK adds a slash to directories returned - - foreach ($files as $file) { - $this->delete($file, true); - } - - \rmdir($path); - } elseif (\is_file($path)) { - return \unlink($path); - } - - return false; - } - - /** - * Returns given file path its size. - * - * @see http://php.net/manual/en/function.filesize.php - * - * @param $path - * - * @return int - */ - public function getFileSize(string $path):int - { - return \filesize($path); - } - - /** - * Returns given file path its mime type. - * - * @see http://php.net/manual/en/function.mime-content-type.php - * - * @param $path - * - * @return string - */ - public function getFileMimeType(string $path):string - { - return \mime_content_type($path); - } - - /** - * Returns given file path its MD5 hash value. - * - * @see http://php.net/manual/en/function.md5-file.php - * - * @param $path - * - * @return string - */ - public function getFileHash(string $path):string - { - return \md5_file($path); - } - - /** - * Get directory size in bytes. - * - * Return -1 on error - * - * Based on http://www.jonasjohn.de/snippets/php/dir-size.htm - * - * @param $path - * - * @return int - */ - public function getDirectorySize(string $path):int - { - $size = 0; - - $directory = \opendir($path); - - if (!$directory) { - return -1; - } - - while (($file = \readdir($directory)) !== false) { - // Skip file pointers - if ($file[0] == '.') { - continue; - } - - // Go recursive down, or add the file size - if (\is_dir($path.$file)) { - $size += $this->getDirectorySize($path.$file.DIRECTORY_SEPARATOR); - } else { - $size += \filesize($path.$file); - } - } - - \closedir($directory); - - return $size; - } - - /** - * Get Partition Free Space. - * - * disk_free_space — Returns available space on filesystem or disk partition - * - * @return float - */ - public function getPartitionFreeSpace():float - { - return \disk_free_space($this->getRoot()); - } - - /** - * Get Partition Total Space. - * - * disk_total_space — Returns the total size of a filesystem or disk partition - * - * @return float - */ - public function getPartitionTotalSpace():float - { - return \disk_total_space($this->getRoot()); - } -} diff --git a/src/Appwrite/Storage/Device/S3.php b/src/Appwrite/Storage/Device/S3.php deleted file mode 100644 index adfe7e8be..000000000 --- a/src/Appwrite/Storage/Device/S3.php +++ /dev/null @@ -1,196 +0,0 @@ - array( - 'B', - 'KiB', - 'MiB', - 'GiB', - 'TiB', - 'PiB', - 'EiB', - 'ZiB', - 'YiB', - ), - 'metric' => array( - 'B', - 'kB', - 'MB', - 'GB', - 'TB', - 'PB', - 'EB', - 'ZB', - 'YB', - ), - ); - - $factor = (int)floor((strlen((string)$bytes) - 1) / 3); - - return sprintf("%.{$decimals}f%s", $bytes / pow($mod, $factor), $units[$system][$factor]); - } -} \ No newline at end of file diff --git a/src/Appwrite/Storage/Validator/File.php b/src/Appwrite/Storage/Validator/File.php deleted file mode 100644 index 1ca7140bb..000000000 --- a/src/Appwrite/Storage/Validator/File.php +++ /dev/null @@ -1,27 +0,0 @@ -max = $max; - } - - public function getDescription() - { - return 'File size can\'t be bigger than '.$this->max; - } - - /** - * Finds whether a file size is smaller than required limit. - * - * @param mixed $fileSize - * - * @return bool - */ - public function isValid($fileSize) - { - if (!is_int($fileSize)) { - return false; - } - - if ($fileSize > $this->max) { - return false; - } - - return true; - } -} diff --git a/src/Appwrite/Storage/Validator/FileType.php b/src/Appwrite/Storage/Validator/FileType.php deleted file mode 100644 index 7b13a9839..000000000 --- a/src/Appwrite/Storage/Validator/FileType.php +++ /dev/null @@ -1,93 +0,0 @@ - "\xFF\xD8\xFF", - self::FILE_TYPE_GIF => 'GIF', - self::FILE_TYPE_PNG => "\x89\x50\x4e\x47\x0d\x0a", - self::FILE_TYPE_GZIP => "application/x-gzip", - ); - - /** - * @var array - */ - protected $whiteList; - - /** - * @param array $whiteList - * - * @throws Exception - */ - public function __construct(array $whiteList) - { - foreach ($whiteList as $key) { - if (!isset($this->types[$key])) { - throw new Exception('Unknown file mime type'); - } - } - - $this->whiteList = $whiteList; - } - - public function getDescription() - { - return 'File mime-type is not allowed '; - } - - /** - * Is Valid. - * - * Binary check to finds whether a file is of valid type - * - * @see http://stackoverflow.com/a/3313196 - * - * @param string $path - * - * @return bool - */ - public function isValid($path) - { - if (!\is_readable($path)) { - return false; - } - - $handle = \fopen($path, 'r'); - - if (!$handle) { - return false; - } - - $bytes = \fgets($handle, 8); - - foreach ($this->whiteList as $key) { - if (\strpos($bytes, $this->types[$key]) === 0) { - \fclose($handle); - - return true; - } - } - - \fclose($handle); - - return false; - } -} diff --git a/src/Appwrite/Storage/Validator/Upload.php b/src/Appwrite/Storage/Validator/Upload.php deleted file mode 100644 index 38296e8d0..000000000 --- a/src/Appwrite/Storage/Validator/Upload.php +++ /dev/null @@ -1,33 +0,0 @@ -object = new GZIP(); - } - - public function tearDown(): void - { - } - - public function testName() - { - $this->assertEquals($this->object->getName(), 'gzip'); - } - - public function testCompressDecompressWithText() - { - $demo = 'This is a demo string'; - $demoSize = mb_strlen($demo, '8bit'); - - $data = $this->object->compress($demo); - $dataSize = mb_strlen($data, '8bit'); - - $this->assertEquals($demoSize, 21); - $this->assertEquals($dataSize, 39); - - $this->assertEquals($this->object->decompress($data), $demo); - } - - public function testCompressDecompressWithJPGImage() - { - $demo = \file_get_contents(__DIR__ . '/../../../../resources/disk-a/kitten-1.jpg'); - $demoSize = mb_strlen($demo, '8bit'); - - $data = $this->object->compress($demo); - $dataSize = mb_strlen($data, '8bit'); - - $this->assertEquals($demoSize, 599639); - $this->assertEquals($dataSize, 599107); - - $this->assertGreaterThan($dataSize, $demoSize); - - $data = $this->object->decompress($data); - $dataSize = mb_strlen($data, '8bit'); - - $this->assertEquals($dataSize, 599639); - } - - public function testCompressDecompressWithPNGImage() - { - $demo = \file_get_contents(__DIR__ . '/../../../../resources/disk-b/kitten-1.png'); - $demoSize = mb_strlen($demo, '8bit'); - - $data = $this->object->compress($demo); - $dataSize = mb_strlen($data, '8bit'); - - $this->assertEquals($demoSize, 3038056); - $this->assertEquals($dataSize, 3029202); - - $this->assertGreaterThan($dataSize, $demoSize); - - $data = $this->object->decompress($data); - $dataSize = mb_strlen($data, '8bit'); - - $this->assertEquals($dataSize, 3038056); - } -} diff --git a/tests/unit/Storage/Device/LocalTest.php b/tests/unit/Storage/Device/LocalTest.php deleted file mode 100644 index c09c0edff..000000000 --- a/tests/unit/Storage/Device/LocalTest.php +++ /dev/null @@ -1,123 +0,0 @@ -object = new Local(realpath(__DIR__ . '/../../../resources/disk-a')); - } - - public function tearDown(): void - { - } - - public function testName() - { - $this->assertEquals($this->object->getName(), 'Local Storage'); - } - - public function testDescription() - { - $this->assertEquals($this->object->getDescription(), 'Adapter for Local storage that is in the physical or virtual machine or mounted to it.'); - } - - public function testRoot() - { - $this->assertEquals($this->object->getRoot(), '/usr/src/code/tests/resources/disk-a'); - } - - public function testPath() - { - $this->assertEquals($this->object->getPath('image.png'), '/usr/src/code/tests/resources/disk-a/i/m/a/g/image.png'); - $this->assertEquals($this->object->getPath('x.png'), '/usr/src/code/tests/resources/disk-a/x/./p/n/x.png'); - $this->assertEquals($this->object->getPath('y'), '/usr/src/code/tests/resources/disk-a/y/x/x/x/y'); - } - - public function testWrite() - { - $this->assertEquals($this->object->write($this->object->getPath('text.txt'), 'Hello World'), true); - $this->assertEquals(file_exists($this->object->getPath('text.txt')), true); - $this->assertEquals(is_readable($this->object->getPath('text.txt')), true); - - $this->object->delete($this->object->getPath('text.txt')); - } - - public function testRead() - { - $this->assertEquals($this->object->write($this->object->getPath('text-for-read.txt'), 'Hello World'), true); - $this->assertEquals($this->object->read($this->object->getPath('text-for-read.txt')), 'Hello World'); - - $this->object->delete($this->object->getPath('text-for-read.txt')); - } - - public function testMove() - { - $this->assertEquals($this->object->write($this->object->getPath('text-for-move.txt'), 'Hello World'), true); - $this->assertEquals($this->object->read($this->object->getPath('text-for-move.txt')), 'Hello World'); - $this->assertEquals($this->object->move($this->object->getPath('text-for-move.txt'), $this->object->getPath('text-for-move-new.txt')), true); - $this->assertEquals($this->object->read($this->object->getPath('text-for-move-new.txt')), 'Hello World'); - $this->assertEquals(file_exists($this->object->getPath('text-for-move.txt')), false); - $this->assertEquals(is_readable($this->object->getPath('text-for-move.txt')), false); - $this->assertEquals(file_exists($this->object->getPath('text-for-move-new.txt')), true); - $this->assertEquals(is_readable($this->object->getPath('text-for-move-new.txt')), true); - - $this->object->delete($this->object->getPath('text-for-move-new.txt')); - } - - public function testDelete() - { - $this->assertEquals($this->object->write($this->object->getPath('text-for-delete.txt'), 'Hello World'), true); - $this->assertEquals($this->object->read($this->object->getPath('text-for-delete.txt')), 'Hello World'); - $this->assertEquals($this->object->delete($this->object->getPath('text-for-delete.txt')), true); - $this->assertEquals(file_exists($this->object->getPath('text-for-delete.txt')), false); - $this->assertEquals(is_readable($this->object->getPath('text-for-delete.txt')), false); - } - - public function testFileSize() - { - $this->assertEquals($this->object->getFileSize(__DIR__ . '/../../../resources/disk-a/kitten-1.jpg'), 599639); - $this->assertEquals($this->object->getFileSize(__DIR__ . '/../../../resources/disk-a/kitten-2.jpg'), 131958); - } - - public function testFileMimeType() - { - $this->assertEquals($this->object->getFileMimeType(__DIR__ . '/../../../resources/disk-a/kitten-1.jpg'), 'image/jpeg'); - $this->assertEquals($this->object->getFileMimeType(__DIR__ . '/../../../resources/disk-a/kitten-2.jpg'), 'image/jpeg'); - $this->assertEquals($this->object->getFileMimeType(__DIR__ . '/../../../resources/disk-b/kitten-1.png'), 'image/png'); - $this->assertEquals($this->object->getFileMimeType(__DIR__ . '/../../../resources/disk-b/kitten-2.png'), 'image/png'); - } - - public function testFileHash() - { - $this->assertEquals($this->object->getFileHash(__DIR__ . '/../../../resources/disk-a/kitten-1.jpg'), '7551f343143d2e24ab4aaf4624996b6a'); - $this->assertEquals($this->object->getFileHash(__DIR__ . '/../../../resources/disk-a/kitten-2.jpg'), '81702fdeef2e55b1a22617bce4951cb5'); - $this->assertEquals($this->object->getFileHash(__DIR__ . '/../../../resources/disk-b/kitten-1.png'), '03010f4f02980521a8fd6213b52ec313'); - $this->assertEquals($this->object->getFileHash(__DIR__ . '/../../../resources/disk-b/kitten-2.png'), '8a9ed992b77e4b62b10e3a5c8ed72062'); - } - - public function testDirectorySize() - { - $this->assertGreaterThan(0, $this->object->getDirectorySize(__DIR__ . '/../../../resources/disk-a/')); - $this->assertGreaterThan(0, $this->object->getDirectorySize(__DIR__ . '/../../../resources/disk-b/')); - } - - public function testPartitionFreeSpace() - { - $this->assertGreaterThan(0, $this->object->getPartitionFreeSpace()); - } - - public function testPartitionTotalSpace() - { - $this->assertGreaterThan(0, $this->object->getPartitionTotalSpace()); - } -} diff --git a/tests/unit/Storage/StorageTest.php b/tests/unit/Storage/StorageTest.php deleted file mode 100644 index 4d3196612..000000000 --- a/tests/unit/Storage/StorageTest.php +++ /dev/null @@ -1,42 +0,0 @@ -assertEquals(get_class(Storage::getDevice('disk-a')), 'Appwrite\Storage\Device\Local'); - $this->assertEquals(get_class(Storage::getDevice('disk-b')), 'Appwrite\Storage\Device\Local'); - - try { - get_class(Storage::getDevice('disk-c')); - $this->fail("Expected exception not thrown"); - } catch (Exception $e) { - $this->assertEquals('The device "disk-c" is not listed', $e->getMessage()); - } - } - - public function testExists() - { - $this->assertEquals(Storage::exists('disk-a'), true); - $this->assertEquals(Storage::exists('disk-b'), true); - $this->assertEquals(Storage::exists('disk-c'), false); - } -} diff --git a/tests/unit/Storage/Validator/FileNameTest.php b/tests/unit/Storage/Validator/FileNameTest.php deleted file mode 100644 index fcd2d1e7e..000000000 --- a/tests/unit/Storage/Validator/FileNameTest.php +++ /dev/null @@ -1,33 +0,0 @@ -object = new FileName(); - } - - public function tearDown(): void - { - } - - public function testValues() - { - $this->assertEquals($this->object->isValid(''), false); - $this->assertEquals($this->object->isValid(null), false); - $this->assertEquals($this->object->isValid(false), false); - $this->assertEquals($this->object->isValid('../test'), false); - $this->assertEquals($this->object->isValid('test.png'), true); - $this->assertEquals($this->object->isValid('test'), true); - } -} diff --git a/tests/unit/Storage/Validator/FileSizeTest.php b/tests/unit/Storage/Validator/FileSizeTest.php deleted file mode 100644 index b4044dbc0..000000000 --- a/tests/unit/Storage/Validator/FileSizeTest.php +++ /dev/null @@ -1,30 +0,0 @@ -object = new FileSize(1000); - } - - public function tearDown(): void - { - } - - public function testValues() - { - $this->assertEquals($this->object->isValid(1001), false); - $this->assertEquals($this->object->isValid(1000), true); - $this->assertEquals($this->object->isValid(999), true); - } -} diff --git a/tests/unit/Storage/Validator/FileTypeTest.php b/tests/unit/Storage/Validator/FileTypeTest.php deleted file mode 100644 index 7041efefb..000000000 --- a/tests/unit/Storage/Validator/FileTypeTest.php +++ /dev/null @@ -1,31 +0,0 @@ -object = new FileType([FileType::FILE_TYPE_JPEG]); - } - - public function tearDown(): void - { - } - - public function testValues() - { - $this->assertEquals($this->object->isValid(__DIR__ . '/../../../resources/disk-a/kitten-1.jpg'), true); - $this->assertEquals($this->object->isValid(__DIR__ . '/../../../resources/disk-a/kitten-2.jpg'), true); - $this->assertEquals($this->object->isValid(__DIR__ . '/../../../resources/disk-b/kitten-1.png'), false); - $this->assertEquals($this->object->isValid(__DIR__ . '/../../../resources/disk-b/kitten-2.png'), false); - } -} diff --git a/tests/unit/Storage/Validator/UploadTest.php b/tests/unit/Storage/Validator/UploadTest.php deleted file mode 100644 index 4d360ef41..000000000 --- a/tests/unit/Storage/Validator/UploadTest.php +++ /dev/null @@ -1,32 +0,0 @@ -object = new Upload(); - } - - public function tearDown(): void - { - } - - public function testValues() - { - $this->assertEquals($this->object->isValid(__DIR__ . '/../../../resources/disk-a/kitten-1.jpg'), false); - $this->assertEquals($this->object->isValid(__DIR__ . '/../../../resources/disk-a/kitten-2.jpg'), false); - $this->assertEquals($this->object->isValid(__DIR__ . '/../../../resources/disk-b/kitten-1.png'), false); - $this->assertEquals($this->object->isValid(__DIR__ . '/../../../resources/disk-b/kitten-2.png'), false); - $this->assertEquals($this->object->isValid(__FILE__), false); - } -}