From 6f09bdc25f6ed7f731db4409d0072f3dcc762b18 Mon Sep 17 00:00:00 2001 From: Stephen Lang Date: Sun, 29 Jun 2014 18:00:39 +0100 Subject: [PATCH] Implement memory-efficient streaming save --- src/Fetch/Attachment.php | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/Fetch/Attachment.php b/src/Fetch/Attachment.php index 96f7082..50abf49 100644 --- a/src/Fetch/Attachment.php +++ b/src/Fetch/Attachment.php @@ -203,9 +203,29 @@ class Attachment if (($filePointer = fopen($path, 'w')) == false) return false; - $results = fwrite($filePointer, $this->getData()); + switch ($this->encoding) { + case 3: + case 'base64': + $streamFilter = stream_filter_append($filePointer, 'convert.base64-decode', STREAM_FILTER_WRITE); + break; + + case 4: + case 'quoted-printable': + $streamFilter = stream_filter_append($handle, 'convert.quoted-printable', STREAM_FILTER_WRITE); + break; + + default: + $streamFilter = null; + } + + $result = imap_savebody($this->imapStream, $filePointer, $this->messageId, $this->partId ?: 1, FT_UID); + + if ($streamFilter) { + stream_filter_remove($streamFilter); + } + fclose($filePointer); - return is_numeric($results); + return $result; } }