mirror of
https://github.com/appwrite/appwrite.git
synced 2026-05-26 13:51:13 +00:00
Run PHP-CS-FIXER to make sure
code is consisted with PSR-1 + PSR-2
This commit is contained in:
1 parent
c9d3448571
commit
35680bbae9
37 files changed
+928
-753
No files matched your search
@@ -15,7 +15,7 @@ class GZIP extends Compression
|
||||
}
|
||||
|
||||
/**
|
||||
* Compress
|
||||
* Compress.
|
||||
*
|
||||
* We use gzencode over gzcompress for better support of the first format among other tools.
|
||||
* (http://stackoverflow.com/a/621987/2299554)
|
||||
@@ -23,6 +23,7 @@ class GZIP extends Compression
|
||||
* @see http://php.net/manual/en/function.gzencode.php
|
||||
*
|
||||
* @param string $data
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function compress(string $data):string
|
||||
@@ -31,13 +32,14 @@ class GZIP extends Compression
|
||||
}
|
||||
|
||||
/**
|
||||
* Decompress
|
||||
* Decompress.
|
||||
*
|
||||
* @param string $data
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function decompress(string $data):string
|
||||
{
|
||||
return gzdecode($data);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@ namespace Storage\Compression;
|
||||
abstract class Compression
|
||||
{
|
||||
/**
|
||||
* Return the name of compression algorithm
|
||||
* Return the name of compression algorithm.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
@@ -13,13 +13,15 @@ abstract class Compression
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
abstract public function compress(string $data);
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
abstract public function decompress(string $data);
|
||||
}
|
||||
}
|
||||
+58
-45
@@ -7,7 +7,7 @@ use Exception;
|
||||
abstract class Device
|
||||
{
|
||||
/**
|
||||
* Get Name
|
||||
* Get Name.
|
||||
*
|
||||
* Get storage device name
|
||||
*
|
||||
@@ -16,7 +16,7 @@ abstract class Device
|
||||
abstract public function getName();
|
||||
|
||||
/**
|
||||
* Get Description
|
||||
* Get Description.
|
||||
*
|
||||
* Get storage device description and purpose.
|
||||
*
|
||||
@@ -25,7 +25,7 @@ abstract class Device
|
||||
abstract public function getDescription();
|
||||
|
||||
/**
|
||||
* Get Root
|
||||
* Get Root.
|
||||
*
|
||||
* Get storage device root path
|
||||
*
|
||||
@@ -34,42 +34,45 @@ abstract class Device
|
||||
abstract public function getRoot();
|
||||
|
||||
/**
|
||||
* Get Path
|
||||
* Get Path.
|
||||
*
|
||||
* Each device hold a complex directory structure that is being build in this method.
|
||||
*
|
||||
* @param $filename
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getPath($filename)
|
||||
{
|
||||
return $this->getRoot() . DIRECTORY_SEPARATOR . $filename;
|
||||
return $this->getRoot().DIRECTORY_SEPARATOR.$filename;
|
||||
}
|
||||
|
||||
/**
|
||||
* Upload
|
||||
* Upload.
|
||||
*
|
||||
* Upload a file to desired destination in the selected disk.
|
||||
*
|
||||
* @param string $target
|
||||
* @param string $filename
|
||||
* @param string $target
|
||||
* @param string $filename
|
||||
*
|
||||
* @throws \Exception
|
||||
*
|
||||
* @return string|bool saved destination on success or false on failures
|
||||
*/
|
||||
public function upload($target, $filename = '')
|
||||
{
|
||||
$filename = (empty($filename)) ? $target : $filename;
|
||||
$filename = uniqid() . '.' . pathinfo($filename, PATHINFO_EXTENSION);
|
||||
$filename = (empty($filename)) ? $target : $filename;
|
||||
$filename = uniqid().'.'.pathinfo($filename, PATHINFO_EXTENSION);
|
||||
|
||||
$path = $this->getPath($filename);
|
||||
$path = $this->getPath($filename);
|
||||
|
||||
if(!is_uploaded_file($target)) {
|
||||
if (!is_uploaded_file($target)) {
|
||||
throw new Exception('File is not a valid uploaded file');
|
||||
}
|
||||
|
||||
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 (!mkdir(dirname($path), 0755, true)) {
|
||||
throw new Exception('Can\'t create directory '.dirname($path));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,9 +84,10 @@ abstract class Device
|
||||
}
|
||||
|
||||
/**
|
||||
* Read file by given path
|
||||
* Read file by given path.
|
||||
*
|
||||
* @param string $path
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function read(string $path):string
|
||||
@@ -92,10 +96,11 @@ abstract class Device
|
||||
}
|
||||
|
||||
/**
|
||||
* Write file by given path
|
||||
* Write file by given path.
|
||||
*
|
||||
* @param string $path
|
||||
* @param string $data
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function write(string $path, string $data):bool
|
||||
@@ -104,11 +109,12 @@ abstract class Device
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete file in given path, Return true on success and false on failure
|
||||
* 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
|
||||
@@ -117,37 +123,38 @@ abstract class Device
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete all file and directories in given path, Return true on success and false on failure
|
||||
* Delete all file and directories in given path, Return true on success and false on failure.
|
||||
*
|
||||
* @see https://paulund.co.uk/php-delete-directory-and-files-in-directory
|
||||
*
|
||||
* @param string $path
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function deleteDir($target):bool
|
||||
{
|
||||
if (is_dir($target)) {
|
||||
$files = glob($target . '*', GLOB_MARK); // GLOB_MARK adds a slash to directories returned
|
||||
|
||||
$files = glob($target.'*', GLOB_MARK); // GLOB_MARK adds a slash to directories returned
|
||||
|
||||
foreach ($files as $file) {
|
||||
$this->deleteDir($file);
|
||||
$this->deleteDir($file);
|
||||
}
|
||||
|
||||
|
||||
rmdir($target);
|
||||
}
|
||||
elseif (is_file($target)) {
|
||||
unlink( $target );
|
||||
} elseif (is_file($target)) {
|
||||
unlink($target);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns given file path its size
|
||||
* 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
|
||||
@@ -156,11 +163,12 @@ abstract class Device
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns given file path its mime type
|
||||
* 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
|
||||
@@ -169,11 +177,12 @@ abstract class Device
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns given file path its MD5 hash value
|
||||
* 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
|
||||
@@ -182,13 +191,14 @@ abstract class Device
|
||||
}
|
||||
|
||||
/**
|
||||
* Get directory size in bytes
|
||||
* 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
|
||||
@@ -197,19 +207,21 @@ abstract class Device
|
||||
|
||||
$directory = opendir($path);
|
||||
|
||||
if (!$directory)
|
||||
if (!$directory) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
while (($file = readdir($directory)) !== false) {
|
||||
// Skip file pointers
|
||||
if ($file[0] == '.') continue;
|
||||
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);
|
||||
if (is_dir($path.$file)) {
|
||||
$size += $this->getDirectorySize($path.$file.DIRECTORY_SEPARATOR);
|
||||
} else {
|
||||
$size += filesize($path.$file);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -219,7 +231,7 @@ abstract class Device
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Partition Free Space
|
||||
* Get Partition Free Space.
|
||||
*
|
||||
* disk_free_space — Returns available space on filesystem or disk partition
|
||||
*
|
||||
@@ -231,7 +243,7 @@ abstract class Device
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Partition Total Space
|
||||
* Get Partition Total Space.
|
||||
*
|
||||
* disk_total_space — Returns the total size of a filesystem or disk partition
|
||||
*
|
||||
@@ -243,25 +255,26 @@ abstract class Device
|
||||
}
|
||||
|
||||
/**
|
||||
* Human readable data size format from bytes input
|
||||
* Human readable data size format from bytes input.
|
||||
*
|
||||
* As published on https://gist.github.com/liunian/9338301 (first comment)
|
||||
*
|
||||
* @param int $bytes
|
||||
* @param int $decimals
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function human($bytes, $decimals = 2)
|
||||
{
|
||||
$units = array('B','kB','MB','GB','TB','PB','EB','ZB','YB');
|
||||
$step = 1024;
|
||||
$i = 0;
|
||||
$units = array('B','kB','MB','GB','TB','PB','EB','ZB','YB');
|
||||
$step = 1024;
|
||||
$i = 0;
|
||||
|
||||
while (($bytes / $step) > 0.9) {
|
||||
$bytes = $bytes / $step;
|
||||
$i++;
|
||||
++$i;
|
||||
}
|
||||
|
||||
return round($bytes, $decimals) . $units[$i];
|
||||
return round($bytes, $decimals).$units[$i];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,7 @@ class Local extends Device
|
||||
|
||||
/**
|
||||
* Local constructor.
|
||||
*
|
||||
* @param string $root
|
||||
*/
|
||||
public function __construct($root = '')
|
||||
@@ -41,21 +42,22 @@ class Local extends Device
|
||||
*/
|
||||
public function getRoot()
|
||||
{
|
||||
return '/storage/uploads/' . $this->root;
|
||||
return '/storage/uploads/'.$this->root;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $filename
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getPath($filename)
|
||||
{
|
||||
$path = '';
|
||||
|
||||
for ($i = 0; $i < 4; $i++) {
|
||||
$path = ($i < strlen($filename)) ? $path . DIRECTORY_SEPARATOR . $filename[$i] : $path . DIRECTORY_SEPARATOR . 'x';
|
||||
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;
|
||||
return $this->getRoot().$path.DIRECTORY_SEPARATOR.$filename;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -32,16 +32,17 @@ class S3 extends Device
|
||||
|
||||
/**
|
||||
* @param string $filename
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getPath($filename)
|
||||
{
|
||||
$path = '';
|
||||
|
||||
for ($i = 0; $i < 4; $i++) {
|
||||
$path = ($i < strlen($filename)) ? $path . DIRECTORY_SEPARATOR . $filename[$i] : $path . DIRECTORY_SEPARATOR . 'x';
|
||||
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;
|
||||
return $this->getRoot().$path.DIRECTORY_SEPARATOR.$filename;
|
||||
}
|
||||
}
|
||||
}
|
||||
+17
-13
@@ -7,60 +7,64 @@ use Exception;
|
||||
class Storage
|
||||
{
|
||||
/**
|
||||
* Devices
|
||||
* Devices.
|
||||
*
|
||||
* List of all available storage devices
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
static $devices = array();
|
||||
public static $devices = array();
|
||||
|
||||
/**
|
||||
* Add Device
|
||||
* Add Device.
|
||||
*
|
||||
* Add device by name
|
||||
*
|
||||
* @param string $name
|
||||
* @param Device $device
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
static public function addDevice($name, Device $device)
|
||||
public static function addDevice($name, Device $device)
|
||||
{
|
||||
if(array_key_exists($name, self::$devices)) {
|
||||
throw new Exception('The device "' . $name . '" is already listed');
|
||||
if (array_key_exists($name, self::$devices)) {
|
||||
throw new Exception('The device "'.$name.'" is already listed');
|
||||
}
|
||||
|
||||
self::$devices[$name] = $device;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Device
|
||||
* Get Device.
|
||||
*
|
||||
* Get device by name
|
||||
*
|
||||
* @param string $name
|
||||
*
|
||||
* @return Device
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
static public function getDevice($name)
|
||||
public static function getDevice($name)
|
||||
{
|
||||
if(!array_key_exists($name, self::$devices)) {
|
||||
throw new Exception('The device "' . $name . '" is not listed');
|
||||
if (!array_key_exists($name, self::$devices)) {
|
||||
throw new Exception('The device "'.$name.'" is not listed');
|
||||
}
|
||||
|
||||
return self::$devices[$name];
|
||||
}
|
||||
|
||||
/**
|
||||
* Exists
|
||||
* Exists.
|
||||
*
|
||||
* Checks if given storage name is registered or not
|
||||
*
|
||||
* @param string $name
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
static public function exists($name)
|
||||
public static function exists($name)
|
||||
{
|
||||
return (bool)array_key_exists($name, self::$devices);
|
||||
return (bool) array_key_exists($name, self::$devices);
|
||||
}
|
||||
}
|
||||
@@ -6,18 +6,18 @@ use Utopia\Validator;
|
||||
|
||||
class File extends Validator
|
||||
{
|
||||
|
||||
public function getDescription()
|
||||
{
|
||||
return 'File is not valid';
|
||||
}
|
||||
|
||||
/**
|
||||
* NOT MUCH RIGHT NOW
|
||||
* NOT MUCH RIGHT NOW.
|
||||
*
|
||||
* TODO think what to do here, currently only used for parameter to be present in SDKs
|
||||
*
|
||||
* @param string $name
|
||||
* @param string $name
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isValid($name)
|
||||
|
||||
@@ -6,25 +6,25 @@ 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
|
||||
* The file name can only contain "a-z", "A-Z", "0-9" and "-" and not empty.
|
||||
*
|
||||
* @param string $name
|
||||
*
|
||||
* @param string $name
|
||||
* @return bool
|
||||
*/
|
||||
public function isValid($name)
|
||||
{
|
||||
if(empty($name)) {
|
||||
if (empty($name)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!preg_match('/^[a-zA-Z0-9.]+$/', $name)) {
|
||||
if (!preg_match('/^[a-zA-Z0-9.]+$/', $name)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -21,13 +21,14 @@ class FileSize extends Validator
|
||||
|
||||
public function getDescription()
|
||||
{
|
||||
return 'File size can\'t be bigger than ' . $this->max;
|
||||
return 'File size can\'t be bigger than '.$this->max;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds whether a file size is smaller than required limit
|
||||
* Finds whether a file size is smaller than required limit.
|
||||
*
|
||||
* @param int $fileSize
|
||||
*
|
||||
* @param int $fileSize
|
||||
* @return bool
|
||||
*/
|
||||
public function isValid($fileSize)
|
||||
|
||||
@@ -8,21 +8,21 @@ use Utopia\Validator;
|
||||
class FileType extends Validator
|
||||
{
|
||||
/**
|
||||
* File Types Constants
|
||||
* File Types Constants.
|
||||
*/
|
||||
const FILE_TYPE_JPEG = 'jpeg';
|
||||
const FILE_TYPE_GIF = 'gif';
|
||||
const FILE_TYPE_PNG = 'png';
|
||||
const FILE_TYPE_JPEG = 'jpeg';
|
||||
const FILE_TYPE_GIF = 'gif';
|
||||
const FILE_TYPE_PNG = 'png';
|
||||
|
||||
/**
|
||||
* File Type Binaries
|
||||
* 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_JPEG => "\xFF\xD8\xFF",
|
||||
self::FILE_TYPE_GIF => 'GIF',
|
||||
self::FILE_TYPE_PNG => "\x89\x50\x4e\x47\x0d\x0a",
|
||||
);
|
||||
|
||||
/**
|
||||
@@ -31,7 +31,8 @@ class FileType extends Validator
|
||||
protected $whiteList;
|
||||
|
||||
/**
|
||||
* @param array $whiteList
|
||||
* @param array $whiteList
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __construct(array $whiteList)
|
||||
@@ -51,12 +52,14 @@ class FileType extends Validator
|
||||
}
|
||||
|
||||
/**
|
||||
* Is Valid
|
||||
* Is Valid.
|
||||
*
|
||||
* Binary check to finds whether a file is of valid type
|
||||
*
|
||||
* @see http://stackoverflow.com/a/3313196
|
||||
* @param string $path
|
||||
*
|
||||
* @param string $path
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isValid($path)
|
||||
|
||||
Reference in new issue
Block a user