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..76d0c5db8 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; 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/workers/deletes.php b/app/workers/deletes.php index ac168d6bc..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; diff --git a/composer.json b/composer.json index 7384811bd..973592f48 100644 --- a/composer.json +++ b/composer.json @@ -45,6 +45,7 @@ "utopia-php/preloader": "0.2.*", "utopia-php/domains": "0.2.*", "utopia-php/swoole": "0.2.*", + "utopia-php/storage": "0.1.*", "resque/php-resque": "1.3.6", "matomo/device-detector": "3.13.0", 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 @@ -assertEquals(get_class(Storage::getDevice('disk-a')), 'Appwrite\Storage\Device\Local'); - $this->assertEquals(get_class(Storage::getDevice('disk-b')), 'Appwrite\Storage\Device\Local'); + $this->assertEquals(get_class(Storage::getDevice('disk-a')), 'Utopia\Storage\Device\Local'); + $this->assertEquals(get_class(Storage::getDevice('disk-b')), 'Utopia\Storage\Device\Local'); try { get_class(Storage::getDevice('disk-c')); diff --git a/tests/unit/Storage/Validator/FileNameTest.php b/tests/unit/Storage/Validator/FileNameTest.php index fcd2d1e7e..833cd4d67 100644 --- a/tests/unit/Storage/Validator/FileNameTest.php +++ b/tests/unit/Storage/Validator/FileNameTest.php @@ -2,7 +2,7 @@ namespace Appwrite\Tests; -use Appwrite\Storage\Validator\FileName; +use Utopia\Storage\Validator\FileName; use PHPUnit\Framework\TestCase; class FileNameTest extends TestCase diff --git a/tests/unit/Storage/Validator/FileSizeTest.php b/tests/unit/Storage/Validator/FileSizeTest.php index b4044dbc0..903c8703a 100644 --- a/tests/unit/Storage/Validator/FileSizeTest.php +++ b/tests/unit/Storage/Validator/FileSizeTest.php @@ -2,7 +2,7 @@ namespace Appwrite\Tests; -use Appwrite\Storage\Validator\FileSize; +use Utopia\Storage\Validator\FileSize; use PHPUnit\Framework\TestCase; class FileSizeTest extends TestCase diff --git a/tests/unit/Storage/Validator/FileTypeTest.php b/tests/unit/Storage/Validator/FileTypeTest.php index 7041efefb..8543c4ad6 100644 --- a/tests/unit/Storage/Validator/FileTypeTest.php +++ b/tests/unit/Storage/Validator/FileTypeTest.php @@ -2,7 +2,7 @@ namespace Appwrite\Tests; -use Appwrite\Storage\Validator\FileType; +use Utopia\Storage\Validator\FileType; use PHPUnit\Framework\TestCase; class FileTypeTest extends TestCase diff --git a/tests/unit/Storage/Validator/UploadTest.php b/tests/unit/Storage/Validator/UploadTest.php index 4d360ef41..a42f9c371 100644 --- a/tests/unit/Storage/Validator/UploadTest.php +++ b/tests/unit/Storage/Validator/UploadTest.php @@ -2,7 +2,7 @@ namespace Appwrite\Tests; -use Appwrite\Storage\Validator\Upload; +use Utopia\Storage\Validator\Upload; use PHPUnit\Framework\TestCase; class UploadTest extends TestCase