generated from Nodarx/template
feat: implement download
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -9,10 +9,8 @@ declare(strict_types=1);
|
||||
|
||||
namespace KTXM\ProviderImap\Providers;
|
||||
|
||||
use Gricob\IMAP\Protocol\Response\Line\Data\Fetch\BodyStructure\MultiPart;
|
||||
use Gricob\IMAP\Protocol\Response\Line\Data\Fetch\BodyStructure\Part;
|
||||
use Gricob\IMAP\Protocol\Response\Line\Data\Fetch\BodyStructure\SinglePart;
|
||||
use KTXF\Mail\Object\MessagePartMutableAbstract;
|
||||
use KTXM\ProviderImap\Client\MessagePart as ImapMessagePart;
|
||||
|
||||
/**
|
||||
* Mail Message Part Implementation
|
||||
@@ -25,7 +23,7 @@ class MessagePart extends MessagePartMutableAbstract {
|
||||
* @param Part $part gricob BodyStructure Part (SinglePart or MultiPart)
|
||||
* @param string $partId numeric part identifier (e.g. "1", "1.1", "2")
|
||||
*/
|
||||
public function fromImap(Part $part, string $partId = '1'): static {
|
||||
public function fromImap(ImapMessagePart $part, string $partId = '1'): static {
|
||||
|
||||
$this->data['partId'] = $partId;
|
||||
|
||||
@@ -104,82 +102,4 @@ class MessagePart extends MessagePartMutableAbstract {
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert message part to store array
|
||||
*/
|
||||
public function toStore(): array {
|
||||
$data = $this->data;
|
||||
if (count($this->parts) > 0) {
|
||||
$data['subParts'] = [];
|
||||
foreach ($this->parts as $subPart) {
|
||||
if ($subPart instanceof MessagePart) {
|
||||
$data['subParts'][] = $subPart->toStore();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$data['subParts'] = null;
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Hydrate message part from store array
|
||||
*/
|
||||
public function fromStore(array $data): static {
|
||||
if (isset($data['subParts']) && is_array($data['subParts'])) {
|
||||
foreach ($data['subParts'] as $subPart) {
|
||||
$this->parts[] = (new MessagePart())->fromStore($subPart);
|
||||
}
|
||||
unset($data['subParts']);
|
||||
}
|
||||
$this->data = $data;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inject decoded body content from a map of IMAP section-ID → raw encoded text.
|
||||
*
|
||||
* Walks the MessagePart tree recursively. For each text/* leaf part whose
|
||||
* partId is present in $sectionMap the raw text is decoded according to the
|
||||
* part's Content-Transfer-Encoding and converted to UTF-8 before being
|
||||
* stored in 'content'. Binary parts (images, PDFs, …) are skipped.
|
||||
*
|
||||
* @param array<string,string> $sectionMap Keys: IMAP section IDs (e.g. "1", "1.2");
|
||||
* Values: raw (transfer-encoded) body text
|
||||
*/
|
||||
public function injectSections(array $sectionMap): void
|
||||
{
|
||||
// MultiPart: recurse into children
|
||||
if (!empty($this->parts)) {
|
||||
foreach ($this->parts as $childPart) {
|
||||
if ($childPart instanceof MessagePart) {
|
||||
$childPart->injectSections($sectionMap);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// SinglePart: only inject decoded content for text/* MIME types
|
||||
$type = strtolower($this->data['type'] ?? '');
|
||||
if (!str_starts_with($type, 'text/')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$partId = $this->data['partId'] ?? null;
|
||||
if ($partId === null || !array_key_exists($partId, $sectionMap)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$raw = $sectionMap[$partId];
|
||||
$encoding = strtolower($this->data['encoding'] ?? '7bit');
|
||||
$decoded = match ($encoding) {
|
||||
'quoted-printable' => quoted_printable_decode($raw),
|
||||
'base64' => base64_decode($raw, strict: false),
|
||||
default => $raw, // 7bit, 8bit, binary
|
||||
};
|
||||
|
||||
$charset = $this->data['charset'] ?? 'us-ascii';
|
||||
$this->data['content'] = MessageProperties::toUtf8($decoded, $charset);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user