Switching to utopia-php/storage
This commit is contained in:
parent
1811e1e3b3
commit
d4c7ab045a
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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')
|
||||
|
|
|
@ -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 */
|
||||
|
|
|
@ -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 */
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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';
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -1,45 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Appwrite\Storage\Compression\Algorithms;
|
||||
|
||||
use Appwrite\Storage\Compression\Compression;
|
||||
|
||||
class GZIP extends Compression
|
||||
{
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getName(): string
|
||||
{
|
||||
return 'gzip';
|
||||
}
|
||||
|
||||
/**
|
||||
* Compress.
|
||||
*
|
||||
* We use gzencode over gzcompress for better support of the first format among other tools.
|
||||
* (http://stackoverflow.com/a/621987/2299554)
|
||||
*
|
||||
* @see http://php.net/manual/en/function.gzencode.php
|
||||
*
|
||||
* @param string $data
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function compress(string $data):string
|
||||
{
|
||||
return \gzencode($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Decompress.
|
||||
*
|
||||
* @param string $data
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function decompress(string $data):string
|
||||
{
|
||||
return \gzdecode($data);
|
||||
}
|
||||
}
|
|
@ -1,27 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Appwrite\Storage\Compression;
|
||||
|
||||
abstract class Compression
|
||||
{
|
||||
/**
|
||||
* Return the name of compression algorithm.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
abstract public function getName(): string;
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
abstract public function compress(string $data);
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
abstract public function decompress(string $data);
|
||||
}
|
|
@ -1,167 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Appwrite\Storage;
|
||||
|
||||
use Exception;
|
||||
|
||||
abstract class Device
|
||||
{
|
||||
/**
|
||||
* Get Name.
|
||||
*
|
||||
* Get storage device name
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
abstract public function getName(): string;
|
||||
|
||||
/**
|
||||
* Get Description.
|
||||
*
|
||||
* Get storage device description and purpose.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
abstract public function getDescription(): string;
|
||||
|
||||
/**
|
||||
* Get Root.
|
||||
*
|
||||
* Get storage device root path
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
abstract public function getRoot(): string;
|
||||
|
||||
/**
|
||||
* Get Path.
|
||||
*
|
||||
* Each device hold a complex directory structure that is being build in this method.
|
||||
*
|
||||
* @param $filename
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
abstract public function getPath($filename): string;
|
||||
|
||||
/**
|
||||
* Upload.
|
||||
*
|
||||
* Upload a file to desired destination in the selected disk, return true on success and false on failure.
|
||||
*
|
||||
* @param string $source
|
||||
* @param string $path
|
||||
*
|
||||
* @throws \Exception
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
abstract public function upload($source, $path): bool;
|
||||
|
||||
/**
|
||||
* Read file by given path.
|
||||
*
|
||||
* @param string $path
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
abstract public function read(string $path): string;
|
||||
|
||||
/**
|
||||
* Write file by given path.
|
||||
*
|
||||
* @param string $path
|
||||
* @param string $data
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
abstract public function write(string $path, string $data): bool;
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
abstract public function move(string $source, string $target): bool;
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
abstract public function delete(string $path, bool $recursive = false): bool;
|
||||
|
||||
/**
|
||||
* Returns given file path its size.
|
||||
*
|
||||
* @see http://php.net/manual/en/function.filesize.php
|
||||
*
|
||||
* @param $path
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
abstract public function getFileSize(string $path): int;
|
||||
|
||||
/**
|
||||
* Returns given file path its mime type.
|
||||
*
|
||||
* @see http://php.net/manual/en/function.mime-content-type.php
|
||||
*
|
||||
* @param $path
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
abstract public function getFileMimeType(string $path): string;
|
||||
|
||||
/**
|
||||
* Returns given file path its MD5 hash value.
|
||||
*
|
||||
* @see http://php.net/manual/en/function.md5-file.php
|
||||
*
|
||||
* @param $path
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
abstract public function getFileHash(string $path): string;
|
||||
|
||||
/**
|
||||
* Get directory size in bytes.
|
||||
*
|
||||
* Return -1 on error
|
||||
*
|
||||
* Based on http://www.jonasjohn.de/snippets/php/dir-size.htm
|
||||
*
|
||||
* @param $path
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
abstract public function getDirectorySize(string $path): int;
|
||||
|
||||
/**
|
||||
* Get Partition Free Space.
|
||||
*
|
||||
* disk_free_space — Returns available space on filesystem or disk partition
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
abstract public function getPartitionFreeSpace(): float;
|
||||
|
||||
/**
|
||||
* Get Partition Total Space.
|
||||
*
|
||||
* disk_total_space — Returns the total size of a filesystem or disk partition
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
abstract public function getPartitionTotalSpace(): float;
|
||||
}
|
|
@ -1,280 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Appwrite\Storage\Device;
|
||||
|
||||
use Exception;
|
||||
use Appwrite\Storage\Device;
|
||||
|
||||
class Local extends Device
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $root = 'temp';
|
||||
|
||||
/**
|
||||
* Local constructor.
|
||||
*
|
||||
* @param string $root
|
||||
*/
|
||||
public function __construct($root = '')
|
||||
{
|
||||
$this->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());
|
||||
}
|
||||
}
|
|
@ -1,196 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Appwrite\Storage\Device;
|
||||
|
||||
use Appwrite\Storage\Device;
|
||||
|
||||
class S3 extends Device
|
||||
{
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getName():string
|
||||
{
|
||||
return 'S3 Storage';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getDescription():string
|
||||
{
|
||||
return 'S3 Bucket Storage drive for AWS or on premise solution';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getRoot():string
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $filename
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getPath($filename):string
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read file by given path.
|
||||
*
|
||||
* @param string $path
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function read(string $path):string
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Write file by given path.
|
||||
*
|
||||
* @param string $path
|
||||
* @param string $data
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function write(string $path, string $data):bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
{
|
||||
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
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function delete(string $path, bool $recursive = false):bool
|
||||
{
|
||||
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 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 '';
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 '';
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Partition Free Space.
|
||||
*
|
||||
* disk_free_space — Returns available space on filesystem or disk partition
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
public function getPartitionFreeSpace():float
|
||||
{
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Partition Total Space.
|
||||
*
|
||||
* disk_total_space — Returns the total size of a filesystem or disk partition
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
public function getPartitionTotalSpace():float
|
||||
{
|
||||
return 0.0;
|
||||
}
|
||||
}
|
|
@ -1,113 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Appwrite\Storage;
|
||||
|
||||
use Exception;
|
||||
|
||||
class Storage
|
||||
{
|
||||
/**
|
||||
* Devices.
|
||||
*
|
||||
* List of all available storage devices
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $devices = [];
|
||||
|
||||
/**
|
||||
* Set Device.
|
||||
*
|
||||
* Add device by name
|
||||
*
|
||||
* @param string $name
|
||||
* @param Device $device
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function setDevice($name, Device $device): void
|
||||
{
|
||||
self::$devices[$name] = $device;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Device.
|
||||
*
|
||||
* Get device by name
|
||||
*
|
||||
* @param string $name
|
||||
*
|
||||
* @return Device
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function getDevice($name)
|
||||
{
|
||||
if (!\array_key_exists($name, self::$devices)) {
|
||||
throw new Exception('The device "'.$name.'" is not listed');
|
||||
}
|
||||
|
||||
return self::$devices[$name];
|
||||
}
|
||||
|
||||
/**
|
||||
* Exists.
|
||||
*
|
||||
* Checks if given storage name is registered or not
|
||||
*
|
||||
* @param string $name
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function exists($name)
|
||||
{
|
||||
return (bool) \array_key_exists($name, self::$devices);
|
||||
}
|
||||
|
||||
/**
|
||||
* Human readable data size format from bytes input.
|
||||
*
|
||||
* Based on: https://stackoverflow.com/a/38659168/2299554
|
||||
*
|
||||
* @param int $bytes
|
||||
* @param int $decimals
|
||||
* @param string $system
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function human(int $bytes, $decimals = 2, $system = 'metric')
|
||||
{
|
||||
$mod = ($system === 'binary') ? 1024 : 1000;
|
||||
|
||||
$units = array(
|
||||
'binary' => 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]);
|
||||
}
|
||||
}
|
|
@ -1,27 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Appwrite\Storage\Validator;
|
||||
|
||||
use Utopia\Validator;
|
||||
|
||||
class File extends Validator
|
||||
{
|
||||
public function getDescription()
|
||||
{
|
||||
return 'File is not valid';
|
||||
}
|
||||
|
||||
/**
|
||||
* NOT MUCH RIGHT NOW.
|
||||
*
|
||||
* TODO think what to do here, currently only used for parameter to be present in SDKs
|
||||
*
|
||||
* @param mixed $name
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isValid($name)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -1,37 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Appwrite\Storage\Validator;
|
||||
|
||||
use Utopia\Validator;
|
||||
|
||||
class FileName extends Validator
|
||||
{
|
||||
public function getDescription()
|
||||
{
|
||||
return 'Filename is not valid';
|
||||
}
|
||||
|
||||
/**
|
||||
* The file name can only contain "a-z", "A-Z", "0-9" and "-" and not empty.
|
||||
*
|
||||
* @param mixed $name
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isValid($name)
|
||||
{
|
||||
if (empty($name)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!is_string($name)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!\preg_match('/^[a-zA-Z0-9.]+$/', $name)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -1,48 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Appwrite\Storage\Validator;
|
||||
|
||||
use Utopia\Validator;
|
||||
|
||||
class FileSize extends Validator
|
||||
{
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected $max;
|
||||
|
||||
/**
|
||||
* Max size in bytes
|
||||
*
|
||||
* @param int $max
|
||||
*/
|
||||
public function __construct($max)
|
||||
{
|
||||
$this->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;
|
||||
}
|
||||
}
|
|
@ -1,93 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Appwrite\Storage\Validator;
|
||||
|
||||
use Exception;
|
||||
use Utopia\Validator;
|
||||
|
||||
class FileType extends Validator
|
||||
{
|
||||
/**
|
||||
* File Types Constants.
|
||||
*/
|
||||
const FILE_TYPE_JPEG = 'jpeg';
|
||||
const FILE_TYPE_GIF = 'gif';
|
||||
const FILE_TYPE_PNG = 'png';
|
||||
const FILE_TYPE_GZIP = 'gz';
|
||||
|
||||
/**
|
||||
* File Type Binaries.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $types = array(
|
||||
self::FILE_TYPE_JPEG => "\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;
|
||||
}
|
||||
}
|
|
@ -1,33 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Appwrite\Storage\Validator;
|
||||
|
||||
use Utopia\Validator;
|
||||
|
||||
class Upload extends Validator
|
||||
{
|
||||
public function getDescription()
|
||||
{
|
||||
return 'Not a valid upload file';
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a file is a valid upload file
|
||||
*
|
||||
* @param mixed $path
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isValid($path)
|
||||
{
|
||||
if (!is_string($path)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (\is_uploaded_file($path)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace Appwrite\Tests;
|
||||
|
||||
use Appwrite\Storage\Compression\Algorithms\GZIP;
|
||||
use Utopia\Storage\Compression\Algorithms\GZIP;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class GZIPTest extends TestCase
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace Appwrite\Tests;
|
||||
|
||||
use Appwrite\Storage\Device\Local;
|
||||
use Utopia\Storage\Device\Local;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class LocalTest extends TestCase
|
||||
|
|
|
@ -3,8 +3,8 @@
|
|||
namespace Appwrite\Tests;
|
||||
|
||||
use Exception;
|
||||
use Appwrite\Storage\Storage;
|
||||
use Appwrite\Storage\Device\Local;
|
||||
use Utopia\Storage\Storage;
|
||||
use Utopia\Storage\Device\Local;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
Storage::setDevice('disk-a', new Local(__DIR__ . '/../../resources/disk-a'));
|
||||
|
@ -22,8 +22,8 @@ class StorageTest extends TestCase
|
|||
|
||||
public function testGetters()
|
||||
{
|
||||
$this->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'));
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue