feat: implement download

Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
2026-05-23 20:18:58 -04:00
parent 640e3aa811
commit 9cdebd82b8
15 changed files with 336 additions and 172 deletions

View File

@@ -6,9 +6,11 @@ namespace KTXM\ProviderImap\Client\Protocol;
use Generator;
use KTXM\ProviderImap\Client\Command\CommandInterface;
use KTXM\ProviderImap\Client\FetchTarget;
use KTXM\ProviderImap\Client\ImapException;
use KTXM\ProviderImap\Client\Protocol\Response\TaggedResponse;
use KTXM\ProviderImap\Client\Protocol\Response\UntaggedResponse;
use KTXM\ProviderImap\Client\Protocol\RequestFrame;
use KTXM\ProviderImap\Client\SessionContext;
use KTXM\ProviderImap\Client\SessionState;
use Psr\Log\LoggerInterface;
@@ -41,10 +43,42 @@ final class CommandExecutor
$this->writer->write($tag, $frame);
return $command->handle(new ResponseStream(function () use ($tag, $context): Generator {
yield from $this->responsesUntilCompletion($tag, $context);
yield from $this->processPerform($tag, $context);
}), $context);
}
/**
* Stream the raw bytes of a single IMAP BODY section without buffering.
*
* Sends a UID FETCH for the given section and yields the literal bytes
* directly from the socket in chunks, never assembling a full string.
* The caller MUST fully exhaust the returned Generator before issuing
* any further IMAP commands.
*
* @return \Generator<string> raw (transfer-encoded) bytes from the socket
*/
public function download(FetchTarget $target, string $section, int $chunkSize, SessionContext $context): \Generator
{
$this->assertState([SessionState::Selected], $context->state(), 'FETCH (download)');
$tag = $this->tags->next();
$this->writer->write($tag, new RequestFrame(sprintf(
'UID FETCH %s (UID BODY[%s])',
$target->sequenceSet()->toCommand(),
$section,
)));
$result = $this->reader->readUntilFetchLiteral($tag);
if ($result === null) {
return; // UID not found or empty FETCH result
}
yield from $this->reader->streamLiteral($result['literalLength'], $chunkSize);
$this->reader->readToEnd($tag);
}
/**
* @param list<SessionState> $allowedStates
*/
@@ -63,7 +97,7 @@ final class CommandExecutor
));
}
private function responsesUntilCompletion(string $tag, SessionContext $context): Generator
private function processPerform(string $tag, SessionContext $context): Generator
{
while (true) {
$response = $this->reader->readResponse();