New logic for storage provider (breaking changes)

This commit is contained in:
RainLoop Team 2015-02-05 03:54:26 +04:00
parent 44566aad4b
commit 51666d7c7e
19 changed files with 453 additions and 297 deletions

View file

@ -9,14 +9,21 @@ class DefaultStorage implements \RainLoop\Providers\Storage\StorageInterface
*/
private $sDataPath;
/**
* @var bool
*/
private $bLocal;
/**
* @param string $sStoragePath
* @param bool $bLocal = false
*
* @return void
*/
public function __construct($sStoragePath)
public function __construct($sStoragePath, $bLocal = false)
{
$this->sDataPath = \rtrim(\trim($sStoragePath), '\\/');
$this->bLocal = !!$bLocal;
}
/**
@ -49,7 +56,7 @@ class DefaultStorage implements \RainLoop\Providers\Storage\StorageInterface
{
$mValue = \file_get_contents($sFileName);
}
return false === $mValue ? $mDefault : $mValue;
}
@ -68,33 +75,78 @@ class DefaultStorage implements \RainLoop\Providers\Storage\StorageInterface
{
$mResult = @\unlink($sFileName);
}
return $mResult;
}
/**
* @param \RainLoop\Model\Account|string $oAccount
*
* @return bool
*/
public function DeleteStorage($oAccount)
{
$sPath = $this->generateFileName($oAccount,
\RainLoop\Providers\Storage\Enumerations\StorageType::USER, 'xxx', false, true);
if (!empty($sPath) && \is_dir($sPath))
{
\MailSo\Base\Utils::RecRmDir($sPath);
}
$sPath = $this->generateFileName($oAccount,
\RainLoop\Providers\Storage\Enumerations\StorageType::CONFIG, 'xxx', false, true);
if (!empty($sPath) && \is_dir($sPath))
{
\MailSo\Base\Utils::RecRmDir($sPath);
}
return true;
}
/**
* @return bool
*/
public function IsLocal()
{
return $this->bLocal;
}
/**
* @param \RainLoop\Model\Account|string|null $mAccount
* @param int $iStorageType
* @param string $sKey
* @param bool $bMkDir = false
* @param bool $bForDeleteAction = false
*
* @return string
*/
private function generateFileName($mAccount, $iStorageType, $sKey, $bMkDir = false)
private function generateFileName($mAccount, $iStorageType, $sKey, $bMkDir = false, $bForDeleteAction = false)
{
if (null === $mAccount)
{
$iStorageType = \RainLoop\Providers\Storage\Enumerations\StorageType::NOBODY;
}
$sEmail = $mAccount instanceof \RainLoop\Model\Account ? \preg_replace('/[^a-z0-9\-\.@]+/', '_',
('' === $mAccount->ParentEmail() ? '' : $mAccount->ParentEmail().'/').$mAccount->Email()) : '';
$sEmail = $sSubEmail = '';
if ($mAccount instanceof \RainLoop\Model\Account)
{
$sEmail = $mAccount->ParentEmailHelper();
if ($this->bLocal && $mAccount->IsAdditionalAccount() && !$bForDeleteAction)
{
$sSubEmail = $mAccount->Email();
}
}
if (\is_string($mAccount) && empty($sEmail))
{
$sEmail = \preg_replace('/[^a-z0-9\-\.@]+/', '_', $mAccount);
$sEmail = $mAccount;
}
$sEmail = \preg_replace('/[^a-z0-9\-\.@]+/', '_', $sEmail);
$sSubEmail = \preg_replace('/[^a-z0-9\-\.@]+/', '_', $sSubEmail);
$sTypePath = $sKeyPath = '';
switch ($iStorageType)
{
@ -118,10 +170,13 @@ class DefaultStorage implements \RainLoop\Providers\Storage\StorageInterface
}
else if (!empty($sEmail))
{
$sFilePath = $this->sDataPath.'/'.$sTypePath.'/'.rtrim(substr($sEmail, 0, 2), '@').'/'.$sEmail.'/'.$sKeyPath;
$sFilePath = $this->sDataPath.'/'.$sTypePath.'/'.
\str_pad(\rtrim(\substr($sEmail, 0, 2), '@'), 2, '_').'/'.$sEmail.'/'.
(0 < \strlen($sSubEmail) ? $sSubEmail.'/' : '').
($bForDeleteAction ? '' : $sKeyPath);
}
if ($bMkDir && !empty($sFilePath) && !@\is_dir(\dirname($sFilePath)))
if ($bMkDir && !$bForDeleteAction && !empty($sFilePath) && !@\is_dir(\dirname($sFilePath)))
{
if (!@\mkdir(\dirname($sFilePath), 0755, true))
{

View file

@ -32,4 +32,9 @@ interface StorageInterface
* @return bool
*/
public function Clear($oAccount, $iStorageType, $sKey);
/**
* @return bool
*/
public function IsLocal();
}