mirror of
https://github.com/the-djmaze/snappymail.git
synced 2026-06-26 08:16:44 +03:00
Reduce memory usage on addressbook vcf import
This commit is contained in:
parent
e6bdb37ca1
commit
9466fc71a8
3 changed files with 27 additions and 35 deletions
|
|
@ -253,15 +253,24 @@ trait Contacts
|
|||
|
||||
private function importContactsFromVcfFile(\RainLoop\Model\Account $oAccount, /*resource*/ $rFile): int
|
||||
{
|
||||
$iCount = 0;
|
||||
$oAddressBookProvider = $this->AddressBookProvider($oAccount);
|
||||
if (\is_resource($rFile) && $oAddressBookProvider && $oAddressBookProvider->IsActive()) {
|
||||
$sFile = \stream_get_contents($rFile);
|
||||
if (\is_string($sFile) && 5 < \strlen($sFile)) {
|
||||
try
|
||||
{
|
||||
$this->Logger()->Write('Import contacts from vcf');
|
||||
return $oAddressBookProvider->ImportVcfFile($sFile);
|
||||
foreach (\RainLoop\Providers\AddressBook\Utils::VcfStreamToContacts($rFile) as $oContact) {
|
||||
if ($oAddressBookProvider->ContactSave($oContact)) {
|
||||
++$iCount;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (\Throwable $oExc)
|
||||
{
|
||||
$this->Logger()->WriteException($oExc);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
return $iCount;
|
||||
}
|
||||
|
||||
private function importContactsFromCsvFile(\RainLoop\Model\Account $oAccount, /*resource*/ $rFile, string $sFileStart): int
|
||||
|
|
@ -269,13 +278,20 @@ trait Contacts
|
|||
$iCount = 0;
|
||||
$oAddressBookProvider = $this->AddressBookProvider($oAccount);
|
||||
if (\is_resource($rFile) && $oAddressBookProvider && $oAddressBookProvider->IsActive()) {
|
||||
$this->oLogger->Write('Import contacts from csv');
|
||||
$sDelimiter = ((int)\strpos($sFileStart, ',') > (int)\strpos($sFileStart, ';')) ? ',' : ';';
|
||||
foreach (\RainLoop\Providers\AddressBook\Utils::CsvStreamToContacts($rFile, $sDelimiter) as $oContact) {
|
||||
if ($oAddressBookProvider->ContactSave($oContact)) {
|
||||
++$iCount;
|
||||
try
|
||||
{
|
||||
$this->Logger()->Write('Import contacts from csv');
|
||||
$sDelimiter = ((int)\strpos($sFileStart, ',') > (int)\strpos($sFileStart, ';')) ? ',' : ';';
|
||||
foreach (\RainLoop\Providers\AddressBook\Utils::CsvStreamToContacts($rFile, $sDelimiter) as $oContact) {
|
||||
if ($oAddressBookProvider->ContactSave($oContact)) {
|
||||
++$iCount;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (\Throwable $oExc)
|
||||
{
|
||||
$this->Logger()->WriteException($oExc);
|
||||
}
|
||||
}
|
||||
return $iCount;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -82,24 +82,4 @@ class AddressBook extends AbstractProvider
|
|||
{
|
||||
return $this->IsActive() ? $this->oDriver->IncFrec($aEmails, $bCreateAuto) : false;
|
||||
}
|
||||
|
||||
public function ImportVcfFile(string $sVcfData) : int
|
||||
{
|
||||
$iCount = 0;
|
||||
if ($this->IsActive()) {
|
||||
try
|
||||
{
|
||||
foreach (AddressBook\Utils::VcfFileToContacts($sVcfData) as $oContact) {
|
||||
if ($this->ContactSave($oContact)) {
|
||||
++$iCount;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (\Throwable $oExc)
|
||||
{
|
||||
$this->Logger()->WriteException($oExc);
|
||||
}
|
||||
}
|
||||
return $iCount;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -198,13 +198,9 @@ class Utils
|
|||
));
|
||||
}
|
||||
|
||||
public static function VcfFileToContacts(string $sVcfData) : iterable
|
||||
public static function VcfStreamToContacts(/*resource*/ $rFile) : iterable
|
||||
{
|
||||
$sVcfData = \trim($sVcfData);
|
||||
if ("\xef\xbb\xbf" === \substr($sVcfData, 0, 3)) {
|
||||
$sVcfData = \substr($sVcfData, 3);
|
||||
}
|
||||
$oVCardSplitter = new \Sabre\VObject\Splitter\VCard($sVcfData);
|
||||
$oVCardSplitter = new \Sabre\VObject\Splitter\VCard($rFile);
|
||||
if ($oVCardSplitter) {
|
||||
while ($oVCard = $oVCardSplitter->getNext()) {
|
||||
if ($oVCard instanceof \Sabre\VObject\Component\VCard) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue