mirror of
https://github.com/the-djmaze/snappymail.git
synced 2026-07-13 03:27:39 +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
|
private function importContactsFromVcfFile(\RainLoop\Model\Account $oAccount, /*resource*/ $rFile): int
|
||||||
{
|
{
|
||||||
|
$iCount = 0;
|
||||||
$oAddressBookProvider = $this->AddressBookProvider($oAccount);
|
$oAddressBookProvider = $this->AddressBookProvider($oAccount);
|
||||||
if (\is_resource($rFile) && $oAddressBookProvider && $oAddressBookProvider->IsActive()) {
|
if (\is_resource($rFile) && $oAddressBookProvider && $oAddressBookProvider->IsActive()) {
|
||||||
$sFile = \stream_get_contents($rFile);
|
try
|
||||||
if (\is_string($sFile) && 5 < \strlen($sFile)) {
|
{
|
||||||
$this->Logger()->Write('Import contacts from vcf');
|
$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
|
private function importContactsFromCsvFile(\RainLoop\Model\Account $oAccount, /*resource*/ $rFile, string $sFileStart): int
|
||||||
|
|
@ -269,13 +278,20 @@ trait Contacts
|
||||||
$iCount = 0;
|
$iCount = 0;
|
||||||
$oAddressBookProvider = $this->AddressBookProvider($oAccount);
|
$oAddressBookProvider = $this->AddressBookProvider($oAccount);
|
||||||
if (\is_resource($rFile) && $oAddressBookProvider && $oAddressBookProvider->IsActive()) {
|
if (\is_resource($rFile) && $oAddressBookProvider && $oAddressBookProvider->IsActive()) {
|
||||||
$this->oLogger->Write('Import contacts from csv');
|
try
|
||||||
$sDelimiter = ((int)\strpos($sFileStart, ',') > (int)\strpos($sFileStart, ';')) ? ',' : ';';
|
{
|
||||||
foreach (\RainLoop\Providers\AddressBook\Utils::CsvStreamToContacts($rFile, $sDelimiter) as $oContact) {
|
$this->Logger()->Write('Import contacts from csv');
|
||||||
if ($oAddressBookProvider->ContactSave($oContact)) {
|
$sDelimiter = ((int)\strpos($sFileStart, ',') > (int)\strpos($sFileStart, ';')) ? ',' : ';';
|
||||||
++$iCount;
|
foreach (\RainLoop\Providers\AddressBook\Utils::CsvStreamToContacts($rFile, $sDelimiter) as $oContact) {
|
||||||
|
if ($oAddressBookProvider->ContactSave($oContact)) {
|
||||||
|
++$iCount;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch (\Throwable $oExc)
|
||||||
|
{
|
||||||
|
$this->Logger()->WriteException($oExc);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return $iCount;
|
return $iCount;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -82,24 +82,4 @@ class AddressBook extends AbstractProvider
|
||||||
{
|
{
|
||||||
return $this->IsActive() ? $this->oDriver->IncFrec($aEmails, $bCreateAuto) : false;
|
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);
|
$oVCardSplitter = new \Sabre\VObject\Splitter\VCard($rFile);
|
||||||
if ("\xef\xbb\xbf" === \substr($sVcfData, 0, 3)) {
|
|
||||||
$sVcfData = \substr($sVcfData, 3);
|
|
||||||
}
|
|
||||||
$oVCardSplitter = new \Sabre\VObject\Splitter\VCard($sVcfData);
|
|
||||||
if ($oVCardSplitter) {
|
if ($oVCardSplitter) {
|
||||||
while ($oVCard = $oVCardSplitter->getNext()) {
|
while ($oVCard = $oVCardSplitter->getNext()) {
|
||||||
if ($oVCard instanceof \Sabre\VObject\Component\VCard) {
|
if ($oVCard instanceof \Sabre\VObject\Component\VCard) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue