Small StorageType change

This commit is contained in:
the-djmaze 2024-02-26 16:44:25 +01:00
parent 2559c09b23
commit ab748aab99
5 changed files with 70 additions and 58 deletions

View file

@ -12,7 +12,7 @@ class NextcloudStorage extends \RainLoop\Providers\Storage\FileStorage
$sDataPath = parent::GenerateFilePath($mAccount, $iStorageType, $bMkDir); $sDataPath = parent::GenerateFilePath($mAccount, $iStorageType, $bMkDir);
if (StorageType::CONFIG === $iStorageType) { if (StorageType::CONFIG === $iStorageType) {
$sUID = \OC::$server->getUserSession()->getUser()->getUID(); $sUID = \OC::$server->getUserSession()->getUser()->getUID();
$sDataPath .= "/.config/{$sUID}/"; $sDataPath .= ".config/{$sUID}/";
if ($bMkDir && !\is_dir($sDataPath) && !\mkdir($sDataPath, 0700, true)) { if ($bMkDir && !\is_dir($sDataPath) && !\mkdir($sDataPath, 0700, true)) {
throw new \RainLoop\Exceptions\Exception('Can\'t make storage directory "'.$sDataPath.'"'); throw new \RainLoop\Exceptions\Exception('Can\'t make storage directory "'.$sDataPath.'"');
} }

View file

@ -306,4 +306,11 @@ abstract class Account implements \JsonSerializable
return $bResult; return $bResult;
} }
/*
// Stores settings in AdditionalAccount else MainAccount
public function settingsLocal() : \RainLoop\Settings
{
return \RainLoop\Api::Actions()->SettingsProvider(true)->Load($this);
}
*/
} }

View file

@ -63,4 +63,11 @@ class MainAccount extends Account
{ {
$this->sCryptKey = new SensitiveString($sKey); $this->sCryptKey = new SensitiveString($sKey);
} }
/*
// Stores settings in MainAccount
public function settings() : \RainLoop\Settings
{
return \RainLoop\Api::Actions()->SettingsProvider()->Load($this);
}
*/
} }

View file

@ -10,11 +10,10 @@ enum StorageType: int {
*/ */
abstract class StorageType abstract class StorageType
{ {
const USER = 1; const CONFIG = 1;
const CONFIG = 2; const NOBODY = 2;
const NOBODY = 3; const SIGN_ME = 3;
const SIGN_ME = 4; const SESSION = 4;
const SESSION = 5; const PGP = 5;
const PGP = 6; const ROOT = 6;
const ROOT = 7;
} }

View file

@ -90,58 +90,57 @@ class FileStorage implements \RainLoop\Providers\Storage\IStorage
*/ */
public function GenerateFilePath($mAccount, int $iStorageType, bool $bMkDir = false) : string public function GenerateFilePath($mAccount, int $iStorageType, bool $bMkDir = false) : string
{ {
$sEmail = $sSubFolder = ''; $sEmail = $sSubFolder = $sFilePath = '';
if (null === $mAccount) { if (null === $mAccount || StorageType::NOBODY === $iStorageType) {
$iStorageType = StorageType::NOBODY; $sFilePath = $this->sDataPath.'/__nobody__/';
} else if ($mAccount instanceof \RainLoop\Model\MainAccount) { } else {
$sEmail = $mAccount->Email(); if ($mAccount instanceof \RainLoop\Model\MainAccount) {
} else if ($mAccount instanceof \RainLoop\Model\AdditionalAccount) { $sEmail = $mAccount->Email();
$sEmail = $mAccount->ParentEmail(); } else if ($mAccount instanceof \RainLoop\Model\AdditionalAccount) {
if ($this->bLocal) { $sEmail = $mAccount->ParentEmail();
$sSubFolder = $mAccount->Email(); if ($this->bLocal) {
} $sSubFolder = $mAccount->Email();
} else if (\is_string($mAccount)) {
$sEmail = $mAccount;
}
if ($sEmail) {
if (StorageType::SIGN_ME === $iStorageType) {
$sSubFolder = '.sign_me';
} else if (StorageType::SESSION === $iStorageType) {
$sSubFolder = '.sessions';
} else if (StorageType::PGP === $iStorageType) {
$sSubFolder = '.pgp';
} else if (StorageType::ROOT === $iStorageType) {
$sSubFolder = '';
}
}
$sFilePath = '';
switch ($iStorageType)
{
case StorageType::NOBODY:
$sFilePath = $this->sDataPath.'/__nobody__/';
break;
case StorageType::SIGN_ME:
case StorageType::SESSION:
case StorageType::CONFIG:
case StorageType::PGP:
case StorageType::ROOT:
if (empty($sEmail)) {
return '';
} }
if (\is_dir("{$this->sDataPath}/cfg")) { } else if (\is_string($mAccount)) {
\SnappyMail\Upgrade::FileStorage($this->sDataPath); $sEmail = $mAccount;
}
if ($sEmail) {
// these are never local
if (StorageType::SIGN_ME === $iStorageType) {
$sSubFolder = '.sign_me';
} else if (StorageType::SESSION === $iStorageType) {
$sSubFolder = '.sessions';
} else if (StorageType::PGP === $iStorageType) {
$sSubFolder = '.pgp';
} else if (StorageType::ROOT === $iStorageType) {
$sSubFolder = '';
} }
$aEmail = \explode('@', $sEmail ?: 'nobody@unknown.tld'); }
$sDomain = \trim(1 < \count($aEmail) ? \array_pop($aEmail) : '');
$sFilePath = $this->sDataPath switch ($iStorageType)
.'/'.\MailSo\Base\Utils::SecureFileName($sDomain ?: 'unknown.tld') {
.'/'.\MailSo\Base\Utils::SecureFileName(\implode('@', $aEmail) ?: '.unknown') case StorageType::CONFIG:
.'/'.($sSubFolder ? \MailSo\Base\Utils::SecureFileName($sSubFolder).'/' : ''); case StorageType::SIGN_ME:
break; case StorageType::SESSION:
default: case StorageType::PGP:
throw new \Exception("Invalid storage type {$iStorageType}"); case StorageType::ROOT:
if (empty($sEmail)) {
return '';
}
if (\is_dir("{$this->sDataPath}/cfg")) {
\SnappyMail\Upgrade::FileStorage($this->sDataPath);
}
$aEmail = \explode('@', $sEmail ?: 'nobody@unknown.tld');
$sDomain = \trim(1 < \count($aEmail) ? \array_pop($aEmail) : '');
$sFilePath = $this->sDataPath
.'/'.\MailSo\Base\Utils::SecureFileName($sDomain ?: 'unknown.tld')
.'/'.\MailSo\Base\Utils::SecureFileName(\implode('@', $aEmail) ?: '.unknown')
.'/'.($sSubFolder ? \MailSo\Base\Utils::SecureFileName($sSubFolder).'/' : '');
break;
default:
throw new \Exception("Invalid storage type {$iStorageType}");
}
} }
if ($bMkDir && !empty($sFilePath) && !\is_dir($sFilePath) && !\mkdir($sFilePath, 0700, true)) if ($bMkDir && !empty($sFilePath) && !\is_dir($sFilePath) && !\mkdir($sFilePath, 0700, true))